Semantic Diff

#9435 Accept reserved words as field names, labels, variant tags and JSX names

3 commits

895cf4cfede60ea51cedbb21f86c4e72b166d027..pr-9435

b0ac12f1a · open-session-submodule[bot] + Jaap Frolich · Nov 16, 2026, 20:50 UTC

Accept reserved words as field names, labels, variant tags and JSX names

Why

A record field, argument label or JSX prop named like a keyword (type, open, in, for) has to be renamed and mapped back with @as, or escaped as \"type". Keys such as type are common in JavaScript objects; the standard library itself declares @as("type") type_ in JsxDOM.

Where the grammar expects a name that no variable is bound to, a reserved word is now that name: a record field (in a type, an expression, a pattern and after the . of a field access), an argument label after ~, a JSX prop, a polymorphic variant tag after # and a lowercase JSX tag name. Each of these positions is entered on a token that is not a keyword, so the reading is unambiguous; the one choice, { starting a record or a block, is made by a reserved word followed by :, which no block item starts with. The parse tree is the one the escaped spelling gives, so type checking and the generated JavaScript do not change.

A shorthand ({type}, ~type, <input type />) stands for a variable of the same name, and a reserved word still can't be one. After the . of a field access or the ~ of a label a reserved word is taken only on the same line, so user. or f(~ at the end of a line being written does not swallow the next let, and editor completion still sees the unfinished argument. let?, one token, is split into the name let and ? where a name is expected ({let?: int}, x == #let? a : b).

modifiedFormat escaped names

type t = {\"type": string, mutable \"open": bool}let get = r => r.\"type"let call = f(~\"for"="a")let tag = #"true"let el = <\"switch" />
rescript
Before
After
type t = {\"type": string, mutable \"open": bool}let get = r => r.\"type"let call = f(~\"for"="a")let tag = #"true"let el = <\"switch" />type t = {type: string, mutable open: bool}let get = r => r.typelet call = f(~for="a")let tag = #truelet el = <switch />

removedDiagnostic keyword as a field name

let r = {type: 1}
diagnostic
Before
After
Cannot use keyword `type` as a record field name. Suggestion: rename it (e.g. `type_`)none, `type` is the field's name

addedDiagnostic shorthand with a reserved word

let {type} = r
diagnostic
After
`type` is a reserved keyword, so it can't be a variable name and this shorthand can't be used.Write it out instead: `{type: pattern}`

Review

  • Can a program that parses today change meaning? Every accepted form was a syntax error before. All 2,948 .res/.resi files in the repository that the previous parser accepts give the same parse tree (-print ml), and so do the 33,367 of 199,692 corpus mutants (keywords inserted, removed, escaped and unescaped, line breaks added) that it accepts. None crashes or hangs, with or without -recover, beyond the debug -print ml printer and the JSX transform's errors, which the previous parser reaches the same way. Formatting every accepted mutant gives the same tree again; the few that don't fail the same way with an ordinary name in place of the keyword.
  • Why do runtime and test sources change? They used escaped labels and tags (\"type": ..., #"true"), which the formatter now prints bare. The runtime and tests/tests compile to the same JavaScript.
  • Why do analysis snapshots change? Hover and completion print the types with bare tags ([#and | #or], #switch).
  • Does parsing allocate more? The benchmark sources (tests/compiler_benchmarks/data) allocate within 0.03% of the previous parser: the new checks use one-token lookahead, no backtracking, and no closures on the field, label and JSX prop paths.
  • What checks the two spellings agree? tests/syntax_tests generates 2,000 seeded programs in both spellings and checks that they parse, give the same tree, format identically and idempotently, and that formatting adds no escape; every shorthand with each of the 28 words, and every record row with one of them after a missing comma ({a type: x}), must be rejected.
412e65b8d · open-session-submodule[bot] + Jaap Frolich · Nov 25, 2026, 20:46 UTC

Don't crash on a name missing at the end of a file

Why

type t = int as ' at the end of a file stopped the parser with an assertion instead of a syntax error, in the compiler and the editor.

parse_ident reported the missing name and then skipped the token in its place with Parser.next, which asserts at the end of the file. It now uses Parser.next_unsafe, as the other error paths do.

addedDiagnostic missing type variable name at the end of a file

type t = int as '
diagnostic
Before
After
none, the parser stops with an assertionA type param consists of a singlequote followed by a name like `'a` or `'A`

Review

  • How was it found? Mutation fuzzing of the parser; the input crashes the parser before this branch too.
6af6ecd79 · open-session-submodule[bot] + Jaap Frolich · Nov 25, 2026, 20:47 UTC

Keep line numbers right after a quote before a line break

Why

A ' directly followed by a line break moved every later location one line down, so syntax errors pointed at the wrong line; at the end of a file, printing the error crashed.

The scanner reads ' as a codepoint literal when a character and a closing ' follow. When they don't, it went back to the character after the quote, restoring the offset but not the line: the line break it had stepped over stayed counted. It now restores the line number and the line's start too.

modifiedDiagnostic location after a lone quote

let x = y 'let z = 1let w = )
diagnostic
Before
After
ln.res:4:8-9 This let-binding misses an expressionln.res:3:8-9 This let-binding misses an expression

Review

  • Why does aliasAtEndOfFile.res.txt change? Its error was reported on line 3 of a two-line file; it is now on line 2.