Skip to content

Commit 36dac3b

Browse files
olwangclaude
andcommitted
rsscript: narrow reserved namespace to __rss_/__rsscript_ prefixes
The earlier blanket `__` reservation also rejected Python-style dunders (`__hash__`, `__init__`), which the tinygrad port uses. Reserve only the actual generated prefixes `__rss_` and `__rsscript_`; plain `__`-dunders and the synthetic `__TupleN` structs are legal again. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent ba27b2d commit 36dac3b

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

crates/rsscript/src/analyzer.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,13 @@ fn analyze_program(
203203
diagnostics
204204
}
205205

206-
/// `__TupleN` (N digits) is the synthetic tuple struct injected by the tuple
207-
/// desugar, not a user declaration, so it is exempt from the reserved-name rule.
208-
fn is_synthetic_tuple_name(name: &str) -> bool {
209-
name.strip_prefix("__Tuple")
210-
.is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()))
206+
/// Namespaces that the compiler generates and a user declaration must not claim:
207+
/// desugaring temporaries (`__rss_*`) and runtime helpers (`__rsscript_*`). Other
208+
/// `__`-prefixed names (Python-style dunders like `__hash__`, `__eq__`, and the
209+
/// synthetic `__TupleN` structs the tuple desugar injects) are legal — they don't
210+
/// collide with any generated namespace.
211+
fn is_reserved_generated_name(leaf: &str) -> bool {
212+
leaf.starts_with("__rss_") || leaf.starts_with("__rsscript_")
211213
}
212214

213215
fn type_aliases_from_program(
@@ -1257,11 +1259,11 @@ impl Analyzer<'_> {
12571259
}
12581260
}
12591261

1260-
/// Identifiers beginning with `__` are reserved for compiler-generated
1261-
/// symbols (tuple structs `__TupleN`, desugaring temporaries `__rss_*`,
1262-
/// runtime helpers `__rsscript_*`). Reject user declarations that claim a
1263-
/// reserved name so generated helpers can never collide with source symbols.
1264-
/// The synthetic `__TupleN` structs the tuple desugar injects are exempt.
1262+
/// The `__rss_*` and `__rsscript_*` namespaces are reserved for
1263+
/// compiler-generated desugaring temporaries and runtime helpers. Reject user
1264+
/// declarations that claim one so generated helpers can never collide with
1265+
/// source symbols. Other `__`-prefixed names (Python-style dunders like
1266+
/// `__hash__`, and the synthetic `__TupleN` tuple structs) are left legal.
12651267
fn check_reserved_declaration_names(&mut self) {
12661268
use crate::syntax::ast::Item;
12671269
for item in self.syntax_program.items.clone() {
@@ -1275,11 +1277,11 @@ impl Analyzer<'_> {
12751277
};
12761278
// `Type.method` reserves on the member, not the (user) type prefix.
12771279
let leaf = name.rsplit('.').next().unwrap_or(name);
1278-
if leaf.starts_with("__") && !is_synthetic_tuple_name(leaf) {
1280+
if is_reserved_generated_name(leaf) {
12791281
self.unsupported_syntax(
12801282
span.clone(),
12811283
"reserved declaration name",
1282-
"Identifiers beginning with `__` are reserved for compiler-generated symbols; rename this declaration.",
1284+
"The `__rss_` and `__rsscript_` prefixes are reserved for compiler-generated symbols; rename this declaration.",
12831285
);
12841286
}
12851287
}

crates/rsscript/tests/fixtures/fail/reserved-double-underscore-name.rss

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// expect: RS0015
2-
// Identifiers beginning with `__` are reserved for compiler-generated symbols
3-
// (tuple structs, desugaring temporaries, runtime helpers).
4-
fn __helper() -> Int {
2+
// The `__rss_` and `__rsscript_` prefixes are reserved for compiler-generated
3+
// symbols (desugaring temporaries, runtime helpers). Plain Python-style dunders
4+
// like `__hash__` are NOT reserved — only the generated namespaces are.
5+
fn __rss_helper() -> Int {
56
return 1
67
}
78

docs/tinygrad-port-todo.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ lists), tuples & destructuring, `#lower_name` escape hatch, source-qualified
99
symbol inventory, type-associated constants & static methods, value-semantics
1010
clone/derives, Option ergonomics (`?`-on-Option + combinators), default
1111
parameters (Copy and non-Copy), type aliases (generic + non-generic, expanded
12-
at every comparison site), a reserved `__`-prefix namespace for
13-
compiler-generated helpers, and struct field defaults (`name: T = expr`, filled at
12+
at every comparison site), reserved `__rss_`/`__rsscript_` namespaces for
13+
compiler-generated helpers (Python-style dunders like `__hash__` stay legal), and
14+
struct field defaults (`name: T = expr`, filled at
1415
construction on both backends).
1516

1617
Verified already-met against their written acceptance (closed without new code):

0 commit comments

Comments
 (0)