-
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 code suggestion for local enum patterns in non-exhaustive matches #137783
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in match checking cc @Nadrieril |
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 is not the right approach. It's not going to work right when the pattern is nested into another type like (Shadowed::Foo,)
. String manipulation doesn't have enough context to fix a suggestion like this.
You probably want to modify something a bit more close to the root of the problem, like https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_pattern_analysis/rustc/print.rs.html#47-126.
Also, there is no test here.
@rustbot author |
Some changes occurred in exhaustiveness checking cc @Nadrieril |
Thanks! I just changed to a new approach that will force trimming paths (by |
This comment has been minimized.
This comment has been minimized.
@rustbot ready |
@@ -44,12 +44,33 @@ pub(crate) enum EnumInfo<'tcx> { | |||
NotEnum, | |||
} | |||
|
|||
fn erase_path_if_local<'tcx>( |
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.
Can you document what this function is meant to do?
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.
fn erase_path_if_local<'tcx>( | |
/// Determines whether to trim the enum path. | |
/// | |
/// This decision is based on whether the `scrutinee` expression and | |
/// the enum are within the same function or method. | |
fn trim_paths_if_local<'tcx>( |
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.
Doc looks good, I would name it something like should_trim_enum_paths
.
scrut: &thir::Expr<'tcx>, | ||
) -> bool { | ||
let enum_parent_def_id = tcx.parent(adt_def.did()); | ||
let scrut_parent_def_id = if let thir::ExprKind::Scope { region_scope: _, lint_level, value: _ } = |
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'm not super familiar with how lint scopes work in THIR, but this feels very un-robust: there seem to be many reasons for this expression not to be a scope. Can you not use self.match_lint_level
(which is the hir id of the match I believe)?
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 function wants a scope expression surrounding the scrutinee, like:
match v {}
^ -- scope surrounding this
Then we can get the def id of the scrutinee's owner.
Sorry that I can't find an approach to get the def id by not using lint_level
here. :(
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.
Pretty sure a match and its scrutinee always have the same owner def_id; you should be able to use self.match_lint_level.owner.to_def_id()
.
ty::print::with_forced_trimmed_paths!(format!( | ||
"{}::{}", | ||
tcx.def_path_str(adt_def.did()), | ||
variant.name | ||
)) |
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 feels quite ad-hoc: the underlying issue is that def_path_str
does not give a path that can be used in code. Shouldn't there be a function that can return usable paths for a target def_id in the context of a source def_id?
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.
My idea here is that if the scrutinee and the enum both have the same owner (which means they are at the same function or method), then trimming the path is ok because we just want the enum name here(I guess).
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.
Oh perhaps I got what you were worrying about, cc #137845.
It seems that they try to suggest a trimmed paths name in code only requiring that the name is unique, even if the things are in a different module.
Once users apply these code suggestions, then just let the compiler emit the suggestions like "use xxx;" to take users to the right way, which may be the idea of the design (btw it's just my guess, i ain't investigated it for now).
So here the problem is that the path is totally wrong because it is a function name which should not appear.
Idk if I got to the right way but I appreciate it if you're willing to point my problems out.
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.
My worry is that this is a very special-cased fix, where the general underlying issue is "how to emit correct paths in suggestions". Maybe @estebank you would know about this? What's the correct way to get a path that can be used in a suggestion?
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 see... No clues at hand right now. But would be happy to be mentored to solve this.
Fixes: #137682 .
The fixed version of the output be like:
I guess only adjusting the code suggestion is better because
make_index::Shadowed::Foo
is more specific thanShadowed::Foo
in this case.