Skip to content

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

Merged
rust-bors[bot] merged 10 commits intorust-lang:mainfrom
xonx4l:suggest_param_env_shadowing
Feb 15, 2026
Merged

diagnostics: add note when param-env shadows global impl#150424
rust-bors[bot] merged 10 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.

Comment on lines 294 to 295
let trait_def_id = alias.trait_def_id(tcx);
let rebased_args = alias.args.rebase_onto(tcx, trait_def_id, 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.

move this below the match, they are only necessary for the let impl_assoc_ty

@@ -0,0 +1,14 @@
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.

also mention that we want to tell the user about param_env shadowing in a comment here

@lcnr
Copy link
Contributor

lcnr commented Feb 14, 2026

@bors delegate+

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 14, 2026

✌️ @xonx4l, you can now approve this pull request!

If @lcnr told you to "r=me" after making some further change, then please make that change and post @bors r=lcnr.

@xonx4l
Copy link
Contributor Author

xonx4l commented Feb 14, 2026

@bors r=lcnr

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 14, 2026

📌 Commit 1be4164 has been approved by lcnr

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 14, 2026
rust-bors bot pushed a commit that referenced this pull request Feb 15, 2026
Rollup of 9 pull requests

Successful merges:

 - #150424 (diagnostics: add note when param-env shadows global impl)
 - #152132 (implement `carryless_mul`)
 - #152508 (Improve write! and writeln! error when called without destination)
 - #152534 (Test(lib/win/net): Skip UDS tests when under Win7)
 - #152578 (ci: Lock cross toolchain version and update docs)
 - #152188 (Include `library/stdarch` for `CURRENT_RUSTC_VERSION` updates)
 - #152402 (Add regression test for #141738)
 - #152472 (unwind/wasm: fix compile error by wrapping wasm_throw in unsafe block)
 - #152610 (Exchange js_lint message between bless and non-bless)
@rust-bors rust-bors bot merged commit 79adcd1 into rust-lang:main Feb 15, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 15, 2026
rust-timer added a commit that referenced this pull request Feb 15, 2026
Rollup merge of #150424 - xonx4l:suggest_param_env_shadowing, r=lcnr

diagnostics: add note when param-env shadows global impl

 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
github-actions bot pushed a commit to rust-lang/stdarch that referenced this pull request Feb 16, 2026
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#150424 (diagnostics: add note when param-env shadows global impl)
 - rust-lang/rust#152132 (implement `carryless_mul`)
 - rust-lang/rust#152508 (Improve write! and writeln! error when called without destination)
 - rust-lang/rust#152534 (Test(lib/win/net): Skip UDS tests when under Win7)
 - rust-lang/rust#152578 (ci: Lock cross toolchain version and update docs)
 - rust-lang/rust#152188 (Include `library/stdarch` for `CURRENT_RUSTC_VERSION` updates)
 - rust-lang/rust#152402 (Add regression test for rust-lang/rust#141738)
 - rust-lang/rust#152472 (unwind/wasm: fix compile error by wrapping wasm_throw in unsafe block)
 - rust-lang/rust#152610 (Exchange js_lint message between bless and non-bless)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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