Skip to content

Commit 5779df7

Browse files
committed
Remove the legacy %re source extension
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 8ea4775 commit 5779df7

14 files changed

Lines changed: 161 additions & 86 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#### :boom: Breaking Change
1616

17+
- Remove `%re`; use regexp literals instead. https://github.com/rescript-lang/rescript/pull/8610
1718
- Reject malformed UTF-8 in documentation comments and invalid string or template literal escapes that were previously accepted, including empty or out-of-range braced Unicode escapes (`\u{}`, `\u{110000}`) and legacy decimal or octal escapes in templates (`\1`, `\01`, `\8`). These inputs now produce syntax diagnostics instead of compiling to invalid or inconsistent JavaScript. https://github.com/rescript-lang/rescript/pull/8606
1819
- Reject tagged template literals in patterns. Patterns cannot invoke their tag; previously their raw payload was compiled as a plain string comparison. https://github.com/rescript-lang/rescript/pull/8606
1920
- Remove runtime APIs that were deprecated for removal in ReScript 13, including the `Char` module, unsafe `Obj` operations, legacy `Pervasives` helpers, and `Array.unsafe_get`. https://github.com/rescript-lang/rescript/pull/8564

compiler/frontend/ast_exp_extension.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ let handle_extension e (_self : Ast_mapper.mapper)
5656
| Some msg -> " - Todo: " ^ msg)) );
5757
]
5858
| "ffi" -> Ast_exp_handle_external.handle_ffi ~loc ~payload
59-
| "raw" -> Ast_exp_handle_external.handle_raw ~kind:Raw_exp loc payload
60-
| "re" ->
61-
Exp.constraint_ ~loc
62-
(Ast_exp_handle_external.handle_raw ~kind:Raw_re loc payload)
63-
(Ast_comb.to_regexp_type loc)
59+
| "raw" -> Ast_exp_handle_external.handle_raw loc payload
6460
| "debugger" ->
6561
{e with pexp_desc = Ast_exp_handle_external.handle_debugger loc payload}
6662
| _ -> e

compiler/frontend/ast_exp_handle_external.ml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,13 @@ let handle_debugger loc (payload : Ast_payload.t) =
3434
| _ ->
3535
Location.raise_errorf ~loc "%%debugger extension doesn't accept arguments"
3636

