Repository navigation
A program's trait outranks the prelude's of the same name, through dyn too (#2520) - #2795
Merged
Merged
Conversation
ehartford
force-pushed
the
fix-2520-tier
branch
from
October 28, 2026 23:33
7d93916 to
b11c895
Compare
…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
force-pushed
the
fix-2520-tier
branch
from
October 29, 2026 00:07
b11c895 to
e8a7434
Compare
Contributor
Author
|
Seed-driven battery GREEN on the stack top ba1cf73 ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.