lang: const-aggregate comptime folds (PLAN-CONST-AGG step 3)
An array const's '.len' and 'K[<const idx>]' element reads, and a
struct const's field ('LIT.r'), fold as compile-time integer leaves —
usable in array dimensions and other constants' initializers. All
source-aware (the SELECTED author's elements, folded in the author's
context with the cyclic-definition frame); a const out-of-range index
diagnoses at fold time, never wraps.
- evalConstIntExpr gains the three ctx hooks (lookupConstAggLen /
lookupConstArrayElem / lookupConstStructField) + an index_expr arm;
all five ctx implementations extended (stateless tiers fold null).
- Array consts dual-register in module_const_map (value = the literal
node) so the folders see elements; bare reads still hit the GLOBAL
arm first, so no double emission.
- Untyped consts whose RHS is a const-aggregate leaf ('L :: K.len',
'E :: K[1]', 'R :: LIT.r') register in a pass 2b AFTER aggregates,
gated on the receiver naming a const aggregate — a namespaced member
('F :: m.PI_ISH') is never mis-typed by the count placeholder.
Examples: 0179 (folds in dims + const exprs), 1163 (OOB diagnostic).
This commit is contained in:
@@ -785,6 +785,30 @@ pub fn scanDecls(self: *Lowering, decls: []const *const Node) void {
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
// Pass 2b: untyped consts whose RHS is a CONST-AGGREGATE leaf
|
||||
// (`L :: K.len`, `E :: K[1]`, `R :: LIT.r`) register with the count
|
||||
// placeholder so the folders reach them. Runs AFTER pass 2 so the
|
||||
// aggregate (array const / struct const) is registered regardless of
|
||||
// declaration order; gated on the receiver naming a const aggregate so
|
||||
// a namespaced member (`F :: m.PI_ISH`) is never mis-typed.
|
||||
for (decls) |decl| {
|
||||
if (decl.data != .const_decl) continue;
|
||||
const cd = decl.data.const_decl;
|
||||
if (cd.type_annotation != null) continue;
|
||||
self.setCurrentSourceFile(decl.source_file);
|
||||
const obj: *const Node = switch (cd.value.data) {
|
||||
.field_access => |fa| fa.object,
|
||||
.index_expr => |ie| ie.object,
|
||||
else => continue,
|
||||
};
|
||||
if (obj.data != .identifier) continue;
|
||||
const recv_is_agg = switch (self.selectModuleConst(obj.data.identifier.name)) {
|
||||
.resolved => |sel| sel.info.value.data == .array_literal or sel.info.value.data == .struct_literal,
|
||||
.own_opaque, .ambiguous, .none => false,
|
||||
};
|
||||
if (!recv_is_agg) continue;
|
||||
self.putModuleConst(decl.source_file, cd.name, .{ .value = cd.value, .ty = .s64 });
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a typed module-level value constant (`AF_INET :s32: 2`). Run in
|
||||
@@ -966,6 +990,11 @@ pub fn registerConstArrayGlobal(self: *Lowering, cd: *const ast.ConstDecl) void
|
||||
.is_const = true,
|
||||
});
|
||||
self.putGlobal(self.current_source_file, cd.name, .{ .id = gid, .ty = arr_ty });
|
||||
// ALSO register as a module const so the comptime folders see the
|
||||
// elements (`K.len` / `K[<const idx>]` in dims and const exprs).
|
||||
// Bare value reads still hit the GLOBAL arm first (identifier arm
|
||||
// order), so this never double-emits.
|
||||
self.putModuleConst(self.current_source_file, cd.name, .{ .value = cd.value, .ty = arr_ty });
|
||||
}
|
||||
|
||||
/// Infer `[N]T` for an untyped array-literal constant. Element types unify:
|
||||
|
||||
Reference in New Issue
Block a user