37-
let handle_raw ~kind loc payload =
37+
let handle_raw loc payload =
3838
let is_function = ref None in
39-
match Ast_payload.raw_as_string_exp_exn ~kind ~is_function payload with
40-
| None -> (
41-
match kind with
42-
| Raw_re ->
43-
Location.raise_errorf ~loc
44-
"%%re extension can only be applied to a string"
45-
| Raw_exp ->
46-
Location.raise_errorf ~loc
47-
"%%raw extension can only be applied to a string"
48-
| Raw_program ->
49-
Location.raise_errorf ~loc
50-
"%%%%raw extension can only be applied to a string")
39+
match
40+
Ast_payload.raw_as_string_exp_exn ~kind:Raw_exp ~is_function payload
41+
with
42+
| None ->
43+
Location.raise_errorf ~loc "%%raw extension can only be applied to a string"
5144
| Some exp ->
5245
{
5346
exp with

compiler/frontend/ast_exp_handle_external.mli

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ val handle_debugger : Location.t -> Ast_payload.t -> Parsetree.expression_desc
2626

2727
val handle_ffi : loc:Location.t -> payload:Ast_payload.t -> Parsetree.expression
2828

29-
val handle_raw :
30-
kind:Js_raw_info.raw_kind ->
31-
Location.t ->
32-
Ast_payload.t ->
33-
Parsetree.expression
29+
val handle_raw : Location.t -> Ast_payload.t -> Parsetree.expression
3430

3531
val handle_raw_structure :
3632
Location.t -> Ast_payload.t -> Parsetree.structure_item

compiler/ml/ast_mapper_to0.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ module E = struct
653653
(sub.pat sub pat) start_expr end_expr Asttypes.Upto
654654
(sub.expr sub body_expr)
655655
| Pexp_regexp {pattern; flags} ->
656+
(* %re is only a frozen PPX wire encoding, not source syntax. *)
656657
extension ~loc ~attrs
657658
( Location.mkloc "re" loc,
658659
Pt.PStr

compiler/ml/parsetree.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type constant =
5454
otherwise the frontend rejects it. The string is JavaScript source, not a
5555
decoded ReScript string value. *)
5656
| Pconst_raw_source of string
57-
(* JavaScript source carried by a compiler extension such as [raw], [ffi], or
58-
[re]. For example, [%raw("x + 1")] stores ["x + 1"]. The extension
57+
(* JavaScript source carried by a compiler extension such as [raw] or [ffi].
58+
For example, [%raw("x + 1")] stores ["x + 1"]. The extension
5959
interprets the string as JavaScript source rather than as a ReScript
6060
runtime string value. *)
6161
| Pconst_float of string * char option

compiler/syntax/src/res_core.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,14 +2153,12 @@ and parse_atomic_expr p =
21532153
Parser.next_regex_token p;
21542154
match p.token with
21552155
| Regex (pattern, flags) -> parse_regex ~start_pos p pattern flags
2156-
| _ -> Ast_helper.Exp.extension (Location.mknoloc "re", Parsetree.PStr [])
2157-
)
2156+
| _ -> assert false (* next_regex_token always returns Regex. *))
21582157
| ForwardslashDot -> (
21592158
Parser.next_regex_token p;
21602159
match p.token with
21612160
| Regex (pattern, flags) -> parse_regex ~start_pos p ("." ^ pattern) flags
2162-
| _ -> Ast_helper.Exp.extension (Location.mknoloc "re", Parsetree.PStr [])
2163-
)
2161+
| _ -> assert false (* next_regex_token always returns Regex. *))
21642162
| token -> (
21652163
let err_pos = p.prev_end_pos in
21662164
Parser.err ~start_pos:err_pos p
@@ -7672,9 +7670,14 @@ and parse_extension ?(module_language = false) p =
76727670
else Parser.expect Percent p;
76737671
let attr_id = parse_attribute_id ~start_pos p in
76747672
let payload = parse_payload p in
7673+
if attr_id.txt = "re" then
7674+
Parser.err ~start_pos:attr_id.loc.loc_start ~end_pos:attr_id.loc.loc_end p
7675+
(Diagnostics.message
7676+
"The %re extension has been removed. Use a regexp literal such as \
7677+
/abc/i.");
76757678
let payload =
76767679
match (attr_id.txt, payload) with
7677-
| ( ("raw" | "ffi" | "re"),
7680+
| ( ("raw" | "ffi"),
76787681
Parsetree.PStr
76797682
[
76807683
({

compiler/syntax/src/res_printer.ml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,20 +3462,8 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34623462
Doc.soft_line;
34633463
Doc.rbrace;
34643464
])
3465-
| Pexp_extension extension -> (
3466-
match extension with
3467-
| ( {txt = "re"},
3468-
PStr
3469-
[
3470-
{
3471-
pstr_desc =
3472-
Pstr_eval
3473-
({pexp_desc = Pexp_constant (Pconst_raw_source expr)}, []);
3474-
};
3475-
] ) ->
3476-
Doc.text expr
3477-
| extension ->
3478-
print_extension ~state ~at_module_lvl:false extension cmt_tbl)
3465+
| Pexp_extension extension ->
3466+
print_extension ~state ~at_module_lvl:false extension cmt_tbl
34793467
| Pexp_regexp {pattern; flags} -> Doc.text ("/" ^ pattern ^ "/" ^ flags)
34803468
| Pexp_template {source_segments; values} ->
34813469
print_template_literal ~state ~source_segments ~values cmt_tbl
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Syntax error!
3+
/.../fixtures/syntaxErrorsRemovedReExtension.res:1:10-12
4+
5+
1 │ let re = %re("/abc/i")
6+
2 │ // Use the literal syntax instead.
7+
3 │ let literal = /abc/i
8+
9+
The %re extension has been removed. Use a regexp literal such as /abc/i.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let re = %re("/abc/i")
2+
// Use the literal syntax instead.
3+
let literal = /abc/i

0 commit comments

Comments
 (0)