Files
sx/examples/0904-optionals-any-to-string-optional.sx
agra f49a49cd07 fix(diagnostics): reject reserved/builtin type names used as identifiers (issue 0076)
A value binding (local/global `var` or a parameter) spelled as a
reserved/builtin type name parses as a `.type_expr` rather than an
`.identifier` (parser.zig, via `Type.fromName`), so the address-of
family in lower.zig never saw a scoped local and mis-lowered it —
loading the aggregate and passing it by value to a `ptr` parameter
(LLVM verifier abort, or a silent `*self`-mutation-losing copy).

Add a declaration-site diagnostic in semantic_diagnostics.zig
(`UnknownTypeChecker.checkBindingName`): reject any parameter name or
`var` binding name (`:=` / typed-local / global forms) whose spelling
collides with a reserved type name. `isReservedTypeName` defers to the
parser's own classifier (`types.Type.fromName`) so the rejected set
never drifts from the set that would parse as a type — the named
builtins (bool/string/void/f32/f64/usize/isize/Any) and `[su]N` over
sx's 1-64 range. Bare value names (`s`, `self`, `index`) are untouched.
No lowering special-case; the `.identifier`-only address-of paths are
correct once type-shaped names can never be bound. The rejected
attempt-1 `bareVarName` approach was never landed.

Tests:
- 0125-types-type-named-var-rejected: `:=` form (s2) rejected
  (repurposed from the old test that asserted the now-illegal behavior).
- 1119-diagnostics-reserved-type-name-as-identifier: parameter (u8),
  typed-local (s64, bool), `:=` (string) forms rejected.
- 0135-types-self-streaming-nonreserved: positive — `*self` streaming
  with non-reserved names accumulates correctly via both call styles.
- 0904-optionals: renamed incidental locals s1/s2 -> filled/empty.
2026-06-03 19:00:39 +03:00

32 lines
1.1 KiB
Plaintext

// any_to_string didn't handle optionals. A struct field of type `?T`
// printed as `<?>` (any_to_string's "no case matched" default)
// because there was no `case optional:` arm, and no dispatch table
// entry mapping `?T` TypeIds to the optional category.
//
// The variadic auto-unwrap path (packVariadicCallArgs) papered over
// this for direct `print("{}\n", opt)` calls — it stringified
// optionals to either the inner value's repr or `"null"` BEFORE
// boxing as Any. But anywhere else that boxes an optional and reads
// it back through any_to_string (struct field printing,
// `xx opt : Any`, future user code) hit the `<?>` floor.
//
// Locks in the fix: each ?T variant routes through `case optional:`
// → `optional_to_string(cast(type) val)` → either the inner value's
// `any_to_string` representation or the literal `"null"`.
#import "modules/std.sx";
S :: struct {
a: ?s64;
b: ?string;
c: ?bool;
}
main :: () {
filled := S.{ a = 42, b = "hi", c = true };
print("{}\n", filled);
empty := S.{ a = null, b = null, c = null };
print("{}\n", empty);
0;
}