Skip to content

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

@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

@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)

@lcnr
Copy link
Contributor

lcnr commented Aug 22, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 22, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rperier
Copy link
Contributor Author

rperier commented Aug 22, 2025

@lcnr I am a bit confused because we have:

  • "there are multiple different versions of crate {krate} in the dependency graph" , in rustc_hir_typeck

and

which one are we talking about ? both ? Because currently my suggestion in the note_version_mismatch() function (into rustc_trait_selection). I can easily be exclusive with the second one (as I am in the same function), the first suggestion being inside another compiler crate. There is way to know that a suggestion was already emitted from another compiler crate ? (rustc_hir_typeck in our case)

@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from 66cc4f4 to 35d44c6 Compare August 31, 2025 15:50
@rperier
Copy link
Contributor Author

rperier commented Aug 31, 2025

Renaming the fonction to note_impl_ambiguous_trait() . What about the rest ? Do you want that I investigate something ?

/// a probable version mismatch is added to `err`. Otherwise if it implements
/// another trait with the same name, a note message about a similarly named
/// trait is added to `err`.
fn note_impl_ambiguous_trait(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn note_impl_ambiguous_trait(
fn note_different_trait_with_same_name(

Comment on lines 2417 to 2418
// If there are multiple different versions of a crate in the dependency graph, there is already
// a suggestion designed for this purpose in the rustc_hir_typeck compiler crate
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change this to a FIXME and explicitly mention FnCtxt::detect_and_explain_multiple_crate_versions_of_trait_item 🤔

I don't really understand when this doesn't trigger but the method one does 🤔 I feel like the method one is just a subset of the above note and should be removed 🤔

moving this to zulip https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/.28PR.20.23144674.29.20Merge.20multiple.20suggestions.20into.20a.20single/with/536652274

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So to continue the discussion, as dicussed on zulip , by disabling detect_and_explain_multiple_crate_versions_of_trait_item (it returns false) and updating note_different_trait_with_same_name (the new note_version_mismatch) my suggestion is emitted in tests/run-make. But only for E0599 , not for E0277. for E0277 the old disabled suggestion "note: there are multiple different versions of crate dependency in the dependency graph" is still shown... Suppose that depending on the constrainst on the instanciated DiagCtxt (when instanciating the err) the span_note might be attached or ignored for a given error... let's admit that... but how is it technically possible to still see the message ? It is disabled in the code... I think I need help to understand this

@lcnr lcnr added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 17, 2025
@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from 35d44c6 to db2569b Compare September 22, 2025 06:49
@rperier
Copy link
Contributor Author

rperier commented Sep 22, 2025

The other CCed PR, is quit similar no ? ^^

@rperier
Copy link
Contributor Author

rperier commented Sep 22, 2025

@rustbot ready

@rperier
Copy link
Contributor Author

rperier commented Sep 22, 2025

So I have basically no ideas why the output of the compiler is not completly updated when I disable the suggestion in detect_and_explain_multiple_crate_versions_of_trait_item . So I propose to merge this PR like this, and refactorize merge with the note in rustc_hir_typeck in a second time (I can add this to my TODO list).

EDIT: see #t-compiler/help > (PR #144674) Merge multiple suggestions into a single @ 💬

I am gonna to test by removing the 3nd diagnostic

@rustbot author

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2025
@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from db2569b to de2b67f Compare October 2, 2025 18:13
@rustbot
Copy link
Collaborator

rustbot commented Oct 2, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rperier
Copy link
Contributor Author

rperier commented Oct 2, 2025

@rustbot ready

This is a proposal of a first refactoring. I have voluntarily kept things simple, so it is maintainable. The idea being to not report exactly the same messages as before, but to report a single diagnostic about possible different crate versions instead, and this in all cases. Otherwises, in the other case, if we find another trait with the same name and the same "interface", which is implemented by the Ty, we report a similarly named trait. I am opened to suggestions.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 2, 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 de2b67f to 4ceac06 Compare October 3, 2025 06:26
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.

❤️

maybe even split the function into two sub-functions here

  • check_same_trait_different_version
  • check_same_name_different_path

and only call teh second one if the first one didn't say anything

View changes since this review

}
err.span_note(multi_span, msg);
true
};
Copy link
Contributor

Choose a reason for hiding this comment

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

if you've always returning false if the argument is None, i generally prefer doing this in the caller. Is this method call code even reachable? I remember you saying that even replacing it with a panic never triggers?

Copy link
Contributor Author

@rperier rperier Oct 3, 2025

Choose a reason for hiding this comment

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

This is reachable by the error E0599 "no method named foo found for struct dep_2_reexport::Typein the current scope" from the test tests/run-make/crate-loading/multiple-dep-versions

Comment on lines +2345 to +2342
&& (self.tcx.def_path_str(trait_def_id) == required_trait_path
|| self.tcx.crate_name(trait_def_id.krate) == krate
&& self.tcx.item_name(trait_def_id) == name)
Copy link
Contributor

Choose a reason for hiding this comment

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

when is self.tcx.crate_name(trait_def_id.krate) == krate && self.tcx.item_name(trait_def_id) == name false but self.tcx.def_path_str(trait_def_id) == required_trait_path holds?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It used by tests/ui/traits/bound/same-crate-name.rs , disabling this case makes the test fail.

@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from 4ceac06 to 2f81c6e Compare October 4, 2025 15:10
@rperier
Copy link
Contributor Author

rperier commented Oct 4, 2025

Fixed, see the replies to your questions above

@rust-log-analyzer

This comment has been minimized.

…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. This commits refactorizes existing
diagnostics about multiple different crates with the same version and adds
a note when similarly named traits are found. Both diagnostics are merged
into a single function.
@rperier rperier force-pushed the add_note_if_a_type_impl_a_trait_with_the_same_name branch from 2f81c6e to 1181c3f Compare October 4, 2025 15:28
@bors
Copy link
Collaborator

bors commented Oct 6, 2025

☔ The latest upstream changes (presumably #147397) made this pull request unmergeable. Please resolve the merge conflicts.

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.

6 participants