Semantic Diff

#9436 Make as, open, private and seven other keywords contextual

1 commit

6af6ecd79f9ab12bc2e731e11bfc386f6657f2c4..pr-9436

377f429cb · open-session-submodule[bot] + Jaap Frolich · Nov 16, 2026, 21:16 UTC

Make as, open, private and seven other keywords contextual

Why

open, private, as, exception and of are common JavaScript names (a dialog's open, a component's as prop, an exception callback parameter), but as reserved words they can't be variables, so {...dialog, open}, (~as) => as and exception => log(exception) need an escape or a rename.

as, constraint, exception, external, mutable, of, open, private, rec and when are now scanned as identifiers. Each is a keyword only in the position where the grammar uses it, and there only when what follows is what the keyword takes: open Belt opens a module, open(file) calls a function. One token of lookahead decides, because what follows the keyword can't follow a variable: open takes a module name or !, external an identifier, exception a constructor or a pattern, private a type, mutable a field name, constraint and type as a type variable, let rec/type rec a binding. Everywhere else the word is a name, so it can be a variable, a type name, a type variable and a shorthand. A signature has no expressions, so open, external and exception are always keywords there.

An escaped word, such as \"open", stays a name everywhere. The scanner gives it the same token as open, so the parser checks the token's first source character before reading the word as a keyword; every program that parses today keeps its meaning. open, exception or external followed on the same line by an identifier, a string or a number is a misspelled declaration, since no variable is followed by one, so open belt keeps its error about the module name.

Incomplete code recovers as before: open M, exception E or external f starting a line, after an unfinished x., x->, f(~ or {a: on the line before, is the declaration, as it was when the word was reserved, so error recovery and editor completion still see the unfinished expression. A variable at the start of a line followed by a module name on the same line is never valid code, so this changes nothing that parses.

modifiedFormat escaped names

let \"open" = dialog.\"open"let render = (~\"as", ~\"open"=false) => <Dialog \"as" \"open" />
rescript
Before
After
let \"open" = dialog.\"open"let render = (~\"as", ~\"open"=false) => <Dialog \"as" \"open" />let open = dialog.openlet render = (~as, ~open=false) => <Dialog as open />

addedFormat where the bare name reads as the keyword

\"open"Belt.xlet (rec as x) = 1let f = x => switch x { | exception (as) => 1 }type t = (private)
rescript
After
\"open"Belt.xlet (rec as x) = 1let f = x =>  switch x {  | exception (as) => 1  }type t = (private)

removedDiagnostic contextual keyword as a variable

let open = 1
diagnostic
Before
After
`open` is a reserved keyword. Keywords need to be escaped: \"open"none, `open` is the variable's name

Review

  • Can a program that parses today change meaning? No. All 2,948 .res/.resi files in the repository that the parser before this branch accepts give the same parse tree (-print ml), and so do the 33,587 of 199,671 corpus mutants (keywords inserted, removed, escaped and unescaped, line breaks added) that it accepts. The property test checks that each of the ten words, escaped, means what an ordinary name means in 31 keyword positions. Part 2 rejects 8 mutants the previous parser accepted; each drops a keyword without an error there (private private A, ${x as}), and its formatter deleted the word.
  • Why is a statement open printed escaped and not in parentheses? After a variant declaration, (open) on the next line is read as the constructor's arguments, as (x) is today: type t = Foo then (open) is Foo(open).
  • How was the recovery checked? The editor analysis tests complete inside unfinished expressions followed by open M; their snapshots are unchanged.
  • What checks the printer? The seeded property test now writes the ten words as variables (bare in one spelling, escaped in the other) next to each keyword position, and checks every shorthand. Removing any one of the four rules makes it fail. Formatting every mutant this branch accepts gives the same tree again; the few that don't fail the same way with an ordinary name in place of the keyword, before this branch too.
  • Why does recordMutable.res change? {foo: string, mutable} is now a punned field named mutable, like any other name; the fixture keeps the error with mutable 1.