Files
sx/examples/types/0158-types-reserved-name-member-exempt.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

56 lines
2.1 KiB
Plaintext

// Reserved-name MEMBER positions are EXEMPT from the reserved-type-name rule:
// a bare reserved spelling (`i2`, `u8`, `i1`, …) is legal as a struct FIELD
// name, a union TAG name, and a protocol METHOD-SIGNATURE name. These are
// unambiguous — the name sits in a member slot and is reached via `obj.name`
// (or dispatched by string), so it is never type-classified and never
// mislowers. The backtick form is optional there and resolves to the same
// member. Backtick access (`obj.`i2`) and bare access (`obj.i2`) both work.
//
// The exemption stops at member SIGNATURES: an `impl` method DEFINITION is a
// real function, so its name is a declaration site (like a free function) and a
// reserved spelling still needs the backtick (`` `i2 :: (self) ``) — bare would
// be type-classified and mislower (the issue-0076 protection). A bare reserved
// VALUE binding / declaration name still errors (see examples/1119, 1141, 1142).
// Regression (issue 0089 — attempt-7: pins the Agra-ruled member-name exemption).
#import "modules/std.sx";
// Struct fields spelled with reserved type names — bare is legal.
Holder :: struct {
i2: i64;
u8: i64;
}
// Union tags spelled with reserved type names — bare is legal.
Tag :: union {
i1: i32;
u16: f64;
}
// Protocol method SIGNATURE spelled with a reserved type name — bare is legal.
Speaker :: protocol {
i2 :: (self: *Self) -> i64;
}
Dog :: struct { n: i64; }
impl Speaker for Dog {
`i2 :: (self: *Dog) -> i64 { self.n } // impl DEFINITION → backtick required
}
main :: () -> i32 {
h := Holder.{ i2 = 10, u8 = 20 };
print("fields bare = {} {}\n", h.i2, h.u8); // bare member access
print("fields tick = {} {}\n", h.`i2, h.`u8); // backtick member access
h.i2 = 11;
h.`u8 = 21; // backtick write
print("fields set = {} {}\n", h.i2, h.u8);
t : Tag = ---;
t.i1 = 5;
print("union = {} {}\n", t.i1, t.`i1); // bare + backtick — same tag
items : List(Speaker) = .{};
items.append(Dog.{ n = 7 });
print("dispatch = {}\n", items.items[0].i2()); // bare reserved-name method call
return 0;
}