lang: reject writes through constants (PLAN-CONST-AGG step 2, fixes 0116)
Any assignment / compound-assignment whose target chain is ROOTED at a
constant — a const-flagged global (array consts, #run consts) or a
module value const (struct consts incl.) — diagnoses 'cannot assign
through constant X' at compile time. A struct const's field write used
to compile and bus-error at runtime (issue 0116); scalars misfired
silently. A deref along the chain (p.*) breaks the root — pointer
writes stay the documented escape until the const-ness steps; a local
shadowing the const name stays writable.
Also: typed struct constants ('W : Color : Color.{...}') register —
the shape list skipped struct_literal, leaving the typed form
unresolved while the untyped one worked.
Examples: 1162 (all rejection shapes incl. the 0116 crash repro),
0178 (typed struct const reads + copy independence).
This commit is contained in:
@@ -801,7 +801,7 @@ pub fn registerTypedModuleConst(self: *Lowering, cd: *const ast.ConstDecl) void
|
||||
// foldable / emittable const, so it cannot manifest a
|
||||
// wrong-type fold/emit; a use-site diagnostic covers it.
|
||||
switch (cd.value.data) {
|
||||
.int_literal, .float_literal, .bool_literal, .string_literal, .undef_literal, .null_literal, .binary_op, .unary_op => {},
|
||||
.int_literal, .float_literal, .bool_literal, .string_literal, .undef_literal, .null_literal, .binary_op, .unary_op, .struct_literal => {},
|
||||
else => return,
|
||||
}
|
||||
const ty = self.resolveType(ta);
|
||||
|
||||
@@ -540,7 +540,49 @@ pub fn lowerReturn(self: *Lowering, rs: *const ast.ReturnStmt) void {
|
||||
}
|
||||
}
|
||||
|
||||
/// The ROOT identifier of an assignment-target chain (`K[0].x` → "K",
|
||||
/// `WHITE.r` → "WHITE"). A deref along the chain (`p.*`, `p.*[i]`) breaks
|
||||
/// it — writing through a pointer VALUE is not a write to the named root.
|
||||
fn assignmentRootIdent(target: *const Node) ?[]const u8 {
|
||||
var n = target;
|
||||
while (true) {
|
||||
switch (n.data) {
|
||||
.identifier => |id| return id.name,
|
||||
.index_expr => |ie| n = ie.object,
|
||||
.field_access => |fa| n = fa.object,
|
||||
else => return null,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// True when `root` names a module CONSTANT from the current source —
|
||||
/// a const-flagged global (array/struct consts, #run consts) or a
|
||||
/// module value const. Locals shadow (caller checks scope first).
|
||||
fn rootIsConstant(self: *Lowering, root: []const u8) bool {
|
||||
switch (self.selectGlobalAuthor(root)) {
|
||||
.resolved => |g| if (self.module.globals.items[g.id.index()].is_const) return true,
|
||||
else => {},
|
||||
}
|
||||
return switch (self.selectModuleConst(root)) {
|
||||
.resolved, .own_opaque => true,
|
||||
.ambiguous, .none => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn lowerAssignment(self: *Lowering, asgn: *const ast.Assignment) void {
|
||||
// Writes through a constant are rejected at compile time (issue 0116):
|
||||
// the target chain's root naming a const global (array/struct consts,
|
||||
// #run consts) or a module value const cannot be stored to — for a
|
||||
// struct const the store previously compiled and bus-errored at
|
||||
// runtime; for scalars it silently misfired.
|
||||
if (assignmentRootIdent(asgn.target)) |root| {
|
||||
const shadowed = if (self.scope) |s| s.lookup(root) != null else false;
|
||||
if (!shadowed and rootIsConstant(self, root)) {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, asgn.target.span, "cannot assign through constant '{s}' — constants are immutable (use a '=' global or a local copy for mutable data)", .{root});
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Set target_type from LHS for RHS lowering (enum literals, struct literals, etc.)
|
||||
const old_target = self.target_type;
|
||||
if (asgn.target.data == .identifier) {
|
||||
|
||||
Reference in New Issue
Block a user