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.
This commit is contained in:
13
examples/0125-types-type-named-var-rejected.sx
Normal file
13
examples/0125-types-type-named-var-rejected.sx
Normal file
@@ -0,0 +1,13 @@
|
||||
// A local declared with a reserved/builtin type-name spelling (`s2` is the
|
||||
// arbitrary-width `sN` integer type) is rejected at the declaration site.
|
||||
// Previously such a name parsed as a `.type_expr`, so address-of sites
|
||||
// mis-lowered it (load-by-value to a `ptr` param → LLVM verifier abort, or a
|
||||
// silent `*self`-mutation-losing copy). Regression (issue 0076). Expected:
|
||||
// error at the declaration; exit 1.
|
||||
#import "modules/std.sx";
|
||||
|
||||
main :: () -> s32 {
|
||||
s2 := 42;
|
||||
print("s2: {}\n", s2);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#import "modules/std.sx";
|
||||
#import "modules/math/math.sx";
|
||||
#import "modules/compiler.sx";
|
||||
#import "modules/test.sx";
|
||||
pkg :: #import "modules/testpkg";
|
||||
|
||||
main :: () {
|
||||
|
||||
// ========================================================
|
||||
// 21. TYPE-NAMED VARIABLES (s2, u8, etc.)
|
||||
// ========================================================
|
||||
print("=== 21. Type-Named Vars ===\n");
|
||||
{
|
||||
s2 := 42;
|
||||
print("s2: {}\n", s2);
|
||||
s2 = s2 + 1;
|
||||
print("s2+1: {}\n", s2);
|
||||
}
|
||||
}
|
||||
30
examples/0135-types-self-streaming-nonreserved.sx
Normal file
30
examples/0135-types-self-streaming-nonreserved.sx
Normal file
@@ -0,0 +1,30 @@
|
||||
// A `*self`-mutating streaming pattern with NON-reserved binding names
|
||||
// (`hasher`, `ctx`) compiles and accumulates state correctly through BOTH
|
||||
// call styles — explicit address-of `update(@h, ...)` and autoref
|
||||
// `h.update(...)` — across multiple mutating calls. Proves the
|
||||
// `.identifier`-only address-of paths in lowering are correct as-is, with no
|
||||
// type-shaped-name special-case (companion to the issue-0076 rejection of
|
||||
// type-named identifiers).
|
||||
#import "modules/std.sx";
|
||||
|
||||
Hasher :: struct { total: s64 = 0; count: s64 = 0; }
|
||||
|
||||
update :: (self: *Hasher, n: s64) {
|
||||
self.total += n;
|
||||
self.count += 1;
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
hasher := Hasher.{ total = 0, count = 0 };
|
||||
update(@hasher, 10); // explicit address-of receiver
|
||||
hasher.update(20); // autoref receiver
|
||||
update(@hasher, 30);
|
||||
hasher.update(40);
|
||||
print("hasher total={} count={}\n", hasher.total, hasher.count);
|
||||
|
||||
ctx := Hasher.{ total = 100, count = 0 };
|
||||
ctx.update(5);
|
||||
update(@ctx, 7);
|
||||
print("ctx total={} count={}\n", ctx.total, ctx.count);
|
||||
return 0;
|
||||
}
|
||||
@@ -23,9 +23,9 @@ S :: struct {
|
||||
}
|
||||
|
||||
main :: () {
|
||||
s1 := S.{ a = 42, b = "hi", c = true };
|
||||
print("{}\n", s1);
|
||||
s2 := S.{ a = null, b = null, c = null };
|
||||
print("{}\n", s2);
|
||||
filled := S.{ a = 42, b = "hi", c = true };
|
||||
print("{}\n", filled);
|
||||
empty := S.{ a = null, b = null, c = null };
|
||||
print("{}\n", empty);
|
||||
0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// A value binding (parameter or local `var`) spelled as a reserved/builtin
|
||||
// type name is rejected at the declaration site, across every declaration
|
||||
// form: a parameter name (`u8`), a typed local (`s64`, `bool`), and a `:=`
|
||||
// local (`string`). Such a spelling parses as a `.type_expr` rather than an
|
||||
// `.identifier`, so the address-of family in lowering mis-lowers it (issue
|
||||
// 0076). Expected: one error per offending name; exit 1.
|
||||
#import "modules/std.sx";
|
||||
|
||||
takes_u8 :: (u8: s32) -> s32 { return u8; }
|
||||
|
||||
main :: () -> s32 {
|
||||
s64 : s32 = 3;
|
||||
bool : bool = true;
|
||||
string := "x";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,5 @@
|
||||
error: 's2' is a reserved type name and cannot be used as an identifier
|
||||
--> /Users/agra/projects/sx/examples/0125-types-type-named-var-rejected.sx:10:5
|
||||
|
|
||||
10 | s2 := 42;
|
||||
| ^^^^^^^^^
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
=== 21. Type-Named Vars ===
|
||||
s2: 42
|
||||
s2+1: 43
|
||||
@@ -0,0 +1,2 @@
|
||||
hasher total=100 count=4
|
||||
ctx total=112 count=2
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,23 @@
|
||||
error: 'u8' is a reserved type name and cannot be used as an identifier
|
||||
--> /Users/agra/projects/sx/examples/1119-diagnostics-reserved-type-name-as-identifier.sx:9:14
|
||||
|
|
||||
9 | takes_u8 :: (u8: s32) -> s32 { return u8; }
|
||||
| ^^
|
||||
|
||||
error: 's64' is a reserved type name and cannot be used as an identifier
|
||||
--> /Users/agra/projects/sx/examples/1119-diagnostics-reserved-type-name-as-identifier.sx:12:5
|
||||
|
|
||||
12 | s64 : s32 = 3;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: 'bool' is a reserved type name and cannot be used as an identifier
|
||||
--> /Users/agra/projects/sx/examples/1119-diagnostics-reserved-type-name-as-identifier.sx:13:5
|
||||
|
|
||||
13 | bool : bool = true;
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: 'string' is a reserved type name and cannot be used as an identifier
|
||||
--> /Users/agra/projects/sx/examples/1119-diagnostics-reserved-type-name-as-identifier.sx:14:5
|
||||
|
|
||||
14 | string := "x";
|
||||
| ^^^^^^^^^^^^^^
|
||||
Reference in New Issue
Block a user