feat(lang): universal backtick raw identifier — valid in value, decl, AND type position [F0.6]

AGRA ruling (attempt 4): `` `name `` is THE LITERAL identifier `name`, usable in
EVERY position — the backtick only means "treat this token as a plain identifier,
never the reserved keyword/type", and is never part of the name's text.

- Raw in TYPE position is now VALID (reverses attempt-2 "raw is not a type"):
  `parseTypeExpr` emits a raw `type_expr`; `TypeResolver.resolveNamed` gains a
  `skip_builtin` flag (threaded from `te.is_raw` via lower.zig + type_bridge) so a
  `` `s2 `` reference resolves to a `` `s2 ``-declared type (struct/enum/union/alias),
  else a normal "unknown type 's2'" error (reportIfUnknownType skips the builtin
  exemption when raw). Bare `s2` in type position stays the builtin int.
- Every declaration-name site is is_raw-exemptible: `is_raw` added to TypeExpr +
  StructDecl/EnumDecl/UnionDecl/ErrorSetDecl/ProtocolDecl/ForeignClassDecl/UfcsAlias/
  NamespaceDecl/ImportDecl/CImportDecl/LibraryDecl; parser threads name_is_raw to
  every decl parse fn; namespace imports carry it through imports.addNamespace.
  Typed-const path (`` `s2 : s64 : 5 ``) now threads name_span+is_raw (fixes the
  1:1-caret bug).
- Check<->exemption made structurally symmetric: checkBindingName/checkDeclName take
  is_raw as a REQUIRED argument and skip inside the check, so no call site can
  validate a name without honoring the exemption (the desync cause of prior rounds).
- Bare reserved-name declarations of every kind still error (0076 preserved);
  `#import c` foreign names stay auto-raw + bare-callable.

specs.md + readme.md updated to the universal model. issue 0089 RESOLVED banner
rewritten. Examples: replace 1139 (raw-not-a-type) with 0154 (raw type reference);
add 0155 (typed const + union tag) and 1141 (bare type-decl negatives).
Gate: zig build + zig build test + run_examples (426 passed, 0 failed).
This commit is contained in:
agra
2026-06-04 20:27:53 +03:00
parent c0e1a5db82
commit 023971cae5
26 changed files with 441 additions and 212 deletions

View File

@@ -1,10 +1,12 @@
// Backtick raw-identifier escape: a leading backtick makes the following
// identifier RAW — its text excludes the backtick and it is NEVER
// type-classified, so a reserved type-name spelling (`s2`, `u8`, …) can be
// used as a value identifier. Exercised in every position: global, local,
// param, struct field + member access, function name + call, and a later
// reference. A *bare* `s2` is still the reserved type name (see
// examples/1119), so the escape is the only way to spell these as values.
// identifier RAW — its text excludes the backtick and it is never the
// reserved/builtin keyword, so a reserved type-name spelling (`s2`, `u8`, …)
// can be used as an ordinary identifier. Exercised in every VALUE position:
// global, local, param, struct field + member access, function name + call,
// and a later reference. (A raw identifier in TYPE position references a
// backtick-declared type instead — see examples/0154.) A *bare* `s2` is still
// the reserved type name (see examples/1119), so the escape is the only way to
// spell these as values.
// Regression (issue 0089).
#import "modules/std.sx";

View File

@@ -0,0 +1,42 @@
// Backtick raw identifier in TYPE position (the universal model, issue 0089):
// `` `name `` is the LITERAL identifier `name` used as a type reference, never
// the builtin/reserved spelling. A reserved type spelling (`s2`, `u8`, …) can
// therefore both DECLARE a type (struct / enum / union / error-set / alias) and
// be REFERENCED as that type via the backtick — while a BARE `s2` in type
// position remains the signed-int type (see `add` below) and a bare reserved-
// name declaration still errors (see examples/1141). The backtick is required
// to declare or reference these names; it is never part of the name's text.
// Regression (issue 0089 — attempt-4 universal raw identifier).
#import "modules/std.sx";
// Type-introducing decls whose NAME is a reserved spelling.
`s2 :: struct { x: s64; }
`s8 :: enum { A; B; }
`u16 :: union { i: s32; f: f32; }
`u32 :: error { Bad, Empty }
RawAlias :: `s2; // alias to a backtick-declared struct
// A bare `s2` in type position is still the 2-bit signed int.
add :: (a: s2, b: s2) -> s2 { return a + b; }
main :: () -> s32 {
// Reference the backtick struct as a type; field access works.
v : `s2 = ---;
v.x = 7;
// Reference via a normal alias too.
a : RawAlias = ---;
a.x = 11;
// Backtick enum / union type references.
e : `s8 = .A;
u : `u16 = ---;
u.i = 5;
print("struct = {}\n", v.x);
print("alias = {}\n", a.x);
print("enum = {}\n", e == .A);
print("union = {}\n", u.i);
print("bare = {}\n", add(1, 0)); // bare s2 = the 2-bit int type
return 0;
}

