Skip to content

effects: let declarations snapshot before writes - #301

Merged
vito merged 2 commits into
mainfrom
declaration-snapshots
Nov 12, 2026
Merged

vito merged 2 commits into
mainfrom
declaration-snapshots

Conversation

@vito

@vito vito commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Rule 2 of top-level initialization (toplevel_order.go) rejected every read of statement-written state (W) from a declaration's initializer, wherever it sat. So this snapshot failed to check:

let before = ws
ws = ws.withNewFile("a.txt", "hi")
ws = ws.withNewFile("b.txt", "there")
[before, ws]

It failed in scripts and in Session/Module.Eval snippets alike, and so in the engine's DangEval. In both settings before would get the value from before the writes.

The rule now

An initializer may read a binding of W if both of these hold:

  • The read is direct: it is under the effect frame the initializer started in, not inside a block argument, a {{ }} field, or a function or type body written in it. Bare { } blocks and if branches run in place, so reads there still count as direct.
  • It comes before every write of the binding, and all of those writes are in its own file. Across the files of a directory module there is no source order to go by.

Scripts run every declaration before any statement. Sessions run forms in source order. Initializers can't write top-level bindings (rule 1), so both see the pre-write value at such a read. What a session accepts, a script still accepts, and both produce the same values.

Every other read is still rejected with the same message: reads after a write, reads between writes, indirect reads, and reads where another file writes the binding. The message now cites the first write the read doesn't come before.

The change only accepts more programs, so there is no version gate.

Changes

  • pkg/dang/toplevel_order.go:
    • topForm.frame records the frame an initializer starts in.
    • outsideRead.direct marks reads made under that frame.
    • topOrder.written keeps every write of each binding.
    • check skips a direct read that comes before all the writes (firstWriteNotAfter).
  • Tests:
    • The "W is a set" case in TestTopLevelStatementState is replaced by cases for the snapshot (including one on the same line as a later write), a read between writes, a read after a write, and reads in a block argument, a {{ }} field, a function, a nested function and a field default.
    • TestRunDirSnapshotAcrossFiles covers a write in the same file (values checked) and a write in another file (rejected).
    • TestSnapshotBeforeWritesInScriptsAndSessions runs the same program as an eager module top level and as a Module.Eval snippet (both give [1, 2]), snapshots a module binding that a snippet reassigns, and checks that a read after a write is rejected in both.
    • tests/test_toplevel_init.dang gains a snapshot.
  • Docs: docs/lit/types/mutation.md (with a working and a dang-failure snippet), the dang-language skill and reference/objects.md, and the dang-internals skill.

go test ./pkg/dang ./tests, docs and fmt/stale all pass.

vito added 2 commits November 12, 2026 03:53
Rule 2 rejected every read of statement-written state (W) from a
declaration's initializer, wherever it sat, so a snapshot like

    let before = ws
    ws = ws.withNewFile("a.txt", "hi")
    [before, ws]

failed to check, in scripts and in Session snippets alike, though both
give `before` the value from before the writes.

An initializer's direct read of a binding of W (not in a block argument,
`{{ }}` field, or function or type body written in it) is now allowed
when it comes before every write of the binding, all in its own file.
Scripts run declarations before statements and sessions run forms in
source order, and both see the pre-write value there, since initializers
write no top-level binding, so what each accepts still agrees. Reads
after a write, indirect reads, and reads with a write in another file of
the module are still rejected with the same message, which now cites
the first write the read doesn't precede.

Only accepts more programs, so no version gate.

Signed-off-by: Alex Suraci <[email protected]>
The mutation page, the dang-language skill and its objects reference,
and the dang-internals skill now say a declaration may read
statement-written state directly before every write in its file, with a
working and a failing snippet on the mutation page.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: b52cf3c
Status: ✅  Deploy successful!
Preview URL: https://ac684456.dang-3kk.pages.dev
Branch Preview URL: https://declaration-snapshots.dang-3kk.pages.dev

View logs

@vito
vito merged commit 11332a9 into main Nov 12, 2026
9 checks passed
vito added a commit to dagger/dagger that referenced this pull request Nov 12, 2026
vito/dang main past v2.4.1: analysis's opt-in diagnostic context and
Render (vito/dang#298), and rule-2 snapshot reads, so `let before = ws`
stays readable after `ws = …` (vito/dang#301). No language version
changes: Dang's current version is still 2.4.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 12, 2026
The 2.5 page covered only the string change. It now covers everything
merged since v2.4.1: declaration snapshots (#301), the editor and
syntax-error changes from #300, and a Go embedders section with
DiagnosticContext (#298) and the AST's new string spelling. Sources and
verification name the full range.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 13, 2026
The 2.5 page covered only the string change. It now covers everything
merged since v2.4.1: declaration snapshots (#301), the editor and
syntax-error changes from #300, and a Go embedders section with
DiagnosticContext (#298) and the AST's new string spelling. Sources and
verification name the full range.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 14, 2026
vito/dang main past v2.4.1: analysis's opt-in diagnostic context and
Render (vito/dang#298), and rule-2 snapshot reads, so `let before = ws`
stays readable after `ws = …` (vito/dang#301). No language version
changes: Dang's current version is still 2.4.

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