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:
agra
2026-06-08 10:02:33 +03:00
parent 81621703ca
commit 3816bfff47
6 changed files with 88 additions and 15 deletions

View File

@@ -0,0 +1,25 @@
// An IMPORTED generic template's field that names a type the CALLER declared
// only as a BLOCK-LOCAL must NOT bind that caller-local type — a block-local is
// visible only within its OWN source. `lib.sx` defines
// `Bad :: struct($T) { x: T; y: LocalOnly; }`; `main` declares `LocalOnly` only
// inside its own body before instantiating `Bad(s32)`. The imported template's
// module cannot see a caller block-local, so `y: LocalOnly` is undeclared in the
// lib file.
//
// Before the fix the global `local_type_names` set was source-insensitive: the
// template's field resolution (run in the template's source context, E3
// attempt-4) consulted it, found the caller's `LocalOnly`, and silently compiled
// (printed a value, exit 0). `local_type_names` is now keyed by declaring source,
// so a cross-source block-local no longer leaks into another source's resolution.
//
// Expected: `error: unknown type 'LocalOnly'` pointing into lib.sx; exit 1.
// Regression (stdlib E3 attempt-5).
#import "modules/std.sx";
#import "0762-modules-imported-generic-caller-local-field-leak/lib.sx";
main :: () -> s32 {
LocalOnly :: struct { v: s32; }
b : Bad(s32) = .{ x = 1, y = .{ v = 9 } };
print("{} {}\n", b.x, b.y.v);
return 0;
}

View File

@@ -0,0 +1,9 @@
// Flat-imported generic struct whose field `y: LocalOnly` names a type that is
// declared only as a BLOCK-LOCAL in the CALLER (`main`). The template's fields
// resolve in THIS module's source context (E3 attempt-4), and a block-local type
// is visible only within its own source, so `LocalOnly` is genuinely undeclared
// here — the source-aware leaf surfaces it instead of binding the caller's local.
Bad :: struct($T: Type) {
x: T;
y: LocalOnly;
}

View File

@@ -0,0 +1,5 @@
error: unknown type 'LocalOnly'
--> examples/0762-modules-imported-generic-caller-local-field-leak/lib.sx:8:8
|
8 | y: LocalOnly;
| ^^^^^^^^^

View File

@@ -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`.