Skip to content
/ rustc Public
forked from rust-lang/rust

Commit

Permalink
Account for type X = impl Trait; in lifetime suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jan 6, 2020
1 parent bc1571c commit 2905f14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/librustc_mir/borrow_check/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// If there is a static predicate, then the only sensible suggestion is to replace
// fr with `'static`.
if has_static_predicate {
diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str,));
diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str));
} else {
// Otherwise, we should suggest adding a constraint on the return type.
let span = infcx.tcx.def_span(*did);
Expand All @@ -810,15 +810,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
} else {
"'_".to_string()
};

let suggestion = if snippet.ends_with(";") {
// `type X = impl Trait;`
format!("{} + {};", &snippet[..snippet.len() - 1], suggestable_fr_name)
} else {
format!("{} + {}", snippet, suggestable_fr_name)
};
diag.span_suggestion(
span,
&format!(
"to allow this `impl Trait` to capture borrowed data with lifetime \
`{}`, add `{}` as a bound",
fr_name, suggestable_fr_name,
),
format!("{} + {}", snippet, suggestable_fr_name),
suggestion,
Applicability::MachineApplicable,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn foo<'a, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
= help: consider replacing `'a` with `'static`
help: to allow this `impl Trait` to capture borrowed data with lifetime `'a`, add `'a` as a bound
|
LL | type E<'a, 'b> = impl Sized; + 'a
LL | type E<'a, 'b> = impl Sized + 'a;
|

error: aborting due to previous error
Expand Down

0 comments on commit 2905f14

Please sign in to comment.