-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Refactor region_name
: add RegionNameHighlight
#74661
Conversation
out of highlight_if_we_can_match_hir_ty_from_argument, which is then renamed
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @estebank (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
match self.give_region_a_name(*outlived_fr).unwrap().source { | ||
RegionNameSource::NamedEarlyBoundRegion(fr_span) | ||
| RegionNameSource::NamedFreeRegion(fr_span) | ||
| RegionNameSource::SynthesizedFreeEnvRegion(fr_span, _) | ||
| RegionNameSource::CannotMatchHirTy(fr_span, _) | ||
| RegionNameSource::MatchedHirTy(fr_span) | ||
| RegionNameSource::MatchedAdtAndSegment(fr_span) | ||
| RegionNameSource::AnonRegionFromUpvar(fr_span, _) | ||
| RegionNameSource::AnonRegionFromOutput(fr_span, _, _) => { | ||
diag.span_label(fr_span, "inferred to be a `FnMut` closure"); | ||
} | ||
_ => {} |
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 match was missing a few variants that have spans (AnonRegionFromYieldTy
/AnonRegionFromAsyncFn
). I don't think that was deliberate, just that they were allowed to be absent by the underscore pattern. On the other hand I'm not sure that they would be used in this code.
.and_then(|arg_hir_ty| self.highlight_if_we_can_match_hir_ty(fr, arg_ty, arg_hir_ty)) | ||
.or_else(|| { | ||
// `highlight_if_we_cannot_match_hir_ty` needs to know the number we will give to | ||
// the anonymous region. If it succeeds, the `synthesize_region_name` call below | ||
// will increment the counter, "reserving" the number we just used. | ||
let counter = *self.next_region_name.try_borrow().unwrap(); | ||
self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span, counter) | ||
}) |
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.
The idea is that this bit (from the highlight_if_we_can_match_hir_ty
call down) can be repeated to create RegionNameHighlight
s in other places. I would have extracted this into its own function in this PR, but I'm a bit concerned about this business with counter
, and if increasing the syntactic distance from the synthesize_region_name
call will make things unclear.
@bors r+ These look like they will be quite useful. Really glad to see these changes. |
📌 Commit 8a776ee has been approved by |
…r=estebank Refactor `region_name`: add `RegionNameHighlight` This PR does not change any diagnostics itself, rather it enables further code changes, but I would like to get approval for the refactoring first before making use of it. In `rustc_mir::borrow_check::diagnostics::region_name`, there is code that allows for, when giving a synthesized name like `'1` to an anonymous lifetime, pointing at e.g. the exact '`&`' that introduces the lifetime. This PR decouples that code from the specific case of arguments, adding a new enum `RegionNameHighlight`, enabling future changes to use it in other places. This allows: * We could change the other `AnonRegionFrom*` variants to use `RegionNameHighlight` to precisely point at where lifetimes are introduced in other locations when they have type annotations, e.g. a closure return `|...| -> &i32`. * Because of how async functions are lowered this affects async functions as well, see rust-lang#74072 * for rust-lang#74597, we could add a second, optional `RegionNameHighlight` to the `AnonRegionFromArgument` variant that highlights a lifetime in the return type of a function when, due to elision, this is the same as the argument lifetime. * in rust-lang#74497 (comment) I noticed that a diagnostic was trying to introduce a lifetime `'2` in the opaque type `impl std::future::Future`. The code for the case of arguments has [code to handle cases like this](https://github.com/rust-lang/rust/blob/bbebe7351fcd29af1eb9a35e315369b15887ea09/src/librustc_mir/borrow_check/diagnostics/region_name.rs#L365) but not the others. This refactoring would allow the same code path to handle this. * It might be appropriate to add another variant of `RegionNameHighlight` to say something like `lifetime '1 appears in the opaque type impl std::future::Future`. These are quite a few changes so I thought I would make sure the refactoring is OK before I start making changes that rely on it. :)
…arth Rollup of 8 pull requests Successful merges: - rust-lang#72954 (revise RwLock for HermitCore) - rust-lang#74367 (Rearrange the pipeline of `pow` to gain efficiency) - rust-lang#74491 (Optimize away BitAnd and BitOr when possible) - rust-lang#74639 (Downgrade glibc to 2.11.1 for ppc, ppc64 and s390x) - rust-lang#74661 (Refactor `region_name`: add `RegionNameHighlight`) - rust-lang#74692 (delay_span_bug instead of silent ignore) - rust-lang#74698 (fixed error reporting for mismatched traits) - rust-lang#74715 (Add a system for creating diffs across multiple mir optimizations.) Failed merges: r? @ghost
This PR does not change any diagnostics itself, rather it enables further code changes, but I would like to get approval for the refactoring first before making use of it.
In
rustc_mir::borrow_check::diagnostics::region_name
, there is code that allows for, when giving a synthesized name like'1
to an anonymous lifetime, pointing at e.g. the exact '&
' that introduces the lifetime.This PR decouples that code from the specific case of arguments, adding a new enum
RegionNameHighlight
, enabling future changes to use it in other places.This allows:
AnonRegionFrom*
variants to useRegionNameHighlight
to precisely point at where lifetimes are introduced in other locations when they have type annotations, e.g. a closure return|...| -> &i32
.'1
without defining it #74072RegionNameHighlight
to theAnonRegionFromArgument
variant that highlights a lifetime in the return type of a function when, due to elision, this is the same as the argument lifetime.'2
in the opaque typeimpl std::future::Future
. The code for the case of arguments has code to handle cases like this but not the others. This refactoring would allow the same code path to handle this.RegionNameHighlight
to say something likelifetime '1 appears in the opaque type impl std::future::Future
.These are quite a few changes so I thought I would make sure the refactoring is OK before I start making changes that rely on it. :)