Skip to content

A program's trait outranks the prelude's of the same name, through dyn too (#2520) - #2795

Merged
ehartford merged 1 commit into
fix-d143-methodsfrom
fix-2520-tier
Oct 29, 2026
Merged

ehartford merged 1 commit into
fix-d143-methodsfrom
fix-2520-tier

Conversation

@ehartford

@ehartford ehartford commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2520's reopened cases: §18.2 precedence for contested bare methods and visible-trait lookup; dyn object safety and codegen trait tables keyed by Sema's trait identity. Not in this PR: a qualified spelling for a static call between two equal-rank traits (needs a syntax ruling). Gate green. Draft until the stack's battery is green.

@ehartford
ehartford added this pull request to stack #2797 October 28, 2026 23:18
@ehartford ehartford changed the title fix 2520 tier A program's trait outranks the prelude's of the same name, through dyn too (#2520) Oct 28, 2026
…ame, through dyn too (#2520)

Reopened on review of #2764: with a program's `trait Unit1: fn one() -> Self`
on f64, `f64.one()` was "ambiguous trait method 'one' … candidates:
f64.One.one, f64.Unit1.one" though the program never imported std's One,
and `dyn One` over a program's own `trait One` resolved std's: "trait 'One'
is not object-safe", then, past Sema, "cannot lower dyn trait method 'one'
because its return type mentions Self".

§18.2 ranks a module's own declarations above imports and imports above
the prelude; the trait resolvers took the first visible candidate or the
last registered one.

- Sema.trait_by_precedence is the one precedence walk: the current
  module's trait, then another program module's, then std's.
  lookup_trait_visible is it under visibility; a contested bare method
  (selected_trait_idx_for_method) takes the receiver's bound, else the one
  non-std candidate; §11.4's ambiguity stays for candidates of equal rank.
- dyn: the object-safety check reads the visible trait's declaration
  (trait_decl_nodes per trait index), and TY_TRAIT_OBJ records the trait
  Sema resolved (index + 1 in d1). Codegen keys its trait tables (trait_map,
  vtables, impl lookups) by Sema's trait identity symbol
  (trait_identity_symbol: the name unless it collides across files, as
  impl method symbols are minted), read from the dyn type, the impl
  (trait_idx_for_impl) or the written `dyn Name` (trait_identity_symbol_
  named_at); none re-derives the trait from a bare name.

Tests: behav_2520_user_trait_outranks_prelude (2 1) and
behav_2520_dyn_user_trait_named_like_std (7) fail on the installed
compiler and pass on stage1, with the #2764 suite and 187 trait, dyn,
vtable, impl and Box tests.

The ambiguity chain (register_method_slot) recorded only the first two
candidates for an (owner, method): with std's One, a program's Unit1 and
Unit2 on f64, it held One and Unit1, so the precedence rule saw one program
candidate and `f64.one()` silently took Unit1's (lldb: selected_trait_idx_
for_method returned Unit1's index). Every later candidate now joins the
chain's end. err_2520_two_program_traits_and_std_ambiguous printed `1` on
the stage1 without it; the installed compiler reports it only because any
two candidates were ambiguous there.

A written `dyn Name` names its trait with lookup_trait_named: the
visible trait by precedence, else the one registered under the name, as
the prelude's Error, which a program reaches with no import, is
(spec_ss10_8_error_trait: "unknown method 'source' for dyn trait 'Error'"
when the dyn path asked for the import-visible trait alone; lldb:
trait_by_precedence returned -1 in the body's context). One answer in
every context, so a signature's `&dyn Error` and a body's are one type.
@ehartford

Copy link
Copy Markdown
Contributor Author

Seed-driven battery GREEN on the stack top ba1cf73 (with run tools/battery.w: gate, uat, battery-checks).

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.

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)

1 participant