Skip to content

Commit a8794b0

Browse files
committed
Reject tagged template literals in patterns
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 558b6c1 commit a8794b0

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

compiler/frontend/ast_utf8_string_interp.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ let transform_pat (p : Parsetree.pattern) s delim : Parsetree.pattern =
328328
Ppat_constant
329329
(Pconst_string (s, Delim.some_escaped_back_quote_delimiter));
330330
}
331-
| Unrecognized -> p
331+
| Unrecognized ->
332+
Location.raise_errorf ~loc:p.ppat_loc
333+
"Tagged template literals are not supported in patterns"
332334

333335
let parse_processed_delim = External_arg_spec.parse_processed_delim

tests/ounit_tests/ounit_string_literal_tests.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ let assert_invalid_backquoted_pattern encoded =
1919
| _ -> OUnit.assert_failure "expected an invalid string escape"
2020
| exception Location.Error _ -> ()
2121

22+
let assert_invalid_tagged_pattern tag contents =
23+
let pattern =
24+
Ast_helper.Pat.constant (Parsetree.Pconst_string (contents, Some tag))
25+
in
26+
match Ast_utf8_string_interp.transform_pat pattern contents tag with
27+
| _ -> OUnit.assert_failure "expected a tagged pattern error"
28+
| exception Location.Error _ -> ()
29+
2230
let suites =
2331
__FILE__
2432
>::: [
@@ -70,6 +78,11 @@ let suites =
7078
( "backquoted patterns reject lone surrogate escapes" >:: fun _ ->
7179
assert_invalid_backquoted_pattern {|\uD800|};
7280
assert_invalid_backquoted_pattern {|\uDC00|} );
81+
( "patterns reject tagged template literals" >:: fun _ ->
82+
(* A tagged pattern cannot invoke its tag. Treating its raw contents as
83+
a string made json`\x61` collide with the ordinary "\\x61"
84+
pattern during string-switch sorting. *)
85+
assert_invalid_tagged_pattern "json" {|\x61|} );
7386
( "comparison uses runtime values" >:: fun _ ->
7487
assert_same_runtime_value ("a", Some "*j") ({|\x61|}, Some "*j");
7588
assert_same_runtime_value ("😀", None) ({|\u{1f600}|}, Some "*j");

0 commit comments

Comments
 (0)