Skip to content

Commit

Permalink
Suggest adding a lifetime constraint when opaque type is responsible …
Browse files Browse the repository at this point in the history
…for "does not live long enough" error
  • Loading branch information
ohadravid committed Dec 25, 2019
1 parent 84d8f9d commit 94089ad
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/librustc_mir/borrow_check/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc::mir::{
use rustc::ty::adjustment::PointerCast;
use rustc::ty::{self, TyCtxt};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::DiagnosticBuilder;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_index::vec::IndexVec;
use syntax_pos::symbol::Symbol;
use syntax_pos::Span;
Expand Down Expand Up @@ -206,6 +206,47 @@ impl BorrowExplanation {
),
);
};

self.add_lifetime_bound_suggestion_to_diagnostic(
tcx,
err,
&category,
span,
region_name,
);
}
_ => {}
}
}
pub(in crate::borrow_check) fn add_lifetime_bound_suggestion_to_diagnostic<'tcx>(
&self,
tcx: TyCtxt<'tcx>,
err: &mut DiagnosticBuilder<'_>,
category: &ConstraintCategory,
span: Span,
region_name: &RegionName,
) {
match category {
ConstraintCategory::OpaqueType => {
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) {
let suggestable_name = if region_name.was_named() {
region_name.to_string()
} else {
"'_".to_string()
};

err.span_suggestion(
span,
&format!(
"you can add a constraint to the {}to make it last less than \
`'static` and match `{}`",
category.description(),
region_name,
),
format!("{} + {}", snippet, suggestable_name),
Applicability::Unspecified,
);
}
}
_ => {}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/impl-trait/does-not-live-long-enough.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct List {
data: Vec<String>,
}
impl List {
fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
//~^ ERROR does not live long enough
}
}

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/impl-trait/does-not-live-long-enough.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0597]: `prefix` does not live long enough
--> $DIR/does-not-live-long-enough.rs:6:51
|
LL | fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
| -- lifetime `'a` defined here --------------------------- opaque type requires that `prefix` is borrowed for `'a`
LL | self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
| --- ^^^^^^ borrowed value does not live long enough
| |
| value captured here
LL |
LL | }
| - `prefix` dropped here while still borrowed
|
help: you can add a constraint to the opaque type to make it last less than `'static` and match `'a`
|
LL | fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> + 'a {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

0 comments on commit 94089ad

Please sign in to comment.