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

@@ -84,91 +84,176 @@ pub const UnknownTypeChecker = struct {
}
}
/// Reserved-type-name binding walk (issues 0076, 0077). Visits every binding
/// site reachable from `node` — `var` / `:=` / typed-local declarations and
/// function / lambda / struct-method parameters, at any nesting depth — and
/// rejects each name that collides with a reserved/builtin type name. Walks
/// into expressions too, so a lambda nested in a call arg / struct literal is
/// reached. Deliberately filter-free (every module is walked) and context-
/// free (spelling is the sole criterion), distinct from the main-file-scoped
/// unknown-type walk. A node carrying its own `source_file` (every module's
/// top-level decls do) becomes the emit file for its whole subtree, restored
/// on exit so a sibling in another module isn't rendered against it.
/// Reserved-type-name binding walk (issues 0076, 0077). Visits every node
/// reachable from `node` and rejects each *binding name* — `var` / `:=` /
/// typed-local declarations, destructure names, function / lambda / method
/// parameters, `if` / `while` optional bindings, `for` capture + index
/// names, match-arm captures, and `catch` / `onfail` tag bindings — whose
/// spelling collides with a reserved/builtin type name. Such a spelling
/// parses as a `.type_expr`, so the address-of family in `lower.zig` never
/// sees the scoped local and mis-lowers it (a loaded aggregate passed
/// by value to a `ptr` param → LLVM verifier abort, or a silent
/// mutation-losing copy). Rejecting the name here, before lowering, keeps
/// the `.identifier`-only address-of paths correct with no lowering
/// special-case.
///
/// The `switch` is EXHAUSTIVE — every `Node.Data` tag is listed and there
/// is NO `else` arm. A future binding-bearing node type therefore fails to
/// compile here until it is handled, so coverage is enforced by the
/// compiler rather than by remembering to extend a hand-maintained list.
/// (The check can't live at the scope-registration choke point in
/// `lower.zig`: lowering is lazy, so an UNCALLED function's bindings never
/// reach `Scope.put` — yet they must still be rejected at their
/// declaration.) Deliberately filter-free (every compiled module is walked)
/// and context-free (spelling is the sole criterion), distinct from the
/// main-file-scoped unknown-type walk. A node carrying its own
/// `source_file` (every module's top-level decls do) becomes the emit file
/// for its whole subtree, restored on exit so a sibling in another module
/// isn't rendered against it (issue 0077).
fn checkBindingNames(self: UnknownTypeChecker, node: *const Node) void {
const saved_file = self.diagnostics.current_source_file;
defer self.diagnostics.current_source_file = saved_file;
if (node.source_file) |sf| self.diagnostics.current_source_file = sf;
switch (node.data) {
// ── Binding-introducing nodes: check the name(s), then recurse. ──
.var_decl => |vd| {
self.checkBindingName(vd.name, node.span);
if (vd.value) |v| self.checkBindingNames(v);
},
.destructure_decl => |dd| {
for (dd.names) |n| self.checkBindingName(n, node.span);
self.checkBindingNames(dd.value);
},
.fn_decl => |fd| {
for (fd.params) |p| self.checkBindingName(p.name, p.name_span);
self.checkParamNames(fd.params);
self.checkBindingNames(fd.body);
},
.lambda => |lm| {
for (lm.params) |p| self.checkBindingName(p.name, p.name_span);
self.checkParamNames(lm.params);
self.checkBindingNames(lm.body);
},
.const_decl => |cd| self.checkBindingNames(cd.value),
// A namespaced import (`mod :: #import "..."`) is wrapped here, its
// module decls held inline. Descend so an imported module's
// reserved-name binding is rejected too (issue 0077).
.namespace_decl => |nd| for (nd.decls) |d| self.checkBindingNames(d),
.struct_decl => |sd| for (sd.methods) |m| self.checkBindingNames(m),
.block => |b| for (b.stmts) |s| self.checkBindingNames(s),
.param => |p| {
self.checkBindingName(p.name, p.name_span);
if (p.default_expr) |de| self.checkBindingNames(de);
},
.if_expr => |ie| {
if (ie.binding_name) |bn| self.checkBindingName(bn, node.span);
self.checkBindingNames(ie.condition);
self.checkBindingNames(ie.then_branch);
if (ie.else_branch) |e| self.checkBindingNames(e);
},
.while_expr => |we| {
if (we.binding_name) |bn| self.checkBindingName(bn, node.span);
self.checkBindingNames(we.condition);
self.checkBindingNames(we.body);
},
.for_expr => |fe| {
if (fe.capture_name.len != 0) self.checkBindingName(fe.capture_name, node.span);
if (fe.index_name) |idx| self.checkBindingName(idx, node.span);
self.checkBindingNames(fe.iterable);
if (fe.range_end) |re| self.checkBindingNames(re);
self.checkBindingNames(fe.body);
},
.match_expr => |me| {
self.checkBindingNames(me.subject);
for (me.arms) |arm| self.checkBindingNames(arm.body);
for (me.arms) |arm| {
if (arm.capture) |cap| self.checkBindingName(cap, node.span);
if (arm.pattern) |p| self.checkBindingNames(p);
self.checkBindingNames(arm.body);
}
},
.match_arm => |arm| {
if (arm.capture) |cap| self.checkBindingName(cap, node.span);
if (arm.pattern) |p| self.checkBindingNames(p);
self.checkBindingNames(arm.body);
},
.catch_expr => |ce| {
if (ce.binding) |b| self.checkBindingName(b, node.span);
self.checkBindingNames(ce.operand);
self.checkBindingNames(ce.body);
},
.onfail_stmt => |os| {
if (os.binding) |b| self.checkBindingName(b, node.span);
self.checkBindingNames(os.body);
},
// impl / protocol-default / foreign-class method bodies: each
// method introduces its own params + locals. A `#jni_main` /
// `#objc_class` bodied method is lowered (M1.2), so its reserved
// param/local names mis-lower the same as any other.
.impl_block => |ib| for (ib.methods) |m| self.checkBindingNames(m),
.protocol_decl => |pd| for (pd.methods) |m| {
if (m.default_body) |body| {
for (m.param_names) |pn| self.checkBindingName(pn, node.span);
self.checkBindingNames(body);
}
},
.foreign_class_decl => |fcd| for (fcd.members) |member| switch (member) {
.method => |m| if (m.body) |body| {
for (m.param_names) |pn| self.checkBindingName(pn, node.span);
self.checkBindingNames(body);
},
.field, .extends, .implements => {},
},
// ── Container / control-flow / expression nodes: recurse children
// so a binding nested anywhere below is still reached. ──
// A namespaced import (`mod :: #import "..."`) is wrapped here, its
// module decls held inline; descend so an imported module's
// reserved-name binding is rejected too (issue 0077).
.namespace_decl => |nd| for (nd.decls) |d| self.checkBindingNames(d),
.const_decl => |cd| self.checkBindingNames(cd.value),
.struct_decl => |sd| {
for (sd.methods) |m| self.checkBindingNames(m);
for (sd.constants) |c| self.checkBindingNames(c);
for (sd.field_defaults) |fdef| if (fdef) |d| self.checkBindingNames(d);
},
.root => |r| for (r.decls) |d| self.checkBindingNames(d),
.block => |b| for (b.stmts) |s| self.checkBindingNames(s),
.push_stmt => |ps| {
self.checkBindingNames(ps.context_expr);
self.checkBindingNames(ps.body);
},
.jni_env_block => |jb| {
self.checkBindingNames(jb.env);
self.checkBindingNames(jb.body);
},
.defer_stmt => |ds| self.checkBindingNames(ds.expr),
.onfail_stmt => |os| self.checkBindingNames(os.body),
.return_stmt => |r| if (r.value) |v| self.checkBindingNames(v),
.raise_stmt => |rs| self.checkBindingNames(rs.tag),
.assignment => |a| {
self.checkBindingNames(a.value);
self.checkBindingNames(a.target);
},
.multi_assign => |ma| for (ma.values) |v| self.checkBindingNames(v),
.destructure_decl => |dd| self.checkBindingNames(dd.value),
.multi_assign => |ma| {
for (ma.targets) |t| self.checkBindingNames(t);
for (ma.values) |v| self.checkBindingNames(v);
},
.call => |c| {
self.checkBindingNames(c.callee);
for (c.args) |a| self.checkBindingNames(a);
},
.ffi_intrinsic_call => |fic| for (fic.args) |a| self.checkBindingNames(a),
.binary_op => |b| {
self.checkBindingNames(b.lhs);
self.checkBindingNames(b.rhs);
},
.chained_comparison => |cc| for (cc.operands) |o| self.checkBindingNames(o),
.unary_op => |u| self.checkBindingNames(u.operand),
.field_access => |fa| self.checkBindingNames(fa.object),
.index_expr => |ix| {
self.checkBindingNames(ix.object);
self.checkBindingNames(ix.index);
},
.slice_expr => |sx| {
self.checkBindingNames(sx.object);
if (sx.start) |s| self.checkBindingNames(s);
if (sx.end) |e| self.checkBindingNames(e);
},
.struct_literal => |sl| {
for (sl.field_inits) |fi| self.checkBindingNames(fi.value);
if (sl.init_block) |ib| self.checkBindingNames(ib);
},
.array_literal => |al| for (al.elements) |e| self.checkBindingNames(e),
.tuple_literal => |tl| for (tl.elements) |e| self.checkBindingNames(e.value),
.force_unwrap => |fu| self.checkBindingNames(fu.operand),
.null_coalesce => |nc| {
self.checkBindingNames(nc.lhs);
@@ -176,13 +261,63 @@ pub const UnknownTypeChecker = struct {
},
.deref_expr => |de| self.checkBindingNames(de.operand),
.try_expr => |te| self.checkBindingNames(te.operand),
.catch_expr => |ce| {
self.checkBindingNames(ce.operand);
self.checkBindingNames(ce.body);
},
.comptime_expr => |ce| self.checkBindingNames(ce.expr),
.insert_expr => |ins| self.checkBindingNames(ins.expr),
.spread_expr => |se| self.checkBindingNames(se.operand),
else => {},
// ── Leaves & pure type-expression nodes: no binding sites below. ──
// Type-expression subtrees carry only type names (no value
// bindings); enum / union / error-set declarations carry only field
// types + comptime constants. Listing each tag explicitly (rather
// than an `else`) is what forces a future binding-bearing node to be
// reconsidered here.
.int_literal,
.float_literal,
.bool_literal,
.string_literal,
.identifier,
.enum_literal,
.type_expr,
.enum_decl,
.union_decl,
.error_set_decl,
.import_decl,
.array_type_expr,
.slice_type_expr,
.parameterized_type_expr,
.pointer_type_expr,
.many_pointer_type_expr,
.optional_type_expr,
.error_type_expr,
.caller_location,
.pack_index_type_expr,
.comptime_pack_ref,
.null_literal,
.break_expr,
.continue_expr,
.undef_literal,
.inferred_type,
.builtin_expr,
.compiler_expr,
.foreign_expr,
.library_decl,
.framework_decl,
.function_type_expr,
.closure_type_expr,
.tuple_type_expr,
.ufcs_alias,
.c_import_decl,
=> {},
}
}
/// Check each parameter's binding name (`fn` / lambda params are stored as
/// `Param` values, not child nodes, so they're walked here rather than via
/// the node `switch`). A param default expression can itself nest bindings
/// (a lambda default), so recurse into it.
fn checkParamNames(self: UnknownTypeChecker, params: []const ast.Param) void {
for (params) |p| {
self.checkBindingName(p.name, p.name_span);
if (p.default_expr) |de| self.checkBindingNames(de);
}
}