Skip to content

diagnostics: add note when param-env shadows global impl#150424

Open
xonx4l wants to merge 9 commits intorust-lang:mainfrom
xonx4l:suggest_param_env_shadowing
Open

diagnostics: add note when param-env shadows global impl#150424
xonx4l wants to merge 9 commits intorust-lang:mainfrom
xonx4l:suggest_param_env_shadowing

Conversation

@xonx4l
Copy link
Contributor

@xonx4l xonx4l commented Dec 27, 2025

This PR adds a diagnostics note when param-env shadows global impl as discussed in #149910

It adds a note explaining that the definition is hidden by the generic bound.

r?lcnr

@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 Dec 27, 2025
@rust-log-analyzer

This comment has been minimized.

Comment on lines 263 to 266
let all_impls =
impls.blanket_impls().iter().chain(impls.non_blanket_impls().values().flatten());

for &impl_def_id in all_impls {
Copy link
Contributor

Choose a reason for hiding this comment

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

use compute_applicable_impls_for_diagnostics instead of all_impls here

Copy link
Contributor

Choose a reason for hiding this comment

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

and specialization_graph::assoc_def to get the DefId of the assoc item

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay!

Comment on lines 293 to 294
"the associated type `{}` is defined as `{}` in the implementation, \
but the generic bound `{}` hides this definition",
Copy link
Contributor

Choose a reason for hiding this comment

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

cc @rust-lang/types unsure about this note. I think it's certainly useful. Ideally we'd link to some reference section or github issue explaining impl shadowing.

I think it's easiest to write a special github issue for this quickly explaining why this happens, what they can do about it, and how this may change in the future

Copy link
Member

Choose a reason for hiding this comment

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

Im not sure a github issue is the best place but also I don't know where would be better... If something reference-like was t-types controlled then certainly there 🤔 github issue is probably good enough :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should I open a issue then ?

Copy link
Contributor

Choose a reason for hiding this comment

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

going to do so myself as I have strong opinions on the way we frame this

Copy link
Contributor

Choose a reason for hiding this comment

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

opened #152409. It took me a while to get to this 😅 sorry

can you refer to this issue in the note (and also make sure its wording matches the wording used in that issue)

r=me after

@rust-log-analyzer

This comment has been minimized.

@xonx4l xonx4l force-pushed the suggest_param_env_shadowing branch from 22a4a6b to f616235 Compare January 14, 2026 04:45
@xonx4l
Copy link
Contributor Author

xonx4l commented Jan 14, 2026

@lcnr I made the suggested changes and CI is also green now .

@xonx4l
Copy link
Contributor Author

xonx4l commented Jan 21, 2026

@lcnr is anything more required other than squashing commits??

@rustbot

This comment has been minimized.

@xonx4l xonx4l force-pushed the suggest_param_env_shadowing branch from 408b4a3 to 071d0cd Compare February 10, 2026 18:43
@rustbot
Copy link
Collaborator

rustbot commented Feb 10, 2026

This PR was rebased onto a different main 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.

@rust-log-analyzer

This comment has been minimized.

};

let impl_item_def_id = leaf_def.item.def_id;
let impl_assoc_ty = tcx.type_of(impl_item_def_id).instantiate(tcx, impl_substs);
Copy link
Contributor

Choose a reason for hiding this comment

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

this currently incorrectly emits the note for the following I think

trait Trait {
    type Assoc;
}

impl<T> Trait for T {
    type Assoc = T;
}

fn foo<T: Trait>(x: T::Assoc) -> u32 { x }

This also doesn't handle GATs and the following probably ICEs

trait Trait {
    type Assoc<T>;
}

impl<T> Trait for T {
    type Assoc<U> = U;
}

fn foo<T: Trait>(x: T::Assoc<T>) -> u32 { x }

Copy link
Contributor

Choose a reason for hiding this comment

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

to handle this, consider the relevant code in fn translate_args

goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id.into(), impl_args)

for this, you actually do have to equate the alias trait_ref with the impl trait_ref (and you can unwrap this equate as it should never fail)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made changes to the logic to handle GATs as suggested. Thanks for the direction.

Copy link
Contributor

Choose a reason for hiding this comment

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

please add the aboe snippets as tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Added the required tests.

@@ -0,0 +1,13 @@
trait Trait {
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 add a comment to each test explaining what it is testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added all the comments for the tests.

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

Labels

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.

5 participants