Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
r? @mejrs rustbot has assigned @mejrs. Use Why was this reviewer chosen?The reviewer was selected based on:
|
There was a problem hiding this comment.
Thanks!
- Please add a regression test for not suggesting
path::_, like reported in the issue. - Please add explicit
HELPannotations in the new tests.
It looks good otherwise but I'm not familiar enough here to tell whether or not corner cases are being missed, so
|
Reminder, once the PR becomes ready for a review, use |
b6a1b9e to
518355f
Compare
|
Thanks @mejrs for your comments, resolved. |
|
@rustbot ready |
2de8a14 to
537deff
Compare
537deff to
189de55
Compare
This comment has been minimized.
This comment has been minimized.
189de55 to
5632423
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
5632423 to
697b3ae
Compare
This comment has been minimized.
This comment has been minimized.
|
@apiraino rebased, thank you for reminding me! |
This comment has been minimized.
This comment has been minimized.
When `unused_variables` finds a similarly named const or enum variant, it suggests replacing the unused binding with a pattern matching that item. The item was printed with a trimmed path, which is not necessarily resolvable from the binding site, so the `MachineApplicable` suggestion could fail to compile. Print the item with `with_crate_prefix!`: a `crate::`-rooted, re-export-aware path that resolves from any binding site in the crate. A const declared inside the current body has no such path, so it is printed by its bare in-scope name. Also suppress const typo suggestions for bare `let x = ...` bindings, where a const pattern would be refutable.
| if tcx.is_descendant_of(item_def_id, body_def_id) { | ||
| return tcx.item_name(item_def_id).to_string(); | ||
| } | ||
|
|
||
| with_crate_prefix!(tcx.def_path_str(item_def_id)) |
There was a problem hiding this comment.
Unfortunately, this approach doesn't fully cut it.
Consider for example:
pub fn veiled() { pub const X: i32 = 1; }
pub fn demo(x: Option<i32>) {
let Some(x) = x else { return };
}where your code suggests crate::veiled::X for x which doesn't resolve since veiled is not a module but a function. def_path_str doesn't care about that as it happily generates pseudo paths which is totally legitimate for it to do, btw.
It's interesting that is_accessible_from doesn't account for that (maybe it shouldn't). Of course, rustc@main is equally incorrect as it suggests veiled::X.
Haven't thought about potential solutions to this yet...
There was a problem hiding this comment.
Furthermore, with_crate_prefix!(…def_path_str…) renders the "principal path" but we would need it to be relative to the current scope since the path might not be accessible here.
Consider:
mod mod1 {
mod mod0 { pub const X: i32 = 1; }
pub use mod0::X;
}
pub fn demo(x: Option<i32>) {
let Some(x) = x else { return };
}Here, you suggest crate::mod1::mod0::X (and main suggests mod1::mod0::X) which isn't accessible inside demo. I'm not sure if we have infra / an API in the compiler that would allow us to find the path if available (here: crate::mod1::X / mod1::X). We probably don't since I figure that can get very complicated very quickly if the re-exports are more complex...
So I'd say let's "just" disqualify this candidate. Clearly is_accessible_from isn't the right check since it follows re-exports it seems. Unclear if there's a method somewhere that would help us out here...
697b3ae to
d4c36cc
Compare
|
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. |
Closes #147595
This fixes
unused_variablestypo suggestions that could print replacementpatterns which are not valid from the binding site.
The fix keeps the existing
def_path_stroutput when it is already valid, butadjusts two local-item cases:
crate::...;It also suppresses const typo suggestions for bare
let x = ...bindings,because replacing the binding with a const path creates a refutable pattern.
Unit ADT typo suggestions are left unchanged.
The regression test covers 2015 and 2021 editions, cross-module consts,
same-module consts, function-local consts, cross-module variants, and bare
letconst initializers.