Skip to content

§9.5: receiver fields in scope by bare name in their type's own module (#1930) - #2034

Merged
ehartford merged 17 commits into
fix-debug-alloc-silentfrom
impl-1930
Oct 4, 2026
Merged

ehartford merged 17 commits into
fix-debug-alloc-silentfrom
impl-1930

Conversation

@ehartford

@ehartford ehartford commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Implements Eric's 2026-10-03 §9.5 ruling (#1930; the words landed in spec v7.17, #2021):

In an instance method declared in its type's own module, a field of the receiver is in scope by its bare name; self.field remains valid. A parameter or local binding named like a receiver field is a shadowing error (§29.8). A bare name that names both a receiver field and a global or a module-level function is a shadowing error; self.field and the qualified name remain valid. A method declared in another module reaches the receiver's fields only through self..

What "the type's own module" is

§18.1: "One file is one module." I checked it by compiling: impl T: in another file is allowed, and a private fn of T's file is refused there ("symbol is private to module 'counter.w'"). So a method is in its type's own module when the file that declares the method also declares the struct or union. Generated text does not count as a module someone wrote: a c_import's translation and a rendered c facade (<c_import …>, <facade …>, <toolchain facade …> files) are excluded, and so are compiler derives, which carry their type's span. Their C parameter names stay as they are.

Mechanism

  • AstPool.resolve_receiver_field_names runs in the frontend after imports and the comptime transform, before freeze. It is file-local and purely syntactic, and it does not decide what a name means. In each own-module instance method body, every bare identifier that names a receiver field becomes self.<field> in place, so MIR, codegen, ownership and capture all see an ordinary field place. A callee becomes (self.<field>)(..): it calls the field's value and never finds a method. Parameters of a field's name are not rewritten. Names a body binds are not rewritten either, so the refused binding is the only diagnostic, not the start of a cascade.
  • Sema decides acceptance:
    • refuse_receiver_field_shadow (in scope_put_at / consuming rebind) rejects any parameter or local binding with a field's name: params, let, patterns, for, closure params and destructures.
    • check_receiver_field_name rejects a rewritten bare name that also resolves, from the current module, to a global, a module-level function, a type, a variant or an import namespace.
    • A method in another module that names a bare field gets "undefined variable" with a §9.5 help pointing at self.<field>.
  • Each diagnostic names the field and the receiver type and gives a rename or self. fix.

Calls I made (Eric may want to confirm)

  1. Prelude functions count as "module-level functions". They are std.builtins' module-level declarations and resolve by bare name, so a field panic, print or drop makes the bare name an error. builtins.panic (after use std.builtins) or self.panic works, and is pinned in behav_receiver_field_clash_spelled.w.
  2. Types, variants and import namespaces sharing a field's name are also refused. The words don't cover them. I chose refusal over a silent meaning (math.PI turning into self.math.PI).
  3. Compiler intrinsics (math builtins, src, channel) are not module-level functions, so a field wins there.
  4. Spec gap: a function declared in the type's own module has no qualified name usable inside that module (gauge.limit() from within gauge.w is "undefined variable"). A field that clashes with a same-module function therefore leaves that function unreachable from the type's methods, and "the qualified name remains valid" has nothing to point at. The census found no such site in the tree.
  5. Pattern/destructure bindings are local bindings. The Destructuring a Drop-typed value by pattern (let { .. } / match) never runs its Drop — a silent leak #1272 disarm idiom let { repr, other: _ } = self inside a type's own move fn now has to be spelled let { repr: r, other: _ } = self. Two fixtures were updated.

Migration (renamed bindings, one per binding name per method)

corpus renames
src 94 (Compilation zcu ×19, Resolve pool ×10, Sema 12, Codegen 6, ComptimeEval 6, Zcu 6, Parser 6, Xz 7, Diagnostic 6 + foundation/Diagnostic 4, CCodegen 5, MirLower 3, CImport 2, Bzip2 1, CodegenUnits 1)
lib/std 26 (rc 8, stackify 5, generators 5, build.w 4, box 2, hash_index 2)
build 1 (zlib_gunzip.w)
test 16 (behavior 7, compile_errors 6 kept on their own error, debug_alloc 3)
examples 1 — examples/service/src/cache/redis.w RedisCache.new(prefix) → key_prefix (spec change, 2026-09-22 ruling: example updated in the same change)
rt, tools, benchmarks, uat 0. The ecs let dt = self.dt is an ext-module method and is unaffected. The UAT fixtures have no methods.

No bare use of a field that clashes with a function or global existed anywhere, so the 16 census clashes needed no edits. Three signatures that the renames touched lost a redundant -> Unit (unit-return-review).

self. was not stripped anywhere. That is optional style and not part of this change.

Tests

New fixtures:

  • behav_receiver_field_bare_name.w: read, mut fn write and compound write, a for over a field, a closure in mut fn/move fn, a callee field, struct-literal shorthand, a generic owner, and self.field beside the bare field.
  • behav_receiver_field_ext_module.w and behav_receiver_field_clash_spelled.w.
  • err_1930_* ×8: ext-module bare field, param, local, closure param, pattern binding, field vs fn, field vs global, field vs prelude fn.

Failing-first: the seed refuses the bare-name fixture ("undefined variable") and accepts every err_1930 program.

Results on stage1: audit:all 647853 facts, 0 violations; --validate-all ok; --debug-alloc leak 0; 120 targeted behavior tests green; :gate green; :drop-audit 254 cells, 0 regressions. The fn-receiver closure crash found while writing these fixtures is #2025, fixed in the stacked #2040.

Eric Hartford added 15 commits October 4, 2026 07:18
…module's methods (§9.5, #1930): src, lib/std, build
…llee, shorthand, generic owner; ext-module refusal; each shadowing error (#1930)
…mples/service (§9.5, #1930)

Spec change: per the 2026-09-22 ruling an example the new rule breaks is
updated in the same change. examples/service/src/cache/redis.w:
RedisCache.new's parameter `prefix` shadowed the field; it is now
`key_prefix`. The compile_errors fixtures keep testing their own error
under a renamed binding.
… outside §9.5's bare field names; fixture for self.field beside a clashing prelude fn (#1930)
…edundant -> Unit on signatures the rename touched (#1930)
…eclarations; §18.2 builtins.name reaches every prelude fn and intrinsic (#1930)
…ures for qualified forms, self-names, builtins (#1930)
Eric Hartford added 2 commits October 4, 2026 07:18
…very other emitted function does

The wrapper is a method of the struct in the struct's own module, where a
field is in scope by its bare name (§9.5). It was the one emitted function
that kept the bare C parameter names, so `holder_init(h, cmp)` over a
`holder.cmp` field migrated to a method whose parameter shadows the field
(behav_migrate_fn_typedef_pointer, red in the battery). It now takes
ci_param_signature_name (`__param_cmp`), which no C field has.
…param_count`

The assertion checks that the wrapper calls the aliased C symbol; the
parameter spelling follows ci_param_signature_name, as the extern
declaration's `__param_text` above it already does.
@ehartford
ehartford marked this pull request as ready for review October 4, 2026 14:20
@ehartford
ehartford merged commit 61ec53f into main Oct 4, 2026
@ehartford
ehartford deleted the impl-1930 branch October 4, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant