feat(lang): reserved-name check covers :: const/fn/type decls + scope call rewrite to raw provenance [F0.6]

A bare reserved-type-name `::` declaration was silently accepted, and the
attempt-2 lowerCall rewrite then made a bare `s2 :: (…) {…}` function callable —
bypassing the backtick rule for handwritten sx. The reserved-name binding check
covered `:=` / typed-local / param / captures but NOT the `::` declaration form.

- ast: `ConstDecl`/`FnDecl` carry `is_raw` + `name_span` threaded from the parser
  (parseConstBinding / parseFnDecl, all call sites incl. struct/impl methods).
- semantic_diagnostics: reject a bare reserved spelling at EVERY declaration-name
  site — const, function (incl. struct/impl methods), struct/enum/union/error-set,
  protocol, foreign-class, ufcs alias, namespaced/library/c-import name. Backtick
  (`is_raw`) and the compiler's `#builtin` definition (`string :: []u8 #builtin`)
  are the only exemptions; a value whose node is itself a named decl defers to
  that node's own check.
- c_import: synthesized foreign fn_decls are `is_raw = true`, so a C function
  whose own name collides with a reserved spelling (`int s2(int);`) imports and
  bare-calls unedited.
- lower: scope the `.type_expr`→`.identifier` call rewrite to a callee FnDecl of
  RAW provenance (`is_raw`) — only a backtick / `#import c` foreign fn can carry a
  reserved-name spelling, so a non-raw match never gets rewritten.
- examples: 0153 (positive — backtick `::` const + fn, bare + tick call), 1140
  (negative — bare `::` const + fn rejected).
- docs: specs.md + readme.md state the backtick is required at every binding site
  including `::` const / function / type declarations; issue 0089 banner updated.
This commit is contained in:
agra
2026-06-04 19:16:37 +03:00
parent 640f59dc54
commit c0e1a5db82
16 changed files with 232 additions and 62 deletions

View File

