Skip to content

Add a diagnostic for similarly named traits #144674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rperier
Copy link
Contributor

@rperier rperier commented Jul 30, 2025

cc #133123

This is a first proposal, suggestions are welcome

@rustbot
Copy link
Collaborator

rustbot commented Jul 30, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 30, 2025
@rust-log-analyzer

This comment has been minimized.

@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from ff869dc to abdb087 Compare August 1, 2025 06:40
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Aug 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 1, 2025

This PR modifies run-make tests.

cc @jieyouxu

@rperier
Copy link
Contributor Author

rperier commented Aug 1, 2025

Fixed by using predicate_must_hold_modulo_regions

Comment on lines 5133 to 5135
trait_ref
.skip_binder()
.with_self_ty(self.tcx, trait_predicate.skip_binder().self_ty()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip_binder is pretty much never correct:

https://rustc-dev-guide.rust-lang.org/ty_module/early_binder.html?highlight=Binder#earlybinder-and-instantiating-parameters

also see https://rustc-dev-guide.rust-lang.org/typing_parameter_envs.html

U will also need to properly handle the generic parameters of the traits here, e.g. what if we've got trait Trait {} and a separate trait Trait<T> {}

Also, check predicate must hold for the other trait, not per impl.

Copy link
Contributor Author

@rperier rperier Aug 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhhhh, I see. I have to document myself about this part, thanks for the links.

Also, check predicate must hold for the other trait, not per impl.

Would you have a usecase for this ? You're probably right, but I don't get it.

thanks !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, right now given other::Trait with 2 impls, for each impl you separately check this_trait_self_ty: other::Trait. This is unnecessary, we simply care about

does this_trait_self_ty implement other::Trait

The reason why it implements that trait doens't really matter for us. My usecase is that doing it per impls is unnecessary work and more involved to implement than the alternative.

@rperier
Copy link
Contributor Author

rperier commented Aug 8, 2025

The current version is the following. I no longer browse the impls, so it is no longer per impl. It's mostly working (other::Trait or other::Trait<T> are working), however I still get weird behaviours with super traits and the GenericArgs . That's why I did not push the commit yet, some tests are broken, I am investigating.

UPDATE: switch to the last commit directly and see update below regarding generics and ICE

pub(crate) fn suggest_impl_similarly_named_trait(
        &self,
        err: &mut Diag<'_>,
        obligation: &PredicateObligation<'tcx>,
        trait_predicate: ty::PolyTraitPredicate<'tcx>,
    ) {
        let trait_def_id = trait_predicate.def_id();
        let trait_name = self.tcx.item_name(trait_def_id);
        if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
            if trait_def_id != *def_id && trait_name == self.tcx.item_name(def_id) {
                if let Some(pred) = self.tcx.predicates_of(*def_id).instantiate(self.tcx, trait_predicate.skip_binder().trait_ref.args).predicates.iter().find(|clause| {
                    clause.as_trait_clause().map_or(false, |trait_clause| trait_clause.def_id() == *def_id)
                })
                {
                    let pred = pred.as_trait_clause().unwrap();
                    self.predicate_must_hold_modulo_regions(&Obligation::new(
                        self.tcx,
                        obligation.cause.clone(),
                        obligation.param_env,
                        pred,
                    ))
                } else {
                    false
                }
            } else {
                false
            }
        }) {
            err.note(format!(
                "`{}` implements similarly named `{}`, but not `{}`",
                trait_predicate.self_ty(),
                self.tcx.def_path_str(other_trait_def_id),
                trait_predicate.print_modifiers_and_trait_path()
            ));
        }
        ()
    }

@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from abdb087 to 532c561 Compare August 11, 2025 14:32
@rperier
Copy link
Contributor Author

rperier commented Aug 11, 2025

This is another proposal, feedbacks are welcomed. I am not convinced about the part regarding the generic args. The idea being that you cannot select a similarly named trait with different generics (or different constraints), it does not makes sense and might cause ICE.

@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from 532c561 to 3fcbf63 Compare August 13, 2025 17:40
@rperier
Copy link
Contributor Author

rperier commented Aug 13, 2025

This is another proposal, I have simplified the code. This is to show you the code before I merge it with the other suggestion. I have to analyze the other suggestion you were talking about and try to merge my code with it now.

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the impl lgtm now, I don't think we should have these two similar suggestions however (similarly named trait vs similarly named trait from different crate version), and believe they should be merged into 1

@rperier
Copy link
Contributor Author

rperier commented Aug 15, 2025

OK, thanks for your feedbacks. I will merge with the other suggestion and fix param.kind during the weekend

…equired one

This is useful when you have two dependencies that use different trait for
the same thing and with the same name. The user can accidentally implement
the bad one which might be confusing.
@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from 3fcbf63 to a6853be Compare August 18, 2025 16:35
@rperier
Copy link
Contributor Author

rperier commented Aug 18, 2025

Merged into the suggestion we have discussed above. I have kept each note/notice independent, because I have the feeling that the three notes/notices might still be emitted independently when you think about it. You might have similarly named traits within the same crate but also in different crates. You might have cases when similarly named traits are found but not traits with the same path (this is typically the case in some ui tests)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants