diff --git a/examples/0171-types-undeclared-type-in-generic-struct-field.sx b/examples/0171-types-undeclared-type-in-generic-struct-field.sx new file mode 100644 index 0000000..82a9ad7 --- /dev/null +++ b/examples/0171-types-undeclared-type-in-generic-struct-field.sx @@ -0,0 +1,29 @@ +// A genuinely-undeclared type name used as a field type inside a MAIN-file +// GENERIC struct must emit a clean "unknown type" diagnostic, not silently +// compile. +// +// The `UnknownTypeChecker` used to SKIP generic structs entirely ("their field +// types reference the struct's own `$T`, resolved at instantiation"). That skip +// was too broad: a field type like `bad: MissingType` — which is NOT a type +// param and names no declared type — fell through the type leaf's empty-struct +// stub and the struct silently compiled, mis-sizing every downstream load. +// +// The checker now walks generic-struct fields with the struct's own type params +// (`$T`) in scope: `good: T` resolves (it IS a param) while `bad: MissingType` +// is reported. A value-param position (a `Vector` lane count, a `$N: u32` arg) +// is still skipped, so a valid generic struct keeps compiling unchanged. +// +// Expected: `error: unknown type 'MissingType'` pointing at the field; exit 1. +// Regression (stdlib E3). +#import "modules/std.sx"; + +Box :: struct($T: Type) { + good: T; + bad: MissingType; +} + +main :: () -> s32 { + b : Box(s64) = .{ good = 7, bad = 0 }; + print("{}\n", b.good); + return 0; +} diff --git a/examples/expected/0171-types-undeclared-type-in-generic-struct-field.exit b/examples/expected/0171-types-undeclared-type-in-generic-struct-field.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/examples/expected/0171-types-undeclared-type-in-generic-struct-field.exit @@ -0,0 +1 @@ +1 diff --git a/examples/expected/0171-types-undeclared-type-in-generic-struct-field.stderr b/examples/expected/0171-types-undeclared-type-in-generic-struct-field.stderr new file mode 100644 index 0000000..6685e51 --- /dev/null +++ b/examples/expected/0171-types-undeclared-type-in-generic-struct-field.stderr @@ -0,0 +1,5 @@ +error: unknown type 'MissingType' + --> examples/0171-types-undeclared-type-in-generic-struct-field.sx:22:10 + | +22 | bad: MissingType; + | ^^^^^^^^^^^ diff --git a/examples/expected/0171-types-undeclared-type-in-generic-struct-field.stdout b/examples/expected/0171-types-undeclared-type-in-generic-struct-field.stdout new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/expected/0171-types-undeclared-type-in-generic-struct-field.stdout @@ -0,0 +1 @@ + diff --git a/src/ir/semantic_diagnostics.zig b/src/ir/semantic_diagnostics.zig index 9dacdb4..fb936bb 100644 --- a/src/ir/semantic_diagnostics.zig +++ b/src/ir/semantic_diagnostics.zig @@ -490,10 +490,14 @@ pub const UnknownTypeChecker = struct { } fn checkStructFieldTypes(self: UnknownTypeChecker, sd: *const ast.StructDecl, declared: *std.StringHashMap(void)) void { - // Generic struct fields reference the struct's own type params ($T) — - // resolved at instantiation, not here. - if (sd.type_params.len != 0) return; - for (sd.field_types) |ft| self.checkTypeNodeForUnknown(ft, declared, &.{}, &.{}); + // A generic struct's field types may reference its own type params + // (`$T`, `$N`, the `..$Ts` pack) — those are IN SCOPE here, so pass them + // through rather than skipping the whole decl. Skipping silently let a + // genuinely-undeclared field type (`bad: MissingType`) fall through the + // type leaf's empty-struct stub and compile (stdlib E3). A value-param + // position (a `Vector` lane count, a `$N: u32` arg) is still skipped + // inside `checkTypeNodeForUnknown` / `isValueParamPosition`. + for (sd.field_types) |ft| self.checkTypeNodeForUnknown(ft, declared, sd.type_params, &.{}); } fn checkFnSignatureTypes(self: UnknownTypeChecker, fd: *const ast.FnDecl, declared: *std.StringHashMap(void)) void { @@ -592,7 +596,15 @@ pub const UnknownTypeChecker = struct { if (cd.type_annotation) |ta| self.checkTypeNodeForUnknown(ta, declared, in_scope.items, type_vals.items); self.walkBodyTypes(cd.value, declared, in_scope, type_vals); }, - .struct_decl => |sd| if (sd.type_params.len == 0) { + .struct_decl => |sd| { + // A body-local struct's own type params (`$T`) join the enclosing + // scope's in-scope params (so a local generic struct can name both + // the outer fn's `$T` and its own); any OTHER bare field type is + // still a genuinely-undeclared type. Mirrors the top-level + // `checkStructFieldTypes` close of the generic-struct carveout. + const save = in_scope.items.len; + defer in_scope.shrinkRetainingCapacity(save); + for (sd.type_params) |tp| in_scope.append(self.alloc, tp) catch {}; for (sd.field_types) |ft| self.checkTypeNodeForUnknown(ft, declared, in_scope.items, type_vals.items); }, .call => |c| {