Skip to content

Commit bbac536

Browse files
cknittclaude
andauthored
Keep _-prefixed exotic identifiers escaped, reject \"_\" (#8795)
* Keep _-prefixed exotic identifiers escaped when printing Escape names like \"_'\" and \"_\" that the scanner would read as a wildcard. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * CHANGELOG Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Reject \"_\" as a quoted identifier It couldn't be told apart from the wildcard and the placeholder of underscore apply sugar, e.g. f(\"_\") was parsed as f(_). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NrGqwcpQ2sF3RiHA48TxmU Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 78e29c1 commit bbac536

10 files changed

Lines changed: 88 additions & 5 deletions

File tree

‎CHANGELOG.md‎

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

1515
#### :boom: Breaking Change
1616

17+
- `\"_"` is no longer allowed as a quoted identifier, as it couldn't be told apart from the `_` wildcard and placeholder. https://github.com/rescript-lang/rescript/pull/8795
1718
- Record syntax no longer creates or matches dicts, e.g. `let d: dict<int> = {foo: 1}`; it's an error pointing to `dict{...}`. https://github.com/rescript-lang/rescript/pull/8751
1819
- Remove the call-site `@inlined` attribute, which was parsed but never affected code generation. It is now reported as a misplaced attribute (warning 53). https://github.com/rescript-lang/rescript/pull/8734
1920
- Remove `@deriving(abstract)` and `@deriving(jsConverter)`. Use record types (with optional fields, mutable fields and `@as` renaming) and polymorphic variants directly instead. https://github.com/rescript-lang/rescript/pull/8729
@@ -30,6 +31,7 @@
3031

3132
- Fix parser recovery and editor completion when a record or dict value is missing before a comma, e.g. `{a: , b: 1}`. https://github.com/rescript-lang/rescript/pull/8779
3233
- Derive module names from file names consistently, fixing files like `Button.test.res` that could not reference `Button`, and `.res`/`.resi` pairs with dotted or non-ASCII names. https://github.com/rescript-lang/rescript/pull/8797
34+
- Fix the formatter printing escaped identifiers starting with `_`, like `\"_'"`, in a form that doesn't parse. https://github.com/rescript-lang/rescript/pull/8795
3335
- Fix references to a `@val` global reading a binding with the same name, e.g. a toplevel `let` declared before them. https://github.com/rescript-lang/rescript/pull/8785
3436
- Evaluate the unused arguments of an inlined function call in parameter order. https://github.com/rescript-lang/rescript/pull/8786
3537
- Fix shadowed variables in generated JS colliding with identifiers or external names containing `$`. https://github.com/rescript-lang/rescript/pull/8778

‎analysis/src/utils.ml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,13 @@ let print_maybe_exotic_ident ?(allow_uident = false) txt =
250250
else if i == 0 then
251251
match String.unsafe_get txt i with
252252
| 'A' .. 'Z' when allow_uident -> loop (i + 1)
253-
| 'a' .. 'z' | '_' | '$' -> loop (i + 1)
253+
| 'a' .. 'z' | '$' -> loop (i + 1)
254+
(* Same as Res_printer.classify_ident_content: a lone [_] or one
255+
followed by another character is not scanned as an identifier. *)
256+
| '_' when len > 1 -> (
257+
match String.unsafe_get txt 1 with
258+
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '$' -> loop (i + 1)
259+
| _ -> "\"" ^ txt ^ "\"")
254260
| _ -> "\"" ^ txt ^ "\""
255261
else
256262
match String.unsafe_get txt i with

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,13 @@ let classify_ident_content ?(allow_uident = false) ?(allow_hyphen = false) txt =
496496
match String.unsafe_get txt i with
497497
| '\\' -> UppercaseExoticIdent
498498
| 'A' .. 'Z' when allow_uident -> loop (i + 1)
499-
| 'a' .. 'z' | '_' | '$' -> loop (i + 1)
499+
| 'a' .. 'z' | '$' -> loop (i + 1)
500+
(* The scanner reads [_] as an identifier only when one of these
501+
follows; otherwise it is a wildcard. *)
502+
| '_' when len > 1 -> (
503+
match String.unsafe_get txt 1 with
504+
| 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '$' -> loop (i + 1)
505+
| _ -> ExoticIdent)
500506
| _ -> ExoticIdent
501507
else
502508
match String.unsafe_get txt i with
@@ -3338,6 +3344,10 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
33383344
let constr = print_longident_location longident_loc cmt_tbl in
33393345
let args = print_expression_args ~state args cmt_tbl in
33403346
Doc.group (Doc.concat [constr; args])
3347+
| Pexp_ident ({txt = Longident.Lident "_"} as path) ->
3348+
(* The `_` placeholder of underscore apply sugar, see
3349+
[Parsetree_viewer.rewrite_underscore_apply]. *)
3350+
print_comments (Doc.text "_") cmt_tbl path.loc
33413351
| Pexp_ident path -> print_lident_path path cmt_tbl
33423352
| Pexp_tuple exprs ->
33433353
Doc.group

