Repository navigation
Conversation
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.
Format modified (escaped names):
Example:
let \"open" = dialog.\"open"
let render = (~\"as", ~\"open"=false) => <Dialog \"as" \"open" />
Before (rescript):
let \"open" = dialog.\"open"
let render = (~\"as", ~\"open"=false) => <Dialog \"as" \"open" />
After (rescript):
let open = dialog.open
let render = (~as, ~open=false) => <Dialog as open />
Format added (where the bare name reads as the keyword):
Example:
\"open"
Belt.x
let (rec as x) = 1
let f = x => switch x { | exception (as) => 1 }
type t = (private)
After (rescript):
\"open"
Belt.x
let (rec as x) = 1
let f = x =>
switch x {
| exception (as) => 1
}
type t = (private)
Diagnostic removed (contextual keyword as a variable):
Example:
let open = 1
Before (diagnostic):
`open` is a reserved keyword. Keywords need to be escaped: \"open"
After (diagnostic): 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.
Review: 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)`.
Review: How was the recovery checked? The editor analysis tests complete
inside unfinished expressions followed by `open M`; their snapshots are
unchanged.
Review: 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.
Review: 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`.
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1cf02-c45e-7b38-9d7d-9bb237c29854
Signed-off-by: open-session-submodule[bot] <341020755+open-session-submodule[bot]@users.noreply.github.com>
Changelog🚀 New Feature
Semantic Diff1 commit · Full page Make as, open, private and seven other keywords contextual377f429 · open-session-submodule[bot] + Jaap Frolich · Nov 16, 2026, 21:16 UTC
An escaped word, such as Incomplete code recovers as before: Format modified (escaped names) let \"open" = dialog.\"open"
let render = (~\"as", ~\"open"=false) => <Dialog \"as" \"open" />
-let \"open" = dialog.\"open"
-let render = (~\"as", ~\"open"=false) => <Dialog \"as" \"open" />
+let open = dialog.open
+let render = (~as, ~open=false) => <Dialog as open />Format added (where the bare name reads as the keyword) \"open"
Belt.x
let (rec as x) = 1
let f = x => switch x { | exception (as) => 1 }
type t = (private)
\"open"
Belt.x
let (rec as x) = 1
let f = x =>
switch x {
| exception (as) => 1
}
type t = (private)Diagnostic removed (contextual keyword as a variable) let open = 1
-`open` is a reserved keyword. Keywords need to be escaped: \"open"
+none, `open` is the variable's nameReview
|
✅ Compiler benchmarksAllocated wordsIn total: 639,071,350 → 639,062,931 (±0.00%). Generated projects: 629,051,704 → 628,990,934 (-0.01%).
InstructionsIn total: 9,225,559,445 → 9,225,880,331 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn total: 26,041,952 → 26,045,011 (+0.01%).
Pull request 377f429 compared with base 6af6ecd in this run. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## syntax-keywords-as-names #9436 +/- ##
============================================================
+ Coverage 82.25% 82.33% +0.07%
============================================================
Files 462 461 -1
Lines 65766 65849 +83
============================================================
+ Hits 54098 54214 +116
+ Misses 11668 11635 -33
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Two things I found while probing this against master:
This review was done by Claude. |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 377f429cb4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let typ_doc = | ||
| if Parsetree_viewer.typ_starts_with_lident "private" typ then | ||
| add_parens typ_doc | ||
| else typ_doc |
There was a problem hiding this comment.
Protect private manifests in type re-exports too
When a record or variant re-export has a manifest alias starting with the type name private, formatting produces invalid syntax. For example, type t = (private as 'a) = {x: int} is accepted, but the formatter emits type t = private as 'a = {x: int}: starts_private_type then interprets private as the visibility keyword because it is followed by as. This protection currently applies only to Ptype_abstract; the Ptype_record and Ptype_variant branches still print their manifests directly. Apply the same protection to those manifests so formatting preserves valid programs.
Useful? React with 👍 / 👎.
Recreated from jfrolich/rescript-rust#342 with unchanged commits, on a branch in this repository so CI and benchmark reporting can run here. The validation results below are carried over from the original PR; fresh CI will validate this stack.
Stacked on #9435.
as,constraint,exception,external,mutable,of,open,private,recandwhenbecome contextual keywords. Each is a keyword only in the position where the grammar uses it, and only when the next token is what the keyword takes. Everywhere else it is a name, so these work without an escape:open Beltstill opens a module, andopen(file)calls a function. This is the second part of the RFC in docs/rfcs/reserved-words-as-labels.md.What changes
opentakes a module name or!.externaltakes an identifier.exceptiontakes a constructor or a pattern.privatetakes a type.mutabletakes a field name.constraintand typeastake a type variable.let recandtype rectake a binding.open,externalandexceptionare always keywords there.\"open"stays a name everywhere. The scanner gives it the same token asopen, so the parser checks whether the token was written escaped before reading it as a keyword.open,exceptionorexternalfollowed on the same line by an identifier, a string or a number is treated as a misspelled declaration, soopen beltkeeps its error about the module name.open M,exception Eorexternal f, after an unfinishedx.,x->,f(~or{a:on the line before, is read as the declaration. Error recovery and editor completion therefore still see the unfinished expression.open,exceptionorexternalis printed escaped (\"open"). Parentheses would not work: aftertype t = Foo,(open)on the next line is read asFoo's arguments, the same way(x)is today.let (rec as x) = ...exception (as),exception (when)andexception (of)in a pattern.type t = (private)open,exception,externalormutablewith nothing after it is now a variable or a punned field. The error for it now comes from type checking instead of the parser.parsing/errors/typeDef/recordMutable.reskeeps its parser error by usingmutable 1.Tests
contextualKeywords*.res,errors/structure/contextualKeywordDeclarations.res). Mocha cases are inkeyword_labels_test.res.res_keyword_name_test.mlnow writes the ten words as variables next to each keyword position, bare in one spelling and escaped in the other. It also checks that each escaped word means what an ordinary name means in 31 keyword positions. Removing any of the four printer rules, or the escaped-name check, makes it fail.masterand Accept reserved words as field names, labels, variant tags and JSX names #9435, run out of tree:.res/.resifiles in the repository thatmasteraccepts give an identical-print mltree.masteraccepts give an identical tree. The same holds for a second run of 199,646 mutants against Accept reserved words as field names, labels, variant tags and JSX names #9435.masteraccepts are now errors. In each,mastersilently drops a word (private private A,${x as}, a strayofafter a variant spread), and its formatter deletes it.masterwith an ordinary name in place of the keyword, so these are existing formatter bugs.-recover, except in the debug-print mlprinter and the JSX transform's error reporting.masterreaches both the same way.master, and formatting them within 0.07%.These all pass on this branch:
make test-syntaxand its roundtrip mode,dune build @fmt, Mocha, the editor analysis tests (unchanged snapshots),super_errors, the build tests,make dead-code, the changelog check, and therescript-syntaxbuild with OCaml 4.14.Not included: making
typecontextual, which is a follow-up.Started by Jaap Frolich in this Assistant session