Skip to content

Commit 5352379

Browse files
viratyosinmeta-codesync[bot]
authored andcommitted
Allow prefixed string expression (regex strings like re'/foo/') in constant initializer positions
Summary: These are constants. Reviewed By: madgen Differential Revision: D89828200 fbshipit-source-id: a8be49c24d6df8d5aac9160018f42baab25bbc80
1 parent d425184 commit 5352379

8 files changed

Lines changed: 41 additions & 2 deletions

hphp/hack/src/parser/rust_parser_errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4827,6 +4827,14 @@ impl<'a, State: 'a + Clone> ParserErrors<'a, State> {
48274827
default(self);
48284828
}
48294829
}
4830+
PrefixedStringExpression(x) => {
4831+
// Allow prefixed strings (e.g., re'/foo/') in constant expressions for runtime only
4832+
if self.env.is_typechecker() {
4833+
default(self)
4834+
} else {
4835+
self.check_constant_expression(&x.str, static_allowed)
4836+
}
4837+
}
48304838
_ => default(self),
48314839
}
48324840
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?hh
2+
3+
function test_with_default(
4+
HH\Lib\Regex\Pattern<shape(...)> $pattern = re'/default/',
5+
): void {
6+
echo $pattern;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No errors
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?hh
2+
3+
enum PatternEnum: HH\Lib\Regex\Pattern<shape(...)> as string {
4+
SIMPLE = re'/simple/';
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ERROR: File "prefixed_string_enum_values.php", line 4, characters 12-23:
2+
Expected constant expression for initializer (Parsing[1002])
3+
ERROR: File "prefixed_string_enum_values.php", line 4, characters 12-23:
4+
Illegal constant value (Naming[2023])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?hh
2+
3+
class MyClass {
4+
const HH\Lib\Regex\Pattern<shape(...)> PATTERN = re'/[a-z]+/';
5+
const HH\Lib\Regex\Pattern<shape('x' => string, ...)> NAMED =
6+
re'/(?P<x>[a-z]+)/';
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ERROR: File "prefixed_string_in_constants.php", line 4, characters 52-63:
2+
Expected constant expression for initializer (Parsing[1002])
3+
ERROR: File "prefixed_string_in_constants.php", line 6, characters 5-23:
4+
Expected constant expression for initializer (Parsing[1002])
5+
ERROR: File "prefixed_string_in_constants.php", line 4, characters 52-63:
6+
Illegal constant value (Naming[2023])
7+
ERROR: File "prefixed_string_in_constants.php", line 6, characters 5-23:
8+
Illegal constant value (Naming[2023])

hphp/test/slow/prefixed_string/prefixed_string.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?hh
22

3-
function f(): void {
4-
$x = re"blah blah\n";
3+
function f(string $x = re"blah blah\n"): void {
54
echo($x);
65
$s1 = "Be";
76
$s2 = "diff";

0 commit comments

Comments
 (0)