Skip to content
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

Fix dyn -> param suggestion in struct ICEs #138238

Merged
merged 2 commits into from
Mar 11, 2025

Conversation

compiler-errors
Copy link
Member

Makes the logic from #138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes #138229
Fixes #138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this

@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 Mar 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 8, 2025

HIR ty lowering was modified

cc @fmease


let parent_id = tcx.hir_get_parent_item(self_ty.hir_id).def_id;
let parent_item = tcx.hir_node_by_def_id(parent_id).expect_item();
Copy link
Member Author

Choose a reason for hiding this comment

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

expect_item here is not valid, since there are other kinds of HIR parents that can own this dyn Trait, like extern items (or trait/impl items, closures, etc).

let parent_hir_id = tcx.parent_hir_id(self_ty.hir_id);
let parent_item = tcx.hir_get_parent_item(self_ty.hir_id).def_id;

let generics = match tcx.hir_node_by_def_id(parent_item) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I added a check that we're suggesting this on a field directly. dyn may show up in other positions like in a where clause or in an expr on a default field value.

if let hir::ItemKind::Enum(..) = parent_item.kind {
return false;
}
// Look at the direct HIR parent, since we care about the relationship between
Copy link
Member Author

Choose a reason for hiding this comment

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

This was also expect_iteming unnecessarily. I took the logic and made it focus on the relationship between the ty and its parent node, so we don't suggest turning:

struct Foo { f: Box<Any>, /* more fields */ }

Into this:

struct Foo<T: Any> { f: Box<T>, /* more fields */ }

My general logic here is that if the ty's hir node parent is another ty, then it's probably just a misspelled dyn Trait. There is no perfect heuristic here, but I think it's less invasive to suggest the most naive thing here, which is just to insert dyn, unless we're very confident it will be wrong.