View File

@@ -0,0 +1,24 @@
// Backtick raw identifier at the two remaining binding positions (issue 0089,
// attempt-4): a TYPED constant (`` `s2 : s64 : 5 ``) and a union TAG / field
// (`` `s2: s32 ``). The typed-const form previously slipped past the decl check
// without a name span (caret at 1:1); a bare `s2 : s64 : 5` is still rejected
// with the caret ON the name (see examples/1141). A union tag spelled with a
// reserved name works and is accessible bare or backticked.
// Regression (issue 0089 — attempt-4 typed const + union tag).
#import "modules/std.sx";
// Typed constant whose name is a reserved type spelling.
`s2 : s64 : 5;
// Union whose tags are reserved type spellings.
Mix :: union { `s1: s32; `u8: f32; }
main :: () -> s32 {
print("typed const = {}\n", `s2);
m : Mix = ---;
m.`s1 = 42;
print("union tick = {}\n", m.`s1); // backtick member access
print("union bare = {}\n", m.s1); // bare member access — same field
return 0;
}

View File

@@ -1,12 +0,0 @@
// A backtick raw identifier is a VALUE-name escape; it is never a type. Using
// one in type position (`x : `s2 = 1`) is a clean parse error, not a silent
// type-classification — reserved type names are the lowercase `sN`/`uN`/`fNN`
// spellings, and a real type never needs a backtick. A *bare* `s2` in type
// position remains the reserved signed-int type.
// Regression (issue 0089 — attempt-2: raw identifier rejected in type position).
#import "modules/std.sx";
main :: () -> s32 {
x : `s2 = 1;
return 0;
}

View File

@@ -0,0 +1,22 @@
// A reserved/builtin type-name spelling is rejected as the NAME of EVERY
// type-introducing `::` declaration too — struct, enum, union, error-set, and
// a typed constant — not just `:=` / value-const / function names (those are
// examples/1140). Each is a declaration-name binding site: a bare reserved
// spelling there mis-classifies and is rejected, exactly like `s2 := …`. The
// backtick escape (`` `s2 :: struct{…} ``, examples/0154) is the only way to
// spell these names in handwritten sx; `#import c` foreign decls stay exempt
// (examples/1220).
//
// Regression (issue 0089 — attempt-4: 0076 holds across every decl kind).
// Expected: one error per declaration, each caret ON the declared name; exit 1.
#import "modules/std.sx";
s8 :: struct { v: s64; }
s16 :: enum { A; B; }
u16 :: union { a: s32; b: f32; }
u32 :: error { Bad, Empty }
s2 : s64 : 5;
main :: () -> s32 {
return 0;
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,5 @@
struct = 7
alias = 11
enum = true
union = 5
bare = 1

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,3 @@
typed const = 5
union tick = 42
union bare = 42

View File

@@ -1,5 +0,0 @@
error: `s2` is a raw identifier, not a type — the backtick escape names a value, never a type
--> examples/1139-diagnostics-backtick-raw-not-a-type.sx:10:10
|
10 | x : `s2 = 1;
| ^^

View File

@@ -0,0 +1,29 @@
error: 's8' is a reserved type name and cannot be used as an identifier
--> examples/1141-diagnostics-reserved-name-type-decl.sx:14:1
|
14 | s8 :: struct { v: s64; }
| ^^
error: 's16' is a reserved type name and cannot be used as an identifier
--> examples/1141-diagnostics-reserved-name-type-decl.sx:15:1
|
15 | s16 :: enum { A; B; }
| ^^^
error: 'u16' is a reserved type name and cannot be used as an identifier
--> examples/1141-diagnostics-reserved-name-type-decl.sx:16:1
|
16 | u16 :: union { a: s32; b: f32; }
| ^^^
error: 'u32' is a reserved type name and cannot be used as an identifier
--> examples/1141-diagnostics-reserved-name-type-decl.sx:17:1
|
17 | u32 :: error { Bad, Empty }
| ^^^
error: 's2' is a reserved type name and cannot be used as an identifier
--> examples/1141-diagnostics-reserved-name-type-decl.sx:18:1
|
18 | s2 : s64 : 5;
| ^^