-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add outlives suggestions for some lifetime errors #58281
Conversation
cc @lorisdanto |
5e3e44a
to
3614f29
Compare
src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs
Outdated
Show resolved
Hide resolved
src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs
Outdated
Show resolved
Hide resolved
src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs
Outdated
Show resolved
Hide resolved
src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks, I did come across that code and started to modify it allow emitting either a counter or a string as the region name. However, I don’t know how to choose a name that isn’t already used in the code.
… On Feb 11, 2019, at 8:04 PM, Esteban Kuber ***@***.***> wrote:
@estebank commented on this pull request.
In src/librustc_mir/borrow_check/nll/region_infer/error_reporting/outlives_suggestion.rs:
> + if let Some(ref mut outlived_frs) = self.constraints_to_add.get_mut(&fr) {
+ outlived_frs.push(outlived_fr);
+ } else {
+ self.constraints_to_add.insert(fr, vec![outlived_fr]);
+ }
+ }
+
+ /// Either get the existing user-define name of the given region or create one.
+ fn lookup_or_create_region_name(
+ &mut self,
+ fr: RegionVid,
+ rinfcx: &RegionInferenceContext<'tcx>,
+ tcx: TyCtxt<'_, '_, 'tcx>,
+ ) {
+ /// All generated names will be of the form `'PREFIXN`, where `N` is a unique integer.
+ const PREFIX: &str = "generatedl";
The NLL code that creates unused names is https://github.com/rust-lang/rust/blob/master/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs, but right now that only uses numbers to refer to unnamed lifetimes. I know that there was other code to create a sequence of named lifetimes that could be copy-pasted into code without being a syntax error (I think it was the g, h, i, j sequence), but don't recall where it was present.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
6f864bc
to
da8e5de
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
So for now, I'm just going to simplify this:
My thought is that this will be a lot easier to implement and still useful. @estebank What do you think? Also do you know how I can check if a lifetime is |
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #64999) made this pull request unmergeable. Please resolve the merge conflicts. |
@estebank Ok, I think this is finally ready for merging. I added a hacky (but effective) workaround: we just check if the printed name would be equal to The problem still seems to be that somewhere an anon region is being synthesized improperly, but that shows up in other diagnostics already with nll compare mode and async. |
Ping from triage. @estebank any updates on this? Thanks. |
☔ The latest upstream changes (presumably #65845) made this pull request unmergeable. Please resolve the merge conflicts. |
rebased |
Hi @estebank! Thanks for your help so far. Will you have a chance to look at this soon? It has been waiting for review for over a month now, and I am afraid it will bitrot again. Is there anything I can do to expedite the process? |
Raised this issue on Discord triage |
} else { | ||
debug!("Region {:?} is NOT suggestable", name); | ||
false | ||
} |
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 should be just name.name().with(|name| 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.
Yeah, I mostly did that for debugging statements
Let's merge as is, but ideally we'd point at the appropriate place for the lifetime restrictions. @bors r+ |
📌 Commit cba0761 has been approved by |
Apologies for the long delay. It is a relatively big PR and there are a couple of things I'd like to improve on it, not to mention the general lack of free time on my side :). Now that the new beta has been cut, I feel more comfortable with approving and further improving on it directly on master. @mark-i-m would you be open to helping me doing that? The good news is that the follow up PRs should be much smaller in size because the bulk of the work is done in this one. |
@estebank Thanks for your time, I realize that this is a large pr. The next couple of weeks are a bit hectic for me, but I'm absolutely interested in improving. |
Add outlives suggestions for some lifetime errors This PR implements suggestion diagnostics for some lifetime mismatch errors. When the borrow checker finds that some lifetime 'a doesn't outlive some other lifetime 'b that it should outlive, then in addition to the current lifetime error, we also emit a suggestion for how to fix the problem by adding a bound: - If a and b are normal named regions, suggest to add the bound `'a: 'b` - If b is static, suggest to replace a with static - If b also needs to outlive a, they must be the same, so suggest unifying them We start with a simpler implementation that avoids diagnostic regression or implementation complexity: - We only makes suggestions for lifetimes the user can already name (eg not closure regions or elided regions) - For now, we only emit a help note, not an actually suggestion because it is significantly easier. Finally, there is one hack: it seems that implicit regions in async fn are given the name '_ incorrectly. To avoid suggesting '_: 'x, we simply filter out such lifetimes by name. For more info, see this internals thread: https://internals.rust-lang.org/t/mechanical-suggestions-for-some-borrow-checker-errors/9049/3 TL;DR Make suggestions to add a `where 'a: 'b` constraint for some lifetime errors. Details are in the paper linked from the internals thread above. r? @estebank TODO - [x] Clean up code - [x] Only make idiomatic suggestions - [x] don't suggest naming `&'a self` - [x] rather than `'a: 'static`, suggest replacing `'a` with `'static` - [x] rather than `'a: 'b, 'b: 'a`, suggest replacing `'a` with `'b` or vice versa - [x] Performance (maybe need a perf run when this is closer to the finish line?) - perf run was clean... - EDIT: perf run seems to only check non-error performance... How do we check that error performance didn't regress? - [x] Needs ui tests - [x] Integrate the `help` message into the main lifetime `error`
☀️ Test successful - checks-azure |
This PR implements suggestion diagnostics for some lifetime mismatch errors. When the borrow checker finds that some lifetime 'a doesn't outlive some other lifetime 'b that it should outlive, then in addition to the current lifetime error, we also emit a suggestion for how to fix the problem by adding a bound:
'a: 'b
We start with a simpler implementation that avoids diagnostic regression or implementation complexity:
Finally, there is one hack: it seems that implicit regions in async fn are given the name '_ incorrectly. To avoid suggesting '_: 'x, we simply filter out such lifetimes by name.
For more info, see this internals thread:
https://internals.rust-lang.org/t/mechanical-suggestions-for-some-borrow-checker-errors/9049/3
TL;DR Make suggestions to add a
where 'a: 'b
constraint for some lifetime errors. Details are in the paper linked from the internals thread above.r? @estebank
TODO
&'a self
'a: 'static
, suggest replacing'a
with'static
'a: 'b, 'b: 'a
, suggest replacing'a
with'b
or vice versahelp
message into the main lifetimeerror