@@ -13,7 +13,6 @@ struct Foo2 {
//~^ ERROR expected a type, found a trait
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this test out of suggestions/ since that's a kitchen sink


// If the parent item is a struct, check if self_ty is the last field.
if let hir::ItemKind::Struct(variant_data, _) = parent_item.kind {
if variant_data.fields().last().unwrap().ty.span != self_ty.span {
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need to use span comparisons here, hir_id comparisons are more faithful.

Comment on lines +168 to +173
let param = "TUV"
.chars()
.map(|c| c.to_string())
.chain((0..).map(|i| format!("P{i}")))
.find(|s| !generics.params.iter().any(|param| param.name.ident().as_str() == s))
.expect("we definitely can find at least one param name to generate");
Copy link
Member Author

Choose a reason for hiding this comment

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

This logic didn't account for preexisting generic args, so this tries its best to suggest T, U, V, P1, P2, ...

@nnethercote
Copy link
Contributor

@bors r+

Thank for fixing the problems.

@bors
Copy link
Contributor

bors commented Mar 9, 2025

📌 Commit ceb0401 has been approved by nnethercote

It is now in the queue for this repository.

@bors bors 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 Mar 9, 2025
@nnethercote
Copy link
Contributor

cc @xizheyin

@compiler-errors
Copy link
Member Author

error path only so

@bors rollup

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 9, 2025
…struct, r=nnethercote

Fix dyn -> param suggestion in struct ICEs

Makes the logic from rust-lang#138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes rust-lang#138229
Fixes rust-lang#138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 10, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#137279 (Make some invalid codegen attr errors structured/translatable)
 - rust-lang#137793 (Stablize anonymous pipe)
 - rust-lang#138238 (Fix dyn -> param suggestion in struct ICEs)
 - rust-lang#138270 (chore: Fix some comments)
 - rust-lang#138280 (fix ICE in pretty-printing `global_asm!`)
 - rust-lang#138286 (triagebot.toml: Don't label `test/rustdoc-json` as A-rustdoc-search (…)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 10, 2025
…struct, r=nnethercote

Fix dyn -> param suggestion in struct ICEs

Makes the logic from rust-lang#138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes rust-lang#138229
Fixes rust-lang#138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 10, 2025
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#136395 (Update to rand 0.9.0)
 - rust-lang#137279 (Make some invalid codegen attr errors structured/translatable)
 - rust-lang#137585 (Update documentation to consistently use 'm' in atomic synchronization example)
 - rust-lang#137926 (Add a test for `-znostart-stop-gc` usage with LLD)
 - rust-lang#138074 (Support `File::seek` for Hermit)
 - rust-lang#138238 (Fix dyn -> param suggestion in struct ICEs)
 - rust-lang#138270 (chore: Fix some comments)
 - rust-lang#138280 (fix ICE in pretty-printing `global_asm!`)
 - rust-lang#138286 (triagebot.toml: Don't label `test/rustdoc-json` as A-rustdoc-search (…)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 10, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#136395 (Update to rand 0.9.0)
 - rust-lang#137279 (Make some invalid codegen attr errors structured/translatable)
 - rust-lang#137585 (Update documentation to consistently use 'm' in atomic synchronization example)
 - rust-lang#137926 (Add a test for `-znostart-stop-gc` usage with LLD)
 - rust-lang#138074 (Support `File::seek` for Hermit)
 - rust-lang#138238 (Fix dyn -> param suggestion in struct ICEs)
 - rust-lang#138270 (chore: Fix some comments)
 - rust-lang#138286 (triagebot.toml: Don't label `test/rustdoc-json` as A-rustdoc-search (…)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 10, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#136395 (Update to rand 0.9.0)
 - rust-lang#137279 (Make some invalid codegen attr errors structured/translatable)
 - rust-lang#137585 (Update documentation to consistently use 'm' in atomic synchronization example)
 - rust-lang#137926 (Add a test for `-znostart-stop-gc` usage with LLD)
 - rust-lang#138074 (Support `File::seek` for Hermit)
 - rust-lang#138238 (Fix dyn -> param suggestion in struct ICEs)
 - rust-lang#138270 (chore: Fix some comments)
 - rust-lang#138286 (triagebot.toml: Don't label `test/rustdoc-json` as A-rustdoc-search (…)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 10, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#136395 (Update to rand 0.9.0)
 - rust-lang#137279 (Make some invalid codegen attr errors structured/translatable)
 - rust-lang#137585 (Update documentation to consistently use 'm' in atomic synchronization example)
 - rust-lang#137926 (Add a test for `-znostart-stop-gc` usage with LLD)
 - rust-lang#138074 (Support `File::seek` for Hermit)
 - rust-lang#138238 (Fix dyn -> param suggestion in struct ICEs)
 - rust-lang#138270 (chore: Fix some comments)
 - rust-lang#138286 (triagebot.toml: Don't label `test/rustdoc-json` as A-rustdoc-search (…)

r? `@ghost`
`@rustbot` modify labels: rollup
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 10, 2025
…struct, r=nnethercote

Fix dyn -> param suggestion in struct ICEs

Makes the logic from rust-lang#138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes rust-lang#138229
Fixes rust-lang#138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 11, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#136395 (Update to rand 0.9.0)
 - rust-lang#137279 (Make some invalid codegen attr errors structured/translatable)
 - rust-lang#137585 (Update documentation to consistently use 'm' in atomic synchronization example)
 - rust-lang#137926 (Add a test for `-znostart-stop-gc` usage with LLD)
 - rust-lang#138074 (Support `File::seek` for Hermit)
 - rust-lang#138238 (Fix dyn -> param suggestion in struct ICEs)
 - rust-lang#138270 (chore: Fix some comments)
 - rust-lang#138286 (triagebot.toml: Don't label `test/rustdoc-json` as A-rustdoc-search (…)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1ae083d into rust-lang:master Mar 11, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 11, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 11, 2025
Rollup merge of rust-lang#138238 - compiler-errors:dyn-suggestion-in-struct, r=nnethercote

Fix dyn -> param suggestion in struct ICEs

Makes the logic from rust-lang#138042 a bit less ICEy and more clean. Also fixes an incorrect suggestion when the struct already has generics. I'll point out the major changes and observations in the code.

Fixes rust-lang#138229
Fixes rust-lang#138211

r? nnethercote since you reviewed the original pr, or re-roll if you don't want to review this
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.

ICE: compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs None ICE: $ident: found TraitItem(TraitItem
4 participants