fix(diag): source-key local_type_names so a caller block-local can't leak into an imported template field [stdlib E3 attempt-5]
A block-local type is visible only within the source that declares it. The
global `local_type_names` set was source-insensitive, so an imported generic
template's field (resolved in the template's source context, attempt-4) could
bind a type the CALLER declared block-local — silently compiling an undeclared
imported field instead of diagnosing it.
Key `local_type_names` by declaring source. The bare-TYPE gate now resolves a
local only when the query originates in the local's own source (R2 preserved);
a same-name block-local of a DIFFERENT source routes to the undeclared path so
the leak surfaces (`unknown type '...'`, exit 1) instead of escaping via the
`registered` catch-all that would otherwise resolve the globally-registered
cross-source local.
Regression: examples/0762 — imported `Bad :: struct($T) { x: T; y: LocalOnly; }`
with `LocalOnly` declared only in the caller `main` now errors in lib.sx
(fail-before on 8162170 printed `1 9` exit 0).
This commit is contained in:
@@ -218,14 +218,18 @@ pub const Lowering = struct {
|
||||
/// gate would reject it) — so the bare TYPE leaf falls open here (F1).
|
||||
emitting_default_context: bool = false,
|
||||
/// Names declared as a BLOCK-LOCAL type (a `Foo :: struct/enum/union/error_set`
|
||||
/// or bare type-decl statement inside a fn / init body). A local type registers
|
||||
/// into the global type table and CLOBBERS a same-name top-level entry
|
||||
/// (`registerStructDecl`'s `findByName … orelse intern` + `updatePreservingKey`),
|
||||
/// so after it lowers the name IS the local type program-wide (single-author,
|
||||
/// pre-E2). The source-aware bare-TYPE gate consults this so a legitimately
|
||||
/// block-local type is never mistaken for a namespaced-only leak — even when a
|
||||
/// namespaced-only import happens to author a top-level type of the same name.
|
||||
local_type_names: std.StringHashMap(void) = std.StringHashMap(void).init(std.heap.page_allocator),
|
||||
/// or bare type-decl statement inside a fn / init body), keyed by the DECLARING
|
||||
/// source. A local type registers into the global type table and CLOBBERS a
|
||||
/// same-name top-level entry (`registerStructDecl`'s `findByName … orelse intern`
|
||||
/// + `updatePreservingKey`), so after it lowers the name IS the local type
|
||||
/// program-wide (single-author, pre-E2). The source-aware bare-TYPE gate consults
|
||||
/// this so a legitimately block-local type resolves in ITS OWN source (never
|
||||
/// mistaken for a namespaced-only leak, even when a namespaced-only import authors
|
||||
/// a same-name top-level type — R2). It is keyed by source because a local is
|
||||
/// visible ONLY within the source that declares it: an imported template's field
|
||||
/// resolution (run in the template's source context, E3 attempt-4) must NOT bind a
|
||||
/// name the CALLER declared block-local (E3 attempt-5).
|
||||
local_type_names: std.StringHashMap(std.StringHashMap(void)) = std.StringHashMap(std.StringHashMap(void)).init(std.heap.page_allocator),
|
||||
struct_defaults_map: std.StringHashMap([]const ?*const Node) = std.StringHashMap([]const ?*const Node).init(std.heap.page_allocator), // struct name → field defaults
|
||||
struct_instance_bindings: std.StringHashMap(std.StringHashMap(TypeId)) = std.StringHashMap(std.StringHashMap(TypeId)).init(std.heap.page_allocator), // mangled struct name → type param bindings
|
||||
struct_instance_template: std.StringHashMap([]const u8) = std.StringHashMap([]const u8).init(std.heap.page_allocator), // mangled struct name → template name
|
||||
@@ -1977,12 +1981,20 @@ pub const Lowering = struct {
|
||||
}
|
||||
|
||||
// 2. A block-local type (declared inside a fn / init body) clobbers the
|
||||
// global entry for its name, so `existing` IS that local type — never a
|
||||
// namespaced-only leak. Resolve it ungated (R2): a legitimately-scoped
|
||||
// local must not be rejected just because a namespaced-only import also
|
||||
// authors a top-level type of the same name.
|
||||
if (self.local_type_names.contains(name)) {
|
||||
// global entry for its name, so `existing` IS that local type. A local is
|
||||
// visible ONLY in its OWN source. Resolve it ungated when the query
|
||||
// originates in the local's source (R2): a legitimately-scoped local must
|
||||
// not be rejected just because a namespaced-only import also authors a
|
||||
// top-level type of the same name. When the same name is a block-local of a
|
||||
// DIFFERENT source — e.g. an imported template's field (resolved in the
|
||||
// template's source context, E3 attempt-4) naming a type the CALLER
|
||||
// declared block-local — the local is NOT visible here: route to the
|
||||
// undeclared path so the leak surfaces, never the `registered` catch-all
|
||||
// (arm 4) which would resolve the globally-registered cross-source local.
|
||||
if (self.localTypeInSource(from, name)) {
|
||||
if (registered) |existing| return .{ .resolved = existing };
|
||||
} else if (self.localTypeInAnySource(name)) {
|
||||
return self.forwardAliasOrUndeclared(name, from);
|
||||
}
|
||||
|
||||
// 3. Authored as a TYPE (named OR alias) in some module, but NOT flat-
|
||||
@@ -2184,9 +2196,29 @@ pub const Lowering = struct {
|
||||
}
|
||||
|
||||
/// Record a name declared as a BLOCK-LOCAL type so the bare-TYPE gate never
|
||||
/// mistakes it for a namespaced-only leak (see `local_type_names`).
|
||||
/// mistakes it for a namespaced-only leak (see `local_type_names`). Keyed by the
|
||||
/// declaring source (the function being lowered) so the local is visible only
|
||||
/// within that source.
|
||||
fn recordLocalTypeName(self: *Lowering, name: []const u8) void {
|
||||
self.local_type_names.put(name, {}) catch {};
|
||||
const src = self.current_source_file orelse self.main_file orelse return;
|
||||
const gop = self.local_type_names.getOrPut(src) catch return;
|
||||
if (!gop.found_existing) gop.value_ptr.* = std.StringHashMap(void).init(std.heap.page_allocator);
|
||||
gop.value_ptr.put(name, {}) catch {};
|
||||
}
|
||||
|
||||
/// TRUE iff `name` is a block-local type declared in `source`.
|
||||
fn localTypeInSource(self: *Lowering, source: []const u8, name: []const u8) bool {
|
||||
if (self.local_type_names.get(source)) |inner| return inner.contains(name);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// TRUE iff `name` is a block-local type declared in ANY source. A name that is a
|
||||
/// local SOMEWHERE but not in the querying source is a cross-source local — not
|
||||
/// visible from the querying source.
|
||||
fn localTypeInAnySource(self: *Lowering, name: []const u8) bool {
|
||||
var it = self.local_type_names.valueIterator();
|
||||
while (it.next()) |inner| if (inner.contains(name)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Resolve the bare TYPE leaf to a `TypeId` for `resolveTypeWithBindings`.
|
||||
|
||||
Reference in New Issue
Block a user