-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Fix dyn -> param suggestion in struct ICEs #138238
Conversation
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(); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was also expect_item
ing 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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
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"); |
There was a problem hiding this comment.
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, ..
.
@bors r+ Thank for fixing the problems. |
cc @xizheyin |
error path only so @bors rollup |
…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
…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
…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
…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
…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
…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
…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
…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
…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
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
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