feat(lang): backtick raw identifier in every binding form + raw-not-a-type + foreign reserved-name fn bare-call [F0.6]
Completes the issue-0089 backtick raw-identifier / `#import c` exemption across all remaining identifier positions and closes three boundary gaps the F0.6 review found. 1. Exhaustive raw-binding coverage. The `is_raw` bit now threads through `ast.Identifier` and EVERY binding/capture form — `IfExpr`/`WhileExpr` optional bindings, `ForExpr` capture + index, `MatchArm` capture, `CatchExpr`/`OnFailStmt` tag bindings, `DestructureDecl` per-name, and the protocol-default-body / foreign-class method param lists — not just `var_decl`/`param`. `UnknownTypeChecker` skips the reserved-name check at each arm when raw, so a backtick works in every identifier position while a bare reserved spelling still errors (issue 0076 preserved). 2. Raw identifier is never a type. `parseTypeExpr`'s atom rejects a raw identifier in type position (`x : `s2 = 1`, `List(`s2)`) with an accurate diagnostic instead of silently type-classifying it. 3. Reserved-name function bare-callable. A bare `s2(4)` parses its callee as a `.type_expr` (reserved spelling); `lowerCall` now rewrites a type_expr callee to an identifier when a function of that name is in scope, so a backtick-declared sx fn and a `#import c` foreign fn whose C name collides with a reserved type spelling both resolve by their bare name. (`TypeName(val)` is not a cast, so there is no ambiguity.) Tests: examples/0152 (every control-flow/capture form + bare ref/call/member access), examples/1054 (catch/onfail tag bindings), examples/1139 (raw in type position rejected), examples/1220 extended (foreign reserved-name function bare-call). 0076 negatives 1119/1121/1122/1123/1124/1125 stay green. Gate: zig build + zig build test + 422 examples pass. specs.md + readme.md updated; issues/0089 RESOLVED banner refreshed.
This commit is contained in:
57
examples/0152-types-backtick-control-flow.sx
Normal file
57
examples/0152-types-backtick-control-flow.sx
Normal file
@@ -0,0 +1,57 @@
|
||||
// Backtick raw identifier across every control-flow / capture / binding form,
|
||||
// plus bare later uses. A reserved type-name spelling (`s2`, `u8`, …) works as a
|
||||
// binding name in a destructure, an `if`/`while` optional binding, a `for`
|
||||
// capture + index, and a match-arm capture; a backtick-named function is
|
||||
// bare-callable; and a backtick struct field is bare- or backtick-accessible.
|
||||
// The escape is needed only at the binding site — a later BARE reference / call
|
||||
// / member access resolves to the binding. A *bare* binding name is still the
|
||||
// reserved type (see examples/1121), so the escape is the only way to spell
|
||||
// these as values.
|
||||
// Regression (issue 0089 — attempt-2 completeness across binding forms).
|
||||
#import "modules/std.sx";
|
||||
|
||||
pair :: () -> (s64, s64) { (1, 2) }
|
||||
maybe :: () -> ?s64 { return 42; }
|
||||
|
||||
// Function named with a reserved spelling — bare-callable (no backtick at call).
|
||||
`s2 :: (n: s64) -> s64 { return n + 1; }
|
||||
|
||||
Quad :: struct { `s1: s32; `s2: s32; }
|
||||
|
||||
main :: () -> s32 {
|
||||
// destructure binding names
|
||||
`u8, rest := pair();
|
||||
print("dstr = {} {}\n", `u8, rest);
|
||||
|
||||
// if optional binding + bare-position reference inside the branch
|
||||
if `s16 := maybe() {
|
||||
print("if = {}\n", `s16);
|
||||
}
|
||||
|
||||
// while optional binding (name only — the while binding isn't body-exposed)
|
||||
while `s32 := maybe() {
|
||||
break;
|
||||
}
|
||||
|
||||
// for capture + index names
|
||||
xs := [3]s64.{ 10, 20, 30 };
|
||||
for xs: (`bool, `u16) {
|
||||
print("for = {} @ {}\n", `bool, `u16);
|
||||
}
|
||||
|
||||
// match-arm capture
|
||||
opt: ?s64 = 5;
|
||||
m := if opt == {
|
||||
case .some: (`string) { `string * 2 }
|
||||
case .none: { 0 }
|
||||
};
|
||||
print("match = {}\n", m);
|
||||
|
||||
// backtick function called BARE and via backtick — both resolve to the fn
|
||||
print("call = {} {}\n", s2(10), `s2(10));
|
||||
|
||||
// struct field named with a reserved spelling: bare + backtick member access
|
||||
q := Quad.{ `s1 = 7, `s2 = 9 };
|
||||
print("field = {} {} | {} {}\n", q.s1, q.s2, q.`s1, q.`s2);
|
||||
return 0;
|
||||
}
|
||||
40
examples/1054-errors-backtick-reserved-binding.sx
Normal file
40
examples/1054-errors-backtick-reserved-binding.sx
Normal file
@@ -0,0 +1,40 @@
|
||||
// Backtick raw identifier as the error-tag binding of `catch` and `onfail`. A
|
||||
// reserved type-name spelling (`s2`, `u8`) is a value name when backticked, so
|
||||
// it is accepted as the tag binding and a later reference resolves to it. A
|
||||
// *bare* reserved spelling in the same position is still rejected (see
|
||||
// examples/1123), so the backtick escape is the only way to spell these tags.
|
||||
// Regression (issue 0089 — attempt-2 catch/onfail coverage).
|
||||
#import "modules/std.sx";
|
||||
|
||||
E :: error { Bad, Empty }
|
||||
|
||||
parse :: (n: s32) -> (s32, !E) {
|
||||
if n < 0 { raise error.Bad; }
|
||||
if n == 0 { raise error.Empty; }
|
||||
return n * 2;
|
||||
}
|
||||
|
||||
// `catch` tag binding spelled `s2`, referenced in the match body.
|
||||
classify :: (n: s32) -> s32 {
|
||||
return parse(n) catch `s2 == {
|
||||
case .Bad: 1;
|
||||
case .Empty: 2;
|
||||
else: 3
|
||||
};
|
||||
}
|
||||
|
||||
// `onfail` tag binding spelled `u8`, referenced in the cleanup body.
|
||||
cleanup :: (n: s32) -> !E {
|
||||
onfail `u8 { if `u8 == error.Bad { print("cleanup: bad\n"); } }
|
||||
if n < 0 { raise error.Bad; }
|
||||
return;
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
print("classify(-1) = {}\n", classify(-1));
|
||||
print("classify(0) = {}\n", classify(0));
|
||||
print("classify(5) = {}\n", classify(5));
|
||||
c := cleanup(-1);
|
||||
print("done\n");
|
||||
return 0;
|
||||
}
|
||||
12
examples/1139-diagnostics-backtick-raw-not-a-type.sx
Normal file
12
examples/1139-diagnostics-backtick-raw-not-a-type.sx
Normal file
@@ -0,0 +1,12 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -7,3 +7,7 @@ int ffi_pick(int s1, int s2, int which) {
|
||||
int ffi_sum(int s1, int s2) {
|
||||
return s1 + s2;
|
||||
}
|
||||
|
||||
int s2(int u8) {
|
||||
return u8 + 100;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* Foreign C declarations whose parameter names (`s1`, `s2`) collide with
|
||||
sx's reserved signed-int type spellings. The `#import c` exemption must
|
||||
accept these generated names unedited (issue 0089). */
|
||||
/* Foreign C declarations whose names collide with sx's reserved type spellings.
|
||||
The `#import c` exemption must accept these generated names unedited, both as
|
||||
parameter names (`s1`, `s2`) and as a FUNCTION name (`s2`) — and a foreign
|
||||
reserved-name function must be bare-callable (issue 0089). */
|
||||
int ffi_pick(int s1, int s2, int which);
|
||||
int ffi_sum(int s1, int s2);
|
||||
int s2(int u8);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
// `#import c` foreign-name exemption: a C header's parameter names `s1`/`s2`
|
||||
// collide with sx's reserved signed-int type spellings. Foreign decls are
|
||||
// treated as RAW — their names are never type-classified nor reserved-checked
|
||||
// — so the generated `#foreign` bindings import and call without hand-edits
|
||||
// (no backticks needed). Before issue 0089 this errored with "'s1' is a
|
||||
// reserved type name and cannot be used as an identifier".
|
||||
// `#import c` foreign-name exemption: C names that collide with sx's reserved
|
||||
// type spellings import unedited. Foreign decls are treated as RAW — their names
|
||||
// are never type-classified nor reserved-checked — so the generated `#foreign`
|
||||
// bindings import and call without hand-edits (no backticks needed). This covers
|
||||
// parameter names (`s1`/`s2`), a function whose own NAME is a reserved spelling
|
||||
// (`s2`), and bare-calling that function (its callee spelling parses as a type
|
||||
// but resolves to the foreign fn). Before issue 0089 the params errored with
|
||||
// "'s1' is a reserved type name and cannot be used as an identifier", and the
|
||||
// bare call errored with "unresolved 's2'".
|
||||
// Regression (issue 0089).
|
||||
#import "modules/std.sx";
|
||||
|
||||
@@ -16,5 +19,6 @@ main :: () -> s32 {
|
||||
print("pick(10,20,0) = {}\n", ffi_pick(10, 20, 0));
|
||||
print("pick(10,20,1) = {}\n", ffi_pick(10, 20, 1));
|
||||
print("sum(10,20) = {}\n", ffi_sum(10, 20));
|
||||
print("s2(4) bare = {}\n", s2(4));
|
||||
0
|
||||
}
|
||||
|
||||
1
examples/expected/0152-types-backtick-control-flow.exit
Normal file
1
examples/expected/0152-types-backtick-control-flow.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
dstr = 1 2
|
||||
if = 42
|
||||
for = 10 @ 0
|
||||
for = 20 @ 1
|
||||
for = 30 @ 2
|
||||
match = 10
|
||||
call = 11 11
|
||||
field = 7 9 | 7 9
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
classify(-1) = 1
|
||||
classify(0) = 2
|
||||
classify(5) = 10
|
||||
cleanup: bad
|
||||
done
|
||||
@@ -0,0 +1 @@
|
||||
1
|
||||
@@ -0,0 +1,5 @@
|
||||
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;
|
||||
| ^^
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pick(10,20,0) = 10
|
||||
pick(10,20,1) = 20
|
||||
sum(10,20) = 30
|
||||
s2(4) bare = 104
|
||||
|
||||
Reference in New Issue
Block a user