‎compiler/syntax/src/res_scanner.ml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,13 @@ let scan_exotic_identifier scanner =
331331
scanner.err ~start_pos ~end_pos
332332
(Diagnostics.message "A quoted identifier can't be empty string.");
333333
Token.Lident ident)
334+
else if name = "_" then (
335+
(* `_` is the wildcard and the placeholder of underscore apply sugar;
336+
a quoted `_` couldn't be told apart from them. *)
337+
let end_pos = position scanner in
338+
scanner.err ~start_pos ~end_pos
339+
(Diagnostics.message "`_` can't be used as a quoted identifier.");
340+
Token.Lident name)
334341
else if Ext_ident.is_uident name then Token.Lident ident
335342
(* Exotic ident with uppercase letter should be encoded to avoid confusing in OCaml parsetree *)
336343
else Token.Lident name

‎tests/analysis_tests/tests/src/CompletionExpressions.res‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ let arr = ["hello"]
263263
// arr->Belt.Array.map()
264264
// ^com
265265

266-
type exoticPolyvariant = [#"some exotic" | #dollar$]
266+
type exoticPolyvariant = [#"some exotic" | #dollar$ | #"_"]
267267

268268
let takesExotic = (e: exoticPolyvariant) => {
269269
ignore(e)

‎tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,19 +1330,31 @@ Path takesExotic
13301330
"detail": "#\"some exotic\"",
13311331
"documentation": {
13321332
"kind": "markdown",
1333-
"value": "```rescript\n#\"some exotic\"\n```\n\n```rescript\n[#dollar$ | #\"some exotic\"]\n```"
1333+
"value": "```rescript\n#\"some exotic\"\n```\n\n```rescript\n[#\"_\" | #dollar$ | #\"some exotic\"]\n```"
13341334
},
13351335
"insertText": "#\"some exotic\"",
13361336
"insertTextFormat": 2,
13371337
"kind": 4,
13381338
"label": "#\"some exotic\"",
13391339
"tags": []
13401340
},
1341+
{
1342+
"detail": "#\"_\"",
1343+
"documentation": {
1344+
"kind": "markdown",
1345+
"value": "```rescript\n#\"_\"\n```\n\n```rescript\n[#\"_\" | #dollar$ | #\"some exotic\"]\n```"
1346+
},
1347+
"insertText": "#\"_\"",
1348+
"insertTextFormat": 2,
1349+
"kind": 4,
1350+
"label": "#\"_\"",
1351+
"tags": []
1352+
},
13411353
{
13421354
"detail": "#dollar$",
13431355
"documentation": {
13441356
"kind": "markdown",
1345-
"value": "```rescript\n#dollar$\n```\n\n```rescript\n[#dollar$ | #\"some exotic\"]\n```"
1357+
"value": "```rescript\n#dollar$\n```\n\n```rescript\n[#\"_\" | #dollar$ | #\"some exotic\"]\n```"
13461358
},
13471359
"insertText": "#dollar$",
13481360
"insertTextFormat": 2,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Syntax error!
3+
syntax_tests/data/parsing/errors/scanner/quotedUnderscore.res:1:5-8
4+
5+
1 │ let \"_" = 1
6+
2 │ let x = f(\"_")
7+
3 │
8+
9+
`_` can't be used as a quoted identifier.
10+
11+
12+
Syntax error!
13+
syntax_tests/data/parsing/errors/scanner/quotedUnderscore.res:2:11-14
14+
15+
1 │ let \"_" = 1
16+
2 │ let x = f(\"_")
17+
3 │
18+
19+
`_` can't be used as a quoted identifier.
20+
21+
let _ = 1
22+
let x [arity:1]__x = f __x
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let \"_" = 1
2+
let x = f(\"_")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let \"_'" = 2
2+
let \"_''" = 3
3+
let __' = 4
4+
let _a' = 5
5+
let f = (~\"_'") => \"_'"
6+
let a = #"_'"
7+
let b = #"_"
8+
type r = {\"_'": int}
9+
let r = {\"_'": 1}
10+
let g = r.\"_'"
11+
let x = f(a, _, c)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let \"_'" = 2
2+
let \"_''" = 3
3+
let __' = 4
4+
let _a' = 5
5+
let f = (~\"_'") => \"_'"
6+
let a = #"_'"
7+
let b = #"_"
8+
type r = {\"_'": int}
9+
let r = {\"_'": 1}
10+
let g = r.\"_'"
11+
let x = f(a, _, c)

0 commit comments

Comments
 (0)