Skip to content

A trait impl resolves its trait by bare name in the merged prelude scope: a user trait named like a std trait rebinds std's impls (and vice versa) #2520

Closed

Description

@ehartford

Found 2026-10-20 while adding pub trait One: fn one() -> Self to lib/std/traits.w (#2220 step 2, it.product()): test/behavior/behav_1346_trait_impl_body_forms.w declares its own trait One with an instance method fn one() -> i32. With both in scope:

error: undefined variable                       (`self` in the user's `impl One for X: fn one(): self.v + 2`)
error: 'X.one' is a function of the type and takes no receiver; call it as `X.one(...)`
error: a float where an integer is demanded …   --> <embedded-std>/std/traits.w:60  (`impl One for f32: fn one(): 1.0`)

So the user's impl One for X bound to std's One (a static -> Self), and std's own impl One for f32 bound to the user's One (-> i32): trait impls resolve their trait by bare name in the merged prelude scope, whichever module declared them. D70 (last import wins) is about the user's names; an impl written in std.traits must bind to std.traits' One, and a user's impl of their own trait to theirs, as a user's type Foo beside a std Foo already does for types.

Expected: an impl's trait resolves in the impl's own module scope first; a user trait shadowing a prelude trait of the same name changes nothing in std. Workaround: the fixture's trait is renamed (One → PlusTwo) in the step 2 PR with a comment naming this issue; the rename is undone when this is fixed.

Activity

  1. ehartford commented on Oct 21, 2026

    @ehartford
    ContributorAuthor

    Second shape, 2026-10-21: two traits each declaring a method named one, both implemented for f64 (std.traits' One.one and a test's Unit1.one), collide on the f64.one method symbol and MIR reports "body index map mismatch for fn symbol N" (two bodies for one symbol). Method identity is the bare Type.method name, not (trait, Type, method). The D140 fixture names its method unit until this is fixed.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced, both shapes.

    $ cat main.w   # a user trait named like std's One
    trait One:
        fn one(self: &Self) -> i32
    type X { v: i32 }
    impl One for X:
        fn one(): self.v + 2
    fn main:
        let x = X { v: 1 }
        print(x.one())
    $ with run main.w
    error: undefined variable
     --> main.w:5:15
    error: 'X.one' is a function of the type and takes no receiver; call it as `X.one(...)`
    $ cat two.w    # a second trait with a method `one`, implemented for f64
    trait Unit1:
        fn one() -> Self
    impl Unit1 for f64:
        fn one(): 2.0
    fn main:
        print(f64.one())
    $ with run two.w
    error: internal compiler error: invalid MIR before codegen (a bug in the With compiler, not in this program): body index map mismatch for fn symbol 909 in ``
    

    pre-release: ordinary user traits collide with std names, one way with a wrong diagnostic and one with an internal error.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Fix campaign 2026-10-25: both root causes located; the fix is a multi-step change to trait and method identity, so it goes to Eric to schedule.

    Shape 1 (a user trait named like std's One): Sema.trait_lookup: HashMap[i32, i32] (src/Sema.w:1007) maps a trait's bare name symbol to one trait index. A user trait One and std.traits.One are the same key, so impl One for X binds to whichever registered last; the user impl is then checked against std's fn one() -> Self (no receiver), giving "undefined variable self" and "X.one is a function of the type". 24 reads of trait_lookup (SemaDecl 10, Sema 5, SemaCheck 9) assume the name is the identity.

    Shape 2 (two traits with a method one, both implemented for f64): an impl method's symbol is Type.method with no trait in it, so One.one and Unit1.one for f64 are one fn symbol with two bodies, and MIR reports "body index map mismatch for fn symbol N".

    What the spec already rules. Trait names resolve through modules like every other name (D70: imports are namespaces). §11.4 (011_traits.md:147-152) makes a bare call ambiguous when two packages give a type the same method name, and the fully-qualified spelling pkg.method_name(value) disambiguates.

    What the fix needs. (1) Trait identity is (declaring module, name), with impl sites resolving the trait name through D70 scope the way types do (named_type_candidate_head). (2) An impl method's symbol carries its trait (Type.Trait.method) so two traits' one on one type are two functions. (3) A bare x.one() with two candidates is the §11.4 ambiguity error. These touch Sema's trait tables, method symbols in MIR and codegen, and the trait-table audit.

    The question: schedule it as its own campaign (my recommendation, since it is not a one-PR fix), or narrow it for the release to a loud error when a user trait shares a std trait's name?

  4. ehartford commented on Oct 27, 2026

    @ehartford
    ContributorAuthor

    Phase-3 fix campaign, 2026-10-26: both shapes re-reproduced on current main (dee141b79, installed with v0.15.3.0) — the user trait One still binds std's impl ("undefined variable self", "X.one is a function of the type"), and two traits with a method one on f64 still ICE ("body index map mismatch for fn symbol"). Root causes stand as located 2026-10-25 (Sema.trait_lookup keyed by bare name; impl method symbols carry no trait).

    This is the multi-step trait/method-identity change, not a one-PR fix, so it stays human attn needed on do-before-release for scheduling: its own campaign, or a release-scoped loud error when a user trait shares a std trait's name?

  5. added a commit that references this issue on Oct 28, 2026
  6. ehartford commented on Oct 28, 2026

    @ehartford
    ContributorAuthor

    Fixed by #2764 (64ace83), battery-green on 4062c27 and merged.

    Root causes: Ast.inherit_trait_impl_receivers took the first same-named NK_TRAIT_DECL (std's One) for a user's trait; method symbols were Owner.method, so two traits giving one type the same method collided in the MIR body map; trait_lookup was flat last-wins.

    Fix (per ruling, #751 scoped to traits): traits keyed by (module, name) candidates; trait_idx_for_impl resolves from the impl's own module; impl method symbols are Owner.Trait.method (identity-hashed segment only when the trait name collides); (owner, trait, method) registries; a bare call with two candidates is the §11.4 ambiguity error. Battery follow-ups in the same PR: dyn global-effect bodies and async declaration take Sema's semantic symbol; multi-index lowering and drop-owner detection read Sema's registries, not symbol text.

    Regression: test/behavior/behav_2520_{user_trait_shadows_std,two_traits_dispatch_by_bound,two_traits_instance_bound}.w, test/compile_errors/err_2520_ambiguous_bare_trait_method.w. Known remainder: dyn trait-object dispatch still resolves by bare name (pre-existing, #751).

  7. ehartford commented on Oct 28, 2026

    @ehartford
    ContributorAuthor

    Reopened 2026-10-28 on review of #2764 (main fc27c65e8, installed with v0.15.3.0-gfc27c65e8). Both original shapes are fixed, but a user trait still collides with a prelude trait, and the diagnostic's remedy has no spelling.

    trait Unit1:
        fn one() -> Self
    impl Unit1 for f64:
        fn one(): 2.0
    fn main:
        print(f64.one())
    
    error: ambiguous trait method 'one' for type 'f64'; candidates: f64.One.one, f64.Unit1.one; name the trait through a bound (§11.4)
    
    • The user never imported One; it is std's prelude trait (lib/std/traits.w:45). §18.1/§18.2: a visible user declaration wins over a prelude name in its scope, so f64.one() here should be the user's Unit1.one, not an ambiguity.
    • The remedy names a bound, which a static call does not have. Unit1.one() ("undefined variable") and f64.Unit1.one() ("unknown field 'Unit1'") are both refused; only wrapping the call in fn get[T: Unit1]() -> T: T.one() works. A diagnostic whose remedy cannot be written is ceremony with no exit.
    • Also left by fix 2520 #2764 and not filed: dyn trait-object dispatch still resolves traits by bare name (find_dyn_trait_method_info), so a user trait named like a std trait still rebinds on the dyn path.

    Expected: the prelude tier loses to the user's trait (§18.2 precedence), the §11.4 ambiguity applies only between candidates of equal precedence, and a qualified spelling exists for a static call.

  8. ehartford commented on Oct 29, 2026

    @ehartford
    ContributorAuthor

    Landed in stack #2797 (24222fe): #2795 — a program's trait outranks the prelude's of the same name (§18.2), the ambiguity chain keeps every candidate (it held two, so a third trait went unseen), and dyn Name resolves the trait by module through codegen (trait tables keyed by Sema's trait identity); #2798 — D169 Trait.method(args) (§11.4, words blessed) spells the choice between equal-rank traits, and the ambiguity error now names it. Found on the way: #2794, D143 now covers recursive methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions