Skip to content

Version-gate reserved words; let modules shadow builtins - #212

Merged
vito merged 10 commits into
mainfrom
versioned-language
Oct 29, 2026
Merged

vito merged 10 commits into
mainfrom
versioned-language

Conversation

@vito

@vito vito commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Dagger is deleting its separate vito/dang v1.0.1 library and running old modules on Dang v2 in dang = "1.0" mode. A parity run found two places where 1.0 mode didn't behave like v1.0.1. This PR fixes both, and turns the rescue case into a general mechanism for version-gated keywords.

1. Builtins were not shadowable, in any version

When a directory is evaluated as a module (RunDir, DeclareDir, an imported Dang module), a top-level declaration named like a builtin or a prelude binding was silently dropped at evaluation. The type checker still used it:

let loop = 1
assert { loop == 1 }                  # left: builtin:loop

pub loop(n: Int!): Int! { n }
loop(2)                               # too many positional arguments: got 1, expected at most 0

Cause. NewValueScope bound the builtins into the module's own frame. FieldDecl.Eval doesn't publish a name its frame already binds; that check exists for values a constructor provided. The same bug hit every builtin (let toString = ... too), and single-file RunFile was unaffected.

Fix. The builtins and the prelude now live in a parent frame. Copy-on-write treats that frame as a root, so method calls on modules stay as cheap as before. Unqualified imports still never shadow builtins, in both inference and evaluation, for RunDir and RunFile; the qualified name still reaches the import.

2. Version-gated keywords: one grammar, version predicates

rescue was reserved in every version, so let rescue = 1, pub rescue: Int! and f(rescue: Int!) failed in 1.0 mode. The design (agreed with vito) keeps one PEG grammar with version predicates, the mechanism the other version gates already use, rather than one .peg per version:

  • reservedWords (version.go) records the version that reserved each word: null 1.0, rescue 2.1 (first tagged in v2.1.2).

  • A gated keyword's token only matches where the word is reserved: RescueToken <- "rescue" !WordChar RescueKeyword. That one gate covers both the word's exclusion from identifiers and the syntax built on it.

    • So before 2.1 there is no postfix rescue either. Without that, let y = foo followed by a rescue(1) line would silently parse as foo rescue (1).
  • treesitter.go resolves the predicate for the current version, as it does for the other gates.

  • Adding a keyword in a future version takes four edits:

    1. a table entry in reservedWords;
    2. a gated token;
    3. !FooToken in WordToken;
    4. the token listed in ReservedName.

    TestReservedWords then covers the new word in every version. The language-versions skill documents this.

try/catch before 2.1 is the error handling syntax again:

  • It doesn't warn, and dang fmt keeps it.
  • It keeps its own rules in every version: a catch with no clauses re-raises, and clauses that can't match are dead code, not errors.
  • From 2.1 on, dang fmt and dang migrate rewrite it to postfix rescue through modernizeTryCatch.
  • Migration keeps the try/catch where rescue can't express it without loss: unreachable clauses, or comments in an empty catch. It still runs in 2.1, with a warning.

Raw identifier escape: $name (please bikeshed)

$name is the identifier name, in every version, so dang migrate can keep names that a newer version reserves, public ones included:

pub $rescue: Int! = 1        # a field named `rescue`
let y = thing.rescue         # selected as usual
  • Only the spelling changes: the AST name, and therefore Dagger API names, field access and the formatter's round-trip, all stay rescue.
  • The formatter writes the escape wherever it's needed.
  • $ is free in Dang's lexer outside ${ interpolations. Backticks are taken, and Rust's r#rescue doesn't work because # starts a comment.
  • A reserved word is a plain name where only a name can go, so the escape is only needed for declarations and bare references:
    • after a dot: x.rescue
    • in a selection: x.{{rescue}}
    • as a named argument: f(rescue: 1)
    • as an object or object-type key
  • null is a value, so it stays escaped ($null) even in those name positions.

3. for loops can't be captured by a user's loop

Before 2.1, for loops desugar to a loop call. Now that user bindings shadow builtins, a module defining its own loop would capture them.

  • They now call the builtin by hidden aliases, $for and $for(cond). No identifier can spell these, and they are left out of completions.
  • The formatter prints them back as for where the version has for-loops, and as loop in 2.1+.
  • dang migrate refuses only a genuine collision (forLoopCollision): a for loop with a user loop in scope, including a top-level loop in a sibling file of the same module.

Tests

  • tests/builtin_shadowing_test.go: let, functions, sibling files, imported modules and DeclareDir, in 1.0, 2.0 and 2.1. Also for loops next to a user loop, and imports not shadowing builtins (RunDir and RunFile). The shadowing subtests fail without the fix.
  • pkg/dang/version_test.go:
    • TestVersionRescue: 1.0 and 2.0 accept rescue as an identifier, try/catch formatting and migration.
    • TestReservedWords: table-driven, every reserved word × every version.
    • TestKeywordNamesRoundTrip: every keyword as a declared and referenced name, round-tripped through the formatter.
    • The for-loop collision table.
  • Migration fixtures: try_catch.dang and rescue_identifier.dang under 2.0-to-2.1/. case_unmatched_raises.dang moved to try/catch, since 2.0 never had postfix rescue.
  • A tree-sitter corpus test for $rescue and reserved names, and an LSP range test.
  • go test ./pkg/... ./cmd/... ./tests/ passes. So do the test/treesitter, fmt/stale and stale checks for pkg/dang and treesitter.

Notes

  • The tree-sitter grammar is regenerated, because the current syntax changed ($name, reserved names). Highlight queries live in the editors/ submodules and weren't touched.
  • Known limit: a 2.0 try/catch whose dead clause is only detectable through types (a subtype after an interface pattern) migrates to a rescue that 2.1 rejects. Fixing that needs type information; the error surfaces at compile time.
  • Pre-existing and unchanged: ValueScopeFromSchema binds builtins over a schema's same-named root fields, inside the import's own scope.

vito added 7 commits October 29, 2026 13:48
In a directory evaluated as a module (RunDir, DeclareDir, an imported Dang
module), a top-level declaration named like a builtin or a prelude binding,
e.g. `let loop = 1` or `pub toString(...)`, was silently dropped at
evaluation, while the type checker used it: NewValueScope bound the builtins
into the module's own frame, and FieldDecl.Eval does not publish a name its
frame already binds (that check is for values a constructor provided). So
`loop` stayed the builtin and calling the user's `loop(2)` failed with "too
many positional arguments". Single-file RunFile was unaffected, since its
file block evaluates in a derived scope.

The builtins and prelude now live in a parent frame of the module's scope, so
the module's own declarations shadow them like any outer binding, in every
Dang version. Lookups and Bindings see the same names as before.

Signed-off-by: Alex Suraci <[email protected]>
`rescue` was reserved in every version, though Dang 2.1 introduced it: `let
rescue = 1` or `f(rescue: Int!)` stopped parsing in 1.0 and 2.0 modules,
which have no postfix rescue. Reserved words now have a table,
reservedWords, recording the version that reserved each, and a version-gated
keyword's token only matches where it is reserved. That one gate covers both
the word's exclusion from identifiers and the syntax built on it: before 2.1,
`rescue` is an identifier and there is no postfix rescue, which also keeps
`let y = foo` followed by a `rescue(1)` line meaning two forms, as it did.

Before 2.1, try/catch is then the error handling syntax again: it no longer
warns there (bare catch-alls included), and the formatter keeps it. From 2.1
on it still parses as a deprecated Legacy node, and the formatter rewrites it
to the postfix form, so migrating to 2.1 rewrites try/catch. The rewrite is
now an AST transformation, modernizeTryCatch, shared with the migration's
meaning check, which compares a try/catch as its postfix equivalent.

Signed-off-by: Alex Suraci <[email protected]>
Migrating to a version that reserves a word could not keep the names
spelled like it, public ones included (`pub rescue: Int!`), since renaming
changes what they mean. `$name` is now the identifier `name` in every
version, and the formatter writes it for an identifier the version it
formats reserves, so `dang migrate` escapes such names losslessly: only the
spelling differs, and `x.rescue` still selects the field `pub $rescue`
declares.

Where only a name can go, a reserved word is a name, so the escape is only
needed for declarations and bare references: after a dot (as before), in a
selection (`x.{{rescue}}`), as a named argument (`f(rescue: 1)`), and as an
object or object type key. null, a value, stays escaped there too.

TestReservedWords checks each reserved word in every version: where it is
an identifier, where it is a name, that `$word` always is one, and how the
formatter spells it. The tree-sitter grammar gains the escape and the
reserved names, hence its regeneration.

Signed-off-by: Alex Suraci <[email protected]>
Now that a module's own declarations shadow builtins, a Dang 1.0 or 2.0
module binding its own `loop` (legal there) would have its `for` loops call
that `loop`, since they desugar to a call of the builtin by name. They now
call it by an alias, `$loop`, which no identifier spells (`$loop` is the
identifier `loop`), so nothing shadows it. It is left out of completions,
and the formatter prints it as `loop`, as before.

That spelling is the one place the shadowing still shows: migrating such a
`for` loop writes a `loop` call, which the module's own `loop` would capture
where it is in scope. Migration refuses that case, naming the line, rather
than change what the loop does; scope is approximated by the enclosing
blocks, functions, block params and clause bindings, plus the top level of
the module's other files.

Also pins that an unqualified import never shadows a builtin (qualified, it
is still reachable).

Signed-off-by: Alex Suraci <[email protected]>
Fixes from reviewing the branch:

- Before 2.1, `dang fmt` printed a `for` loop as a `loop` call, which a
  module's own `loop` captures, so formatting broke a module that ran. `for
  {}` and `for (cond) {}` now desugar to two aliases of the builtin, and
  print back as the `for` loops they were wherever those exist; only
  migrating to 2.1 writes `loop`, which forLoopCollision guards.
- forLoopCollision also checks constructor parameters.
- The formatter dropped the escape from `$true`, `$self`, `$break`, `$if`,
  `$pub` and the like, whose bare spelling parses as something else where a
  name can go; it keeps it now. TestKeywordNamesRoundTrip checks every
  keyword's word as a declared and referenced name in every version.
- In a single-file run, an unqualified import could now shadow a builtin at
  evaluation while inference kept the builtin; installing an import skips
  builtin names explicitly, as the builtins no longer sit in the frame it
  checks.
- The dang-language skill describes the versioned try/catch and `$rescue`.

Signed-off-by: Alex Suraci <[email protected]>
Fixes from a second review:

- Before 2.1, try/catch is the version's own syntax, so it keeps its rules:
  a catch without clauses re-raises, and clauses after a catch-all are dead
  code rather than errors (the errors pointed at postfix rescue, which those
  versions can't parse). Migrating to 2.1 drops both.
- Unqualified imports now skip builtin names in inference too, so a
  single-file run types such a name as the builtin it evaluates to.
- The frame of builtins under a module's scope no longer counts as a
  generation of it: a method call on a module derived from it again instead
  of copying the whole module scope, as before builtins got their own frame.
- forLoopCollision checks object literal fields, which are in scope for
  the fields after them.
- The formatter keeps the escape on `$return`, `$raise` and `$else`, and the
  error docs describe the versioned try/catch.

Signed-off-by: Alex Suraci <[email protected]>
Fixes from a third review. Migrating a 2.0 try/catch to postfix rescue
could lose comments, or produce a file that no longer compiles:

- A catch with clauses that can't match (after a catch-all, or repeating a
  type) stays a try/catch, which rescue would reject; one without clauses
  but with comments stays too. A try/catch keeps its own rules in 2.1,
  where it still runs with a warning, so the result compiles.
- A try body with comments stays a block, and a commented catch-all stays
  a clause rather than becoming the fallback form; migration compares such
  a clause as the fallback form.
- Comments before a catch's closing brace stay inside it.
- The LSP's ranges for an escaped `$rescue` cover the name itself, so a
  rename keeps the escape.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito marked this pull request as ready for review October 29, 2026 15:08
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8bec442
Status: ✅  Deploy successful!
Preview URL: https://f191c5f8.dang-3kk.pages.dev
Branch Preview URL: https://versioned-language.dang-3kk.pages.dev

View logs

vito added 3 commits October 29, 2026 17:44
The PR's patterns will recur, so give each a checklist: adding a keyword
(reserved or not, incl. the TestKeywordNamesRoundTrip list, the feature
method and tsPredicates), spelling names through Id/ReservedName and
f.ident/f.fieldName, desugaring to builtins via $-aliases, per-version rules
as AST flags, the builtins parent frame, and the unqualified-import hazard of
new globals.

Signed-off-by: Alex Suraci <[email protected]>
The authoring skill had picked up implementation details and change history
with each incremental change: reserved-word escaping rules, legacy try/catch
behaviour per version, dang.toml version parsing minutiae, Dagger prerelease
handling, AST node names, prelude file paths. Cut those down to what an
author needs, folded three copies of the equality rules into one, and fixed
a comma-separated example that 2.1 rejects.

Added the two author-facing points from this branch: `$name` names
something like a keyword, and your own declarations shadow builtins while
unqualified imports don't.

Signed-off-by: Alex Suraci <[email protected]>
engineVersion is going away; dang.toml's `dagger` key is the source of truth
for the Dagger API version.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 3ee188d into main Oct 29, 2026
9 checks passed
vito added a commit to dagger/dagger that referenced this pull request Oct 29, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Oct 29, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 2, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
sipsma added a commit to grouville/dagger that referenced this pull request Nov 3, 2026
Dang moves from the merge of vito/dang#205 to the merge of vito/dang#213,
which adds ParseSourceFile. In between are the v2.2.0 and v2.3.0 releases
and vito/dang#214, which change what Dang modules see:

- The dang.toml that governs a module's source, found by walking up to the
  repository root, now sets the Dang version its source parses and
  evaluates as. A module with none keeps the newest version. A malformed
  dang.toml, which was ignored before, now fails the module's load.
- A module's own top-level declarations shadow builtins of the same name
  (vito/dang#212).
- JSON, YAML and TOML decode into Map[T] (vito/dang#214).
- The Dang language skill the engine serves describes these.

The engine still chooses a module's Dagger API version itself; the
Dagger version a dang.toml can declare doesn't change it.

Signed-off-by: Erik Sipsma <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 4, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 4, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 4, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 5, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 5, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 5, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 5, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 5, 2026
Dang v2.3.0 (vito/dang#212) reserves `rescue` only from Dang 2.1 and lets a
module's own declarations shadow builtins in every version, so a Dang 1.0
module that names something `rescue` or defines its own `loop` runs as it did
on the Dang v1 line. That closes the two known gaps of Dang 1.0 mode; the
README and changelog no longer list them.

Its language versions are unchanged (CurrentVersion and DefaultVersion stay
2.1), and no engine call site changed. Unit and integration tests cover a Dang
1.0 module using both names.

Signed-off-by: Alex Suraci <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant