diff --git a/docs/agent/README.md b/docs/agent/README.md index 2d39033..bfddc08 100644 --- a/docs/agent/README.md +++ b/docs/agent/README.md @@ -150,3 +150,5 @@ DD-017 stable E7006 reasons (cl100k): Quick Reference 8,769 → 8,807 (+38), oth RFC 0037 related positions (cl100k): Quick Reference 8,807 → 8,859 (+52), other source docs 65,799 → 66,195 (+396), full source 74,606 → 75,054 (+0.60%). o200k groups are 8,895 / 66,397 / 75,292; Qwen2.5 9,135 / 67,810 / 76,945. AIR grows 46,937 → 47,328 (+391, +0.83%), 182,254 → 184,060 bytes; library/std.md is unchanged. Two factual roles locate an existing selected conflict or spawn; there is no new programming syntax, diagnostic code, semantic verifier state or runtime rule. One constant-sized origin is carried on each existing task loan. Necessary schema/source-map rules enter the bundles; RFC rationale does not. No empirical MCC benefit is claimed. DD-019 unique diagnostic fields (cl100k): Quick Reference stays 8,859; other source docs 66,195 → 66,312 (+117), full source 75,054 → 75,171 (+0.16%). o200k groups are 8,895 / 66,517 / 75,412; Qwen2.5 9,135 / 67,933 / 77,068. AIR grows 47,328 → 47,445 (+117, +0.25%), 184,060 → 184,628 bytes; library/std.md is unchanged. Explicit payload names preserve the stable envelope and both guard/borrow identities. No language concept, syntax, code, verifier/runtime state or repair application is added; clients that previously read a capability payload as `kind` use `capability_kind`. No empirical MCC benefit is claimed. + +Q-041 source scope names (DD-020, cl100k): Quick Reference 8,859 → 8,889 (+30), other source docs 66,312 → 66,484 (+172), full source 75,171 → 75,373 (+0.27%). o200k groups are 8,925 / 66,690 / 75,615; Qwen2.5 9,167 / 68,115 / 77,282. AIR remains 184,628 bytes / 47,445 cl100k tokens; library/std.md is unchanged. One existing lexical block identifier is explicitly excluded from ordinary value expressions, removing ineffective transfers and misleading repair hints. There is no new syntax, diagnostic code/reason, verifier/runtime state or feature. Former source aliases now reject; ordinary shadowing and AIR scope flow remain available. No empirical MCC benefit is claimed. diff --git a/docs/agent/quick-reference.md b/docs/agent/quick-reference.md index 6957c6d..7c566b5 100644 --- a/docs/agent/quick-reference.md +++ b/docs/agent/quick-reference.md @@ -221,6 +221,7 @@ scope s { // every task spawned in s must ``` - **Spawning:** + - a scope name is only the first operand of `task.spawn`; no aliases, `move` or ordinary value use (`E7006`); - effect `task.spawn`; - a value moved into a task must be `Send`, and data shared by `ref` must be `Share` (`E4001`, `E4002`); - two live tasks may not hold overlapping `mut` loans (`E4101`); diff --git a/docs/design/decisions.md b/docs/design/decisions.md index ef06b71..b93b595 100644 --- a/docs/design/decisions.md +++ b/docs/design/decisions.md @@ -113,3 +113,13 @@ The invariant is unchanged acceptance, ownership/loan verification and determini Compatibility review found legacy payload collisions: unsupported AIR narrowing and unknown source capability kinds each emitted a second `kind`, and unlock with a live guard borrow emitted `borrow` twice. Ordinary JSON consumers lost the stable machine kind or one identity. Spec 10 §2 requires unique names and reserves envelope fields. Capability payloads use `capability_kind`; unlock reports the selected `borrow` and its `guard` separately, retaining the borrow seen by former last-key-wins readers. Clients must read the capability payload by its new name; stable diagnostic kinds are restored rather than redefined. These are diagnostic corrections, not new source/AIR or ownership/concurrency rules. Existing rejection codes, positions, conflict selection, guard/loan state and deterministic execution remain unchanged; valid capabilities and guard programs remain accepted. Pipeline/CLI tests reproduce the three cases, the verifier unit helper checks payload uniqueness across its existing rejection tests, and conformance validates every final mapped diagnostic's fields and serialized JSON. The invariant is an unambiguous machine diagnostic without hidden loss of context. Budget growth is recorded in docs/agent/README.md; no repair candidate or measured MCC benefit is introduced. + +## DD-020 — Source scope names are lexical identifiers + +Spec 15 §7 resolves Q-041: the name introduced by `scope s` is only the named first operand of `task.spawn`, not an ordinary source value. Lowering rejects plain/moved aliases and value expressions before suggesting a transfer. Ordinary lexical shadowing remains legal; block completion retains its original SSA identity independently of source lookup. This removes an apparent ownership transfer that formerly neither consumed nor changed completion of the handle. + +The invariant is one retained completion obligation per source scope, with every child joined and every loan released before completion. Existing AIR verification, including E4301/E4302 and function-local scope flow (spec 04 §33), remains authoritative and unchanged. No runtime rule, instruction, feature, syntax, verifier state or interpreter behavior changes. Shield handles and lock guards already have no source value names; task values remain ordinary linear values. + +Valid: `scope s { let t = task.spawn(s, work); let s = 0u64; let n = task.join(move t); }`. Rejected: `scope s { let alias = move s; }` (E7006, existing reason `invalid_expression`, fields `name`/`type: scope`); plain aliases, yields, returns, aggregates and ordinary arguments are rejected likewise. Borrowing keeps `invalid_place`, assigning the immutable identifier keeps `invalid_assignment`, and a moved/non-scope spawn operand keeps E7005. The existing structured reason plus type identifies the category without reading prose; no additional reason is needed. + +Compatibility: formerly accepted scope aliases, spawning through an alias or the apparently moved original, standalone scope names and block/scope tails now reject. Accepted shadowing, nested scope lookup, cleanup at fall-through/return/break/continue, and all AIR format-0 programs retain their meanings. Public source conformance covers accepted/rejected forms, specialized bodies and live children; CLI tests cover exact reasons/locations, packages and deterministic/hosted execution. Scheduler stress and per-instruction collection check cleanup. Source documentation growth is measured in docs/agent/README.md. The change improves explicitness and repairability without an empirical MCC claim; it does not finish P6-2. diff --git a/docs/design/open-questions.md b/docs/design/open-questions.md index 2810a3f..d2b4c32 100644 --- a/docs/design/open-questions.md +++ b/docs/design/open-questions.md @@ -33,7 +33,7 @@ Status values: `open`, `resolved`. | Q-036 | resolved | STD-6a | Dynamic initialized linear buffer storage and drain | | Q-038 | resolved | STD-6d.2 | Pure construction of built-in errors | | Q-039 | resolved | STD-6d | Explicit static function parameters | -| Q-041 | open | P6-2 task block forms | Source scope-handle value bindings | +| Q-041 | resolved | P6-2 task block forms | Source scope-handle value bindings | ## Q-001 — Text AIR grammar @@ -304,3 +304,5 @@ P6-8 review found an existing parser ambiguity: `if true { *p = 2u64; } *p = *p Independent E7006 review found that `module scope_move fn main()->u64 effects [] { scope sc { let alias = move sc; } 42u64 }` checks and returns 42 with fuel 10,000. Its AIR contains only `task.scope.begin` and `task.scope.end` on the same SSA value; the unused alias introduces no instruction. This observation alone establishes no memory/concurrency safety defect. Spec 15 §3 excludes `scope` from source type spellings, §7 ties scope completion to the block, and spec 04 §33 permits function-local scope flow while preserving linear completion. They do not explicitly say which ordinary source value-binding forms may name that internal handle or how an explicit transfer interacts with the block's retained completion obligation. Clarify whether `let`/`move` aliases and rebinding of a scope name are permitted, how their availability relates to the originating block, and which existing diagnostic applies to rejected forms. Keep AIR scope identity, live-child/loan checks, deterministic completion and the no-escape invariant intact; add source acceptance/rejection tests for the chosen contract before changing lowering. This is separate from DD-017's reason field and RFC 0038's proposed repairs; neither adopts a new scope rule. + +**Resolved (DD-020, spec 15 §7):** a source scope name is a lexical identifier usable only as the named first operand of `task.spawn`. Ordinary `s`/`move s` value uses reject E7006 `invalid_expression`, with `name` and `type: scope`; borrowing remains E7006 `invalid_place`. Ordinary shadowing remains legal and never replaces the originating block's saved completion identity. Source/AIR tests retain existing live-child and loan rules, deterministic completion and function-local AIR scope flow. Previously accepted aliases and scope-valued expressions now reject; no new syntax, reason, code or verifier/runtime state is added. This resolves one P6-2 question, not the phase. diff --git a/docs/plan/phase-6-source-language.md b/docs/plan/phase-6-source-language.md index 3a272b9..aef6f7e 100644 --- a/docs/plan/phase-6-source-language.md +++ b/docs/plan/phase-6-source-language.md @@ -182,3 +182,9 @@ Validation: unit tests and full conformance pass, 1,160/1,160 with no failures o DD-019/spec 10 §2 fixes three legacy duplicate-field emissions discovered during compatibility review: E3202 unsupported narrowing and E7002 unknown source capability kinds use `capability_kind`, preserving the stable envelope `kind`; E2102 unlock with a live child borrow reports `borrow` and `guard` separately. Consumers previously reading a capability payload from the duplicate `kind` must use `capability_kind`; former last-key-wins readers retain the selected E2102 borrow identity. Codes, rejection/transfer rules, primary positions, messages, guard/loan state and deterministic execution are unchanged. No new syntax, feature or repair is introduced; P6-7 remains open. Validation: the new pipeline test reproduces a failure before correction and passes afterwards; unit tests and full conformance pass, 1,162/1,162 with no failures or pending tests, including a source unknown/known capability pair. All final source/AIR diagnostics are checked for noncolliding fields and parsed as JSON with duplicate members forbidden, after frontend fields and source mapping. CLI passes 271 checks/161 context records with a strict JSON reader and targeted checks for all three corrected forms. Across the 1,199-file compatibility baseline, only the two known legacy AIR collision records change beyond the earlier related metadata; all successful outputs and exit codes remain identical. Independent read-only review by Claude found no missed emission or implementation defect; its recommendation to validate final mapped records is implemented in conformance. Budgets are recorded in docs/agent/README.md; frozen evaluation inputs and historical reports are unchanged. + +## P6-2 source scope-name increment (2026-10-07) + +Q-041 is resolved in spec 15 §7 (DD-020): source scope names are lexical identifiers, used only as the named first operand of `task.spawn`. Ordinary plain/moved value use rejects E7006 `invalid_expression`, with `name` and `type: scope`, before any misleading E7003 transfer hint. Borrowing and assignment retain their existing reasons; non-name/wrong-type spawn operands retain E7005. Ordinary shadowing, saved completion identity, live-child/loan checks and function-local AIR scope flow are unchanged. Formerly accepted aliases and scope-valued expressions now reject. No new source syntax, diagnostic code/reason, feature or verifier/runtime state is introduced. P6-2 remains open for its remaining block forms. + +Validation: unit tests and full conformance pass, 1,186/1,186 with no failures or pending tests, including 24 new source fixtures (four runs, twenty rejects). New cases cover aliases, block/branch/scope yields, aggregates, arguments, specialized bodies, shadowing, outer/nested scope lookup and unjoined children. Scope tests pass with 200 schedule seeds and collection before every instruction; the strengthened return case joins a task holding a ref loan, while break/continue cases join mutable loans. CLI diagnostics pass 206 checks over 88 public E7006 fixtures, including exact imported generic locations, unchanged alternate reasons and deterministic/hosted runs at three quanta. Formatter checks pass 3,359 assertions over 900 public files, with 888 parsed files, 12 syntax-error fixtures and 565 identical successful AIR outputs. Coverage remains 120/120. Across 1,208 original public source/AIR/std/example files, all 2,084 check/lower outputs and exit codes are byte-identical, including 560 successful source lowerings. Independent read-only Claude review found no accepted bypass or cleanup regression; its immutable-assignment spec clarification is included. Bundle growth is recorded in docs/agent/README.md; frozen evaluation inputs remain unchanged. This completes Q-041, not Phase 6. diff --git a/docs/spec/15-source-language.md b/docs/spec/15-source-language.md index e24ca17..bd05463 100644 --- a/docs/spec/15-source-language.md +++ b/docs/spec/15-source-language.md @@ -257,7 +257,11 @@ scope s { } ``` -**`scope s { … }`** is `task.scope.begin` and `task.scope.end`, at every normal exit of the block. Inside it: +**`scope s { … }`** is `task.scope.begin` and `task.scope.end`, at every normal exit of the block. + +The name `s` MUST denote only a lexical scope identifier, used by name as the first operand of `task.spawn`. It MUST NOT be used as an ordinary value (`s` or `move s`: `E7006`, reason `invalid_expression`) or borrowed (`E7006`, reason `invalid_place`). Assignment to this immutable identifier is `E7006`, reason `invalid_assignment`. Consequently it cannot be aliased, yielded, returned, put in an aggregate or passed as an ordinary argument. Ordinary lexical shadowing remains permitted: lookup selects the innermost binding, while completion always consumes the originating block's saved AIR scope identity. Leaving a shadowing block restores the outer binding; an unjoined child still rejects with `E4301`. This source restriction does not change function-local AIR scope flow (spec 04 §33). + +Task operations inside the block: - `task.spawn(s, f, args)` spawns `f` (a local or directly imported qualified function, §14) into the scope `s`. Arguments are passed like call arguments: a named linear value is `move`d (`E7003`). A borrow argument is lent until the join (spec 04 §34), so it is a borrow binding passed by name (`mut part = buf;` … `task.spawn(s, f, part)`). A borrow or byte string written inline is `E7006`, because a temporary would have to end right after the spawn. The first operand must be the scope (`E7005`); - `task.join(move t)` joins; its value is the task's result (`task` gives `T`); diff --git a/src/frontend/lower.zig b/src/frontend/lower.zig index 2261e5b..7a35dce 100644 --- a/src/frontend/lower.zig +++ b/src/frontend/lower.zig @@ -1509,7 +1509,7 @@ const Lowerer = struct { }, .boolean => |b| return l.def("c", .bool, if (b) "const.bool true" else "const.bool false", e.span), .name => |n| { - const b = l.lookup(n) orelse return l.unknown(e.span, n); + const b = try l.valueBinding(e.span, n); if (b.kind == .storage) return l.def(n, b.ty, try l.fmt("local.load %{s}", .{b.value}), e.span); return .{ .name = b.value, .ty = b.ty }; }, @@ -1608,7 +1608,7 @@ const Lowerer = struct { }, .or_trap => |x| return l.orTrap(e, x.*), .move => |n| { - const b = l.lookup(n) orelse return l.unknown(e.span, n); + const b = try l.valueBinding(e.span, n); if (!l.isLinear(b.ty) and !(e.generic_transfer and b.ty != .void and b.ty != .never and !b.ty.isBorrow() and b.ty != .task and b.ty != .scope and b.ty != .guard and b.ty != .shield)) return l.invalid(if (b.ty.isBorrow()) .move_borrow else if (b.ty == .void or b.ty == .never) .invalid_move else .move_copy, e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } }); if (b.kind == .storage) return l.def(n, b.ty, try l.fmt("local.load %{s}", .{b.value}), e.span); return .{ .name = b.value, .ty = b.ty }; @@ -2184,12 +2184,23 @@ const Lowerer = struct { } } + /// Scope identifiers are lexical operands, not ordinary source values + /// (spec 15 §7). Do not suggest moving their retained completion handle. + fn valueBinding(l: *Lowerer, span: Span, name: []const u8) Error!*Binding { + const b = l.lookup(name) orelse return l.unknown(span, name); + if (b.ty == .scope) return l.invalid(.invalid_expression, span, "a scope name is only the first operand of `task.spawn`; it cannot be used as a value", &.{ .{ .name = "name", .value = name }, .{ .name = "type", .value = "scope" } }); + return b; + } + /// An operand that consumes its value: a named linear value must be /// written `move x` (spec 15 §5). Use after move, double consumption and /// leaks are the verifier's (E2001, E2002, E3001, E3002). fn consuming(l: *Lowerer, e: ast.Expr, want: ?Ty) Error!Value { - if (e.kind == .name) if (l.lookup(e.kind.name)) |b| if (l.isLinear(b.ty) and b.kind == .ssa) - return l.fail("E7003", e.span, "a linear value is consumed here: write `move` to transfer it, or borrow it with `ref`/`mut`", &.{ .{ .name = "name", .value = e.kind.name }, .{ .name = "suggestion", .value = try l.fmt("move {s}", .{e.kind.name}) } }); + if (e.kind == .name) { + const b = try l.valueBinding(e.span, e.kind.name); + if (l.isLinear(b.ty) and b.kind == .ssa) + return l.fail("E7003", e.span, "a linear value is consumed here: write `move` to transfer it, or borrow it with `ref`/`mut`", &.{ .{ .name = "name", .value = e.kind.name }, .{ .name = "suggestion", .value = try l.fmt("move {s}", .{e.kind.name}) } }); + } return l.expr(e, want); } diff --git a/tests/conformance/source/scopes/array.aji b/tests/conformance/source/scopes/array.aji new file mode 100644 index 0000000..5c7cb4e --- /dev/null +++ b/tests/conformance/source/scopes/array.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.array + +fn main() -> u64 effects [] { scope s { let values = [move s]; } 0u64 } diff --git a/tests/conformance/source/scopes/assignment.aji b/tests/conformance/source/scopes/assignment.aji new file mode 100644 index 0000000..c032d05 --- /dev/null +++ b/tests/conformance/source/scopes/assignment.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.assignment + +fn main() -> u64 effects [] { scope s { s = 0u64; } 0u64 } diff --git a/tests/conformance/source/scopes/bare_alias.aji b/tests/conformance/source/scopes/bare_alias.aji new file mode 100644 index 0000000..b844dea --- /dev/null +++ b/tests/conformance/source/scopes/bare_alias.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.bare_alias + +fn main() -> u64 effects [] { scope s { let alias = s; } 0u64 } diff --git a/tests/conformance/source/scopes/bare_array.aji b/tests/conformance/source/scopes/bare_array.aji new file mode 100644 index 0000000..30e5730 --- /dev/null +++ b/tests/conformance/source/scopes/bare_array.aji @@ -0,0 +1,5 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.bare_array +fn main() -> u64 effects [] { scope s { let values = [s]; } 0u64 } diff --git a/tests/conformance/source/scopes/block_yield.aji b/tests/conformance/source/scopes/block_yield.aji new file mode 100644 index 0000000..5b73f4e --- /dev/null +++ b/tests/conformance/source/scopes/block_yield.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.block_yield + +fn main() -> u64 effects [] { scope s { let alias = { s }; } 0u64 } diff --git a/tests/conformance/source/scopes/borrow.aji b/tests/conformance/source/scopes/borrow.aji new file mode 100644 index 0000000..e93ee71 --- /dev/null +++ b/tests/conformance/source/scopes/borrow.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.borrow + +fn main() -> u64 effects [] { scope s { ref r = s; } 0u64 } diff --git a/tests/conformance/source/scopes/call_argument.aji b/tests/conformance/source/scopes/call_argument.aji new file mode 100644 index 0000000..5e28bc7 --- /dev/null +++ b/tests/conformance/source/scopes/call_argument.aji @@ -0,0 +1,7 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.call_argument + +fn identity(x: u64) -> u64 effects [] { x } +fn main() -> u64 effects [] { scope s { let n = identity(s); } 0u64 } diff --git a/tests/conformance/source/scopes/discard.aji b/tests/conformance/source/scopes/discard.aji new file mode 100644 index 0000000..37a4200 --- /dev/null +++ b/tests/conformance/source/scopes/discard.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.discard + +fn main() -> u64 effects [] { scope s { s; } 0u64 } diff --git a/tests/conformance/source/scopes/equality.aji b/tests/conformance/source/scopes/equality.aji new file mode 100644 index 0000000..0d2d9b3 --- /dev/null +++ b/tests/conformance/source/scopes/equality.aji @@ -0,0 +1,5 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.equality +fn main() -> u64 effects [] { scope s { let same = s == s; } 0u64 } diff --git a/tests/conformance/source/scopes/generic_alias.aji b/tests/conformance/source/scopes/generic_alias.aji new file mode 100644 index 0000000..e9b463d --- /dev/null +++ b/tests/conformance/source/scopes/generic_alias.aji @@ -0,0 +1,10 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.generic_alias + +fn identity(x: T) -> T effects [] { + scope s { let alias = move s; } + move x +} +fn main() -> u64 effects [] { identity::(42u64) } diff --git a/tests/conformance/source/scopes/if_yield.aji b/tests/conformance/source/scopes/if_yield.aji new file mode 100644 index 0000000..eb6b34d --- /dev/null +++ b/tests/conformance/source/scopes/if_yield.aji @@ -0,0 +1,8 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.if_yield +fn main() -> u64 effects [] { + scope s { let alias = if true { s } else { s }; } + 0u64 +} diff --git a/tests/conformance/source/scopes/match_yield.aji b/tests/conformance/source/scopes/match_yield.aji new file mode 100644 index 0000000..125b384 --- /dev/null +++ b/tests/conformance/source/scopes/match_yield.aji @@ -0,0 +1,8 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.match_yield +fn main() -> u64 effects [] { + scope s { let alias = match 0u64 { 0 => { s }, _ => { s } }; } + 0u64 +} diff --git a/tests/conformance/source/scopes/move_alias.aji b/tests/conformance/source/scopes/move_alias.aji new file mode 100644 index 0000000..a035fe7 --- /dev/null +++ b/tests/conformance/source/scopes/move_alias.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.move_alias + +fn main() -> u64 effects [] { scope s { let alias = move s; } 0u64 } diff --git a/tests/conformance/source/scopes/move_discard.aji b/tests/conformance/source/scopes/move_discard.aji new file mode 100644 index 0000000..94bbfec --- /dev/null +++ b/tests/conformance/source/scopes/move_discard.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.move_discard + +fn main() -> u64 effects [] { scope s { move s; } 0u64 } diff --git a/tests/conformance/source/scopes/moved_spawn.aji b/tests/conformance/source/scopes/moved_spawn.aji new file mode 100644 index 0000000..1c77e23 --- /dev/null +++ b/tests/conformance/source/scopes/moved_spawn.aji @@ -0,0 +1,10 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.moved_spawn + +fn work() -> u64 effects [] { 42u64 } +fn main() -> u64 effects [task.spawn, task.wait] { + scope s { let t = task.spawn(move s, work); let n = task.join(move t); } + 0u64 +} diff --git a/tests/conformance/source/scopes/nested_same_name.aji b/tests/conformance/source/scopes/nested_same_name.aji new file mode 100644 index 0000000..0382cc5 --- /dev/null +++ b/tests/conformance/source/scopes/nested_same_name.aji @@ -0,0 +1,21 @@ +// expect: run +// result: 42 +// schedules: 8 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.nested_same_name + +fn work(x: u64) -> u64 effects [] { x } +fn main() -> u64 effects [task.spawn, task.wait] { + var total: u64 = 0; + scope s { + let first = task.spawn(s, work, 20u64); + scope s { + let second = task.spawn(s, work, 22u64); + let s = 0u64; + total = task.join(move second) + s; + } + total = total + task.join(move first); + } + total +} diff --git a/tests/conformance/source/scopes/normal_exits.aji b/tests/conformance/source/scopes/normal_exits.aji new file mode 100644 index 0000000..24a49ec --- /dev/null +++ b/tests/conformance/source/scopes/normal_exits.aji @@ -0,0 +1,28 @@ +// expect: run +// result: 42 +// schedules: 8 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.normal_exits + +fn work(x: mut) -> void effects [] { *x = *x + 7u64; } +fn read(x: ref) -> u64 effects [] { *x } +fn main() -> u64 effects [task.spawn, task.wait] { + var data: u64 = 0; + for i in 0usize..8usize { + scope s { + mut part = data; + let t = task.spawn(s, work, part); + task.join(move t); + let s = i; + if s < 5 { continue; } + break; + } + } + scope s { + ref part = data; + let t = task.spawn(s, read, part); + let s = task.join(move t); + return s; + } +} diff --git a/tests/conformance/source/scopes/out_of_block.aji b/tests/conformance/source/scopes/out_of_block.aji new file mode 100644 index 0000000..96ab305 --- /dev/null +++ b/tests/conformance/source/scopes/out_of_block.aji @@ -0,0 +1,6 @@ +// expect: reject E7002 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.out_of_block + +fn main() -> u64 effects [] { scope s {} s } diff --git a/tests/conformance/source/scopes/return.aji b/tests/conformance/source/scopes/return.aji new file mode 100644 index 0000000..20ba1e2 --- /dev/null +++ b/tests/conformance/source/scopes/return.aji @@ -0,0 +1,6 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.return + +fn main() -> u64 effects [] { scope s { return move s; } 0u64 } diff --git a/tests/conformance/source/scopes/scope_tail.aji b/tests/conformance/source/scopes/scope_tail.aji new file mode 100644 index 0000000..c108487 --- /dev/null +++ b/tests/conformance/source/scopes/scope_tail.aji @@ -0,0 +1,5 @@ +// expect: reject E7006 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.scope_tail +fn main() -> u64 effects [] { scope s { s } 0u64 } diff --git a/tests/conformance/source/scopes/shadow_value.aji b/tests/conformance/source/scopes/shadow_value.aji new file mode 100644 index 0000000..d82c868 --- /dev/null +++ b/tests/conformance/source/scopes/shadow_value.aji @@ -0,0 +1,15 @@ +// expect: run +// result: 42 +// schedules: 8 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.shadow_value + +fn main() -> u64 effects [] { + scope s { + let s = 20u64; + { var s: u64 = 21; s = s + 1u64; } + let n = s + 22u64; + return n; + } +} diff --git a/tests/conformance/source/scopes/spawn_shadow.aji b/tests/conformance/source/scopes/spawn_shadow.aji new file mode 100644 index 0000000..8ea7084 --- /dev/null +++ b/tests/conformance/source/scopes/spawn_shadow.aji @@ -0,0 +1,10 @@ +// expect: reject E7005 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.spawn_shadow + +fn work() -> u64 effects [] { 42u64 } +fn main() -> u64 effects [task.spawn, task.wait] { + scope s { let s = 0u64; let t = task.spawn(s, work); let n = task.join(move t); } + 0u64 +} diff --git a/tests/conformance/source/scopes/unjoined_shadow.aji b/tests/conformance/source/scopes/unjoined_shadow.aji new file mode 100644 index 0000000..89b2a2b --- /dev/null +++ b/tests/conformance/source/scopes/unjoined_shadow.aji @@ -0,0 +1,10 @@ +// expect: reject E4301 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.unjoined_shadow + +fn work() -> u64 effects [] { 42u64 } +fn main() -> u64 effects [task.spawn] { + scope s { let t = task.spawn(s, work); let s = 0u64; } + 0u64 +} diff --git a/tests/conformance/source/scopes/visible_outer.aji b/tests/conformance/source/scopes/visible_outer.aji new file mode 100644 index 0000000..4435f32 --- /dev/null +++ b/tests/conformance/source/scopes/visible_outer.aji @@ -0,0 +1,18 @@ +// expect: run +// result: 42 +// schedules: 8 +// phase: 6 +// spec: docs/spec/15-source-language.md §7 +module scope_cases.visible_outer + +fn work() -> u64 effects [] { 42u64 } +fn main() -> u64 effects [task.spawn, task.wait] { + var result: u64 = 0; + scope outer { + scope inner { + let t = task.spawn(outer, work); + result = task.join(move t); + } + } + result +} diff --git a/tests/hosted/source_diagnostic_reasons.py b/tests/hosted/source_diagnostic_reasons.py index 5f31321..42dba82 100644 --- a/tests/hosted/source_diagnostic_reasons.py +++ b/tests/hosted/source_diagnostic_reasons.py @@ -100,4 +100,49 @@ with tempfile.TemporaryDirectory(prefix='aji-reasons-') as d: ordinary.write_text('module ordinary fn main()->u64 effects [] {return true;}') error = json.loads(invoke('check', ordinary, code=1)) assert error['code'] == 'E7005' and 'reason' not in error + + # A scope name never reaches ordinary value lowering, including generic + # bodies and branch/block yields. No misleading move-repair is emitted. + scope_cases = REPO / 'tests/conformance/source/scopes' + for name in ('array', 'bare_array', 'bare_alias', 'block_yield', 'call_argument', + 'discard', 'equality', 'generic_alias', 'if_yield', 'match_yield', + 'move_alias', 'move_discard', 'return', 'scope_tail'): + path = scope_cases / f'{name}.aji' + error = json.loads(invoke('check', path, code=1)) + assert (error['code'], error['reason'], error['name'], error['type']) == ( + 'E7006', 'invalid_expression', 's', 'scope'), error + assert 'suggestion' not in error, error + line = path.read_text().splitlines()[error['line'] - 1] + assert line[error['col'] - 1:].startswith(('s', 'move s')), (path, error) + assert json.loads(invoke('lower', path, code=1)) == error + + for name, code, reason in (('borrow', 'E7006', 'invalid_place'), + ('assignment', 'E7006', 'invalid_assignment'), + ('moved_spawn', 'E7005', None), + ('spawn_shadow', 'E7005', None), + ('out_of_block', 'E7002', None), + ('unjoined_shadow', 'E4301', None)): + error = json.loads(invoke('check', scope_cases / f'{name}.aji', code=1)) + assert error['code'] == code and error.get('reason') == reason, error + + # Imported specialized bodies retain the location of their forbidden + # scope use, rather than the call or the generated AIR. + entry.write_text('module app import dep; fn main()->u64 effects [] {dep.f::(42u64)}') + dep.write_text('module dep\nfn f(x:T)->T effects [] {\n' + 'scope s { let alias = move s; }\nmove x\n}\n') + error = json.loads(invoke('check', '--manifest', manifest, code=1)) + assert (error['code'], error['reason'], error['file'], error['line'], error['col']) == ( + 'E7006', 'invalid_expression', dep.name, 3, 23), error + assert json.loads(invoke('lower', '--manifest', manifest, code=1)) == error + + # Lexical completion and task/borrow cleanup are identical under both + # profiles and independent scheduler choices, even with shadowed names. + for name in ('nested_same_name', 'normal_exits', 'visible_outer', 'shadow_value'): + path = scope_cases / f'{name}.aji' + for quantum in (1, 5, 16): + args = ['--fuel', '10000', '--quantum', str(quantum), '--schedule-seed', '7'] + assert json.loads(invoke('run', *args, path))['value'] == 42 + result_file = root / 'scope-result.json' + invoke('run', '--profile', 'hosted', '--result-file', result_file, *args, path) + assert json.loads(result_file.read_text())['value'] == 42 print(f'source diagnostic reasons passed ({checks} checks; {fixtures} public E7006 fixtures)')