@@ -152,7 +152,7 @@ pub const Parser = struct {
// IDENT :: ...
if (self.current.tag == .colon_colon) {
self.advance();
return self.parseConstBinding(name, start);
return self.parseConstBinding(name, name_span, start, name_is_raw);
}
// IDENT : type : value; (typed constant)
@@ -173,7 +173,7 @@ pub const Parser = struct {
return self.fail("expected '::', ':=', or ':' after identifier");
}
fn parseConstBinding(self: *Parser, name: []const u8, start_pos: u32) anyerror!*Node {
fn parseConstBinding(self: *Parser, name: []const u8, name_span: ast.Span, start_pos: u32, name_is_raw: bool) anyerror!*Node {
// After `::`
// Could be: #run expr, enum { ... }, (params) -> type { body }, or expr;
@@ -215,7 +215,7 @@ pub const Parser = struct {
const inner = try self.parseExpr();
try self.expect(.semicolon);
const ct = try self.createNode(run_start, .{ .comptime_expr = .{ .expr = inner } });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = null, .value = ct } });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = null, .value = ct, .name_span = name_span, .is_raw = name_is_raw } });
}
// Built-in declaration: name :: #builtin;
@@ -224,7 +224,7 @@ pub const Parser = struct {
self.advance();
try self.expect(.semicolon);
const bi = try self.createNode(bi_start, .{ .builtin_expr = {} });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = null, .value = bi } });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = null, .value = bi, .name_span = name_span, .is_raw = name_is_raw } });
}
// Enum declaration
@@ -280,14 +280,14 @@ pub const Parser = struct {
// Look ahead: is this a function or an expression starting with `(`?
// Heuristic: if after matching parens we see `{` or `->`, it's a function.
if (self.isFunctionDef()) {
return self.parseFnDecl(name, start_pos);
return self.parseFnDecl(name, name_span, name_is_raw, start_pos);
}
}
// Bare block shorthand: name :: { body } is equivalent to name :: () { body }
if (self.current.tag == .l_brace) {
const body = try self.parseBlock();
return try self.createNode(start_pos, .{ .fn_decl = .{ .name = name, .params = &.{}, .return_type = null, .body = body } });
return try self.createNode(start_pos, .{ .fn_decl = .{ .name = name, .params = &.{}, .return_type = null, .body = body, .name_span = name_span, .is_raw = name_is_raw } });
}
// Otherwise it's a constant expression
@@ -299,7 +299,7 @@ pub const Parser = struct {
self.advance();
try self.expect(.semicolon);
const bi = try self.createNode(bi_start, .{ .builtin_expr = {} });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = value, .value = bi } });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = value, .value = bi, .name_span = name_span, .is_raw = name_is_raw } });
}
// name :: type_expr #foreign [lib] ["c_name"]; — foreign with type annotation
@@ -325,11 +325,11 @@ pub const Parser = struct {
.library_ref = lib_ref,
.c_name = c_name,
} });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = value, .value = fi } });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = value, .value = fi, .name_span = name_span, .is_raw = name_is_raw } });
}
try self.expect(.semicolon);
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = null, .value = value } });
return try self.createNode(start_pos, .{ .const_decl = .{ .name = name, .type_annotation = null, .value = value, .name_span = name_span, .is_raw = name_is_raw } });
}
fn parseCImportBlock(self: *Parser, start: u32, name: ?[]const u8) anyerror!*Node {
@@ -1044,10 +1044,12 @@ pub const Parser = struct {
if (self.current.tag == .identifier and self.peekNext() == .colon_colon) {
const method_start = self.current.loc.start;
const method_name = self.tokenSlice(self.current);
const method_name_span = ast.Span{ .start = self.current.loc.start, .end = self.current.loc.end };
const method_is_raw = self.current.is_raw;
self.advance(); // skip name
self.advance(); // skip ::
if (self.current.tag == .l_paren and self.isFunctionDef()) {
try methods.append(self.allocator, try self.parseFnDecl(method_name, method_start));
try methods.append(self.allocator, try self.parseFnDecl(method_name, method_name_span, method_is_raw, method_start));
} else {
// Non-function constant: name :: value;
const value = try self.parseExpr();
@@ -1672,11 +1674,13 @@ pub const Parser = struct {
}
const method_start = self.current.loc.start;
const method_name = self.tokenSlice(self.current);
const method_name_span = ast.Span{ .start = self.current.loc.start, .end = self.current.loc.end };
const method_is_raw = self.current.is_raw;
self.advance();
try self.expect(.colon_colon);
if (self.current.tag == .l_paren and self.isFunctionDef()) {
try methods.append(self.allocator, try self.parseFnDecl(method_name, method_start));
try methods.append(self.allocator, try self.parseFnDecl(method_name, method_name_span, method_is_raw, method_start));
} else {
return self.fail("expected function declaration in impl block");
}
@@ -1900,7 +1904,7 @@ pub const Parser = struct {
return try type_params.toOwnedSlice(self.allocator);
}
fn parseFnDecl(self: *Parser, name: []const u8, start_pos: u32) anyerror!*Node {
fn parseFnDecl(self: *Parser, name: []const u8, name_span: ast.Span, name_is_raw: bool, start_pos: u32) anyerror!*Node {
const params = try self.parseParams();
// Optional return type
@@ -1975,6 +1979,8 @@ pub const Parser = struct {
.type_params = type_params,
.is_arrow = is_arrow,
.call_conv = call_conv,
.name_span = name_span,
.is_raw = name_is_raw,
} });
}
@@ -2043,7 +2049,7 @@ pub const Parser = struct {
if (self.current.tag == .colon_colon) {
self.advance();
return self.parseConstBinding(name, start);
return self.parseConstBinding(name, name_span, start, name_is_raw);
}
if (self.current.tag == .colon_equal) {
self.advance();