fix(diagnostics): make reserved-type-name binding check exhaustive (issue 0076)

The reserved/builtin-type-name binding diagnostic was a hand-walked subset
of binding-bearing AST nodes with a silent `else => {}`, so each review
found another syntactic binding form that bypassed it and hit the original
LLVM verifier abort: destructure names (`s2, x := …`), `impl` method
params/locals, and `if` / `while` / `for` / match-arm / `catch` / `onfail`
captures.

Rewrite `checkBindingNames` (src/ir/semantic_diagnostics.zig) as an
EXHAUSTIVE `switch` over every `Node.Data` tag with NO `else` arm — a future
binding-bearing node type now fails to compile until it is handled here, so
coverage is enforced by the compiler instead of a hand-maintained list. The
check stays in the pre-lowering semantic pass rather than moving to the
`Scope.put` scope-registration choke point: lowering is lazy, so an
uncalled function's bindings never reach `Scope.put`, yet they must still be
rejected at their declaration (e.g. the never-called `takes_u8` in 1119).
No lowering special-case; `lower.zig` unchanged.

Regression tests (fail-before: LLVM abort or silent accept → pass-after:
clean diagnostic, exit 1):
- 1121 control-flow: destructure, if/while bindings, for capture+index,
  match-arm capture
- 1122 impl-block method: reserved param AND reserved local
- 1123 catch + onfail tag bindings
- 1124 destructure name reserved in an imported module
Existing 0125 / 1119 / 0135 / 1120 tests kept; full suite 368 passed.
This commit is contained in:
agra
2026-06-03 20:09:46 +03:00
parent df6e830bec
commit fcc76b9391
19 changed files with 375 additions and 32 deletions

View File

@@ -0,0 +1,30 @@
// Reserved/builtin type names are rejected as binding NAMES across every
// control-flow and destructuring form, not just plain `var`/param decls: a
// destructure name (`s2`), an `if`/`while` optional binding (`u8`/`s16`), a
// `for` capture and index name (`bool`/`s32`), and a match-arm capture
// (`string`). Each spelling parses as a `.type_expr`, so the address-of family
// in lowering mis-lowers it (a loaded aggregate passed by value to a `ptr`
// param → LLVM verifier abort). The declaration-site diagnostic comes from one
// EXHAUSTIVE binding-name walk, so no syntactic binding form can slip through.
//
// Regression (issue 0076, attempt-4 coverage). Expected: one error per
// offending name; exit 1 — NOT an LLVM verifier abort.
#import "modules/std.sx";
pair :: () -> (s64, s64) { (1, 2) }
maybe :: () -> ?s64 { return null; }
main :: () -> s32 {
s2, rest := pair(); // destructure name
if u8 := maybe() { } // if optional binding
while s16 := maybe() { break; } // while optional binding
xs := [3]s64.{ 10, 20, 30 };
for xs: (bool) { } // for capture name
for xs: (v, s32) { } // for index name
opt: ?s64 = 5;
r := if opt == { // match-arm capture
case .some: (string) { 0 }
case .none: { 0 }
};
return 0;
}

View File

@@ -0,0 +1,30 @@
// A reserved/builtin type name is rejected as a binding name inside an `impl`
// block's method too — both as a parameter (`u8`) and as a local (`s2`). The
// impl method is reached through the exhaustive binding-name walk's
// `impl_block` arm (→ each method's `fn_decl`), so an `impl` method is no more
// exempt than a free function. Without the diagnostic the reserved local's
// `@s2` mis-lowers (a loaded aggregate passed by value to a `*Box` param →
// LLVM verifier abort).
//
// Regression (issue 0076, attempt-4 coverage). Expected: one error for the
// param and one for the local; exit 1.
#import "modules/std.sx";
Box :: struct { total: s64 = 0; count: s64 = 0; }
update :: (self: *Box, n: s64) { self.total += n; self.count += 1; }
Doer :: protocol { go :: (self: *Self, n: s64); }
impl Doer for Box {
go :: (self: *Box, u8: s64) {
s2 := Box.{ total = 1 };
update(@s2, u8);
self.total += s2.total;
}
}
main :: () -> s32 {
b := Box.{};
b.go(7);
return 0;
}

View File

@@ -0,0 +1,28 @@
// A reserved/builtin type name is rejected as the error-tag binding of a
// `catch` (`u8`) and of an `onfail` (`s64`). Both are reached through the
// exhaustive binding-name walk's `catch_expr` / `onfail_stmt` arms. The tag is
// a scalar, so before the diagnostic these spellings were silently accepted
// (they never reached the address-of mis-lowering) — the binding must still be
// rejected at its declaration.
//
// Regression (issue 0076, attempt-4 coverage). Expected: one error for each
// binding; exit 1.
#import "modules/std.sx";
E :: error { Bad }
must :: (n: s32) -> !E {
if n < 0 { raise error.Bad; }
return;
}
classify :: (n: s32) -> !E {
onfail s64 { } // onfail tag binding
must(n) catch u8 { return; }; // catch tag binding
return;
}
main :: () -> s32 {
classify(-1) catch { };
return 0;
}

View File

@@ -0,0 +1,15 @@
// A reserved type name used as a DESTRUCTURE binding name (`s2`) is rejected
// even when it lives in an IMPORTED module — the exhaustive binding-name walk
// descends the `namespace_decl` an `mod :: #import` wraps and renders the
// diagnostic against that module's source (issue 0077's universal-coverage
// rule applied to the destructure form). Without it the binding reaches
// lowering and aborts LLVM verification.
//
// Regression (issues 0076 + 0077, attempt-4 coverage). Expected: one clean
// diagnostic pointing at the imported module's `s2, rest := ...`, exit 1.
#import "modules/std.sx";
mod :: #import "1124-diagnostics-imported-reserved-destructure/mod.sx";
main :: () -> s32 {
return mod.run();
}

View File

@@ -0,0 +1,8 @@
#import "modules/std.sx";
pair :: () -> (s64, s64) { (1, 2) }
run :: () -> s32 {
s2, rest := pair(); // destructure name in an IMPORTED module
return 0;
}

View File

@@ -0,0 +1,41 @@
error: 's2' is a reserved type name and cannot be used as an identifier
--> examples/1121-diagnostics-reserved-name-control-flow.sx:18:5
|
18 | s2, rest := pair(); // destructure name
| ^^^^^^^^^^^^^^^^^^^
error: 'u8' is a reserved type name and cannot be used as an identifier
--> examples/1121-diagnostics-reserved-name-control-flow.sx:19:5
|
19 | if u8 := maybe() { } // if optional binding
| ^^^^^^^^^^^^^^^^^^^^
error: 's16' is a reserved type name and cannot be used as an identifier
--> examples/1121-diagnostics-reserved-name-control-flow.sx:20:5
|
20 | while s16 := maybe() { break; } // while optional binding
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: 'bool' is a reserved type name and cannot be used as an identifier
--> examples/1121-diagnostics-reserved-name-control-flow.sx:22:5
|
22 | for xs: (bool) { } // for capture name
| ^^^^^^^^^^^^^^^^^^
error: 's32' is a reserved type name and cannot be used as an identifier
--> examples/1121-diagnostics-reserved-name-control-flow.sx:23:5
|
23 | for xs: (v, s32) { } // for index name
| ^^^^^^^^^^^^^^^^^^^^
error: 'string' is a reserved type name and cannot be used as an identifier
--> examples/1121-diagnostics-reserved-name-control-flow.sx:25:10
|
25 | r := if opt == { // match-arm capture
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26 | case .some: (string) { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 | case .none: { 0 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^
28 | };
| ^^^^^

View File

@@ -0,0 +1,11 @@
error: 'u8' is a reserved type name and cannot be used as an identifier
--> examples/1122-diagnostics-reserved-name-impl-method.sx:19:24
|
19 | go :: (self: *Box, u8: s64) {
| ^^
error: 's2' is a reserved type name and cannot be used as an identifier
--> examples/1122-diagnostics-reserved-name-impl-method.sx:20:9
|
20 | s2 := Box.{ total = 1 };
| ^^^^^^^^^^^^^^^^^^^^^^^^

View File

@@ -0,0 +1,11 @@
error: 's64' is a reserved type name and cannot be used as an identifier
--> examples/1123-diagnostics-reserved-name-catch-onfail.sx:20:5
|
20 | onfail s64 { } // onfail tag binding
| ^^^^^^^^^^^^^^
error: 'u8' is a reserved type name and cannot be used as an identifier
--> examples/1123-diagnostics-reserved-name-catch-onfail.sx:21:5
|
21 | must(n) catch u8 { return; }; // catch tag binding
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@@ -0,0 +1,5 @@
error: 's2' is a reserved type name and cannot be used as an identifier
--> examples/1124-diagnostics-imported-reserved-destructure/mod.sx:6:5
|
6 | s2, rest := pair(); // destructure name in an IMPORTED module
| ^^^^^^^^^^^^^^^^^^^