Skip to content

Commit 2905f14

Browse files
committed
Account for type X = impl Trait; in lifetime suggestion
1 parent bc1571c commit 2905f14

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
800800
// If there is a static predicate, then the only sensible suggestion is to replace
801801
// fr with `'static`.
802802
if has_static_predicate {
803-
diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str,));
803+
diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str));
804804
} else {
805805
// Otherwise, we should suggest adding a constraint on the return type.
806806
let span = infcx.tcx.def_span(*did);
@@ -810,15 +810,20 @@ impl<'tcx> RegionInferenceContext<'tcx> {
810810
} else {
811811
"'_".to_string()
812812
};
813-
813+
let suggestion = if snippet.ends_with(";") {
814+
// `type X = impl Trait;`
815+
format!("{} + {};", &snippet[..snippet.len() - 1], suggestable_fr_name)
816+
} else {
817+
format!("{} + {}", snippet, suggestable_fr_name)
818+
};
814819
diag.span_suggestion(
815820
span,
816821
&format!(
817822
"to allow this `impl Trait` to capture borrowed data with lifetime \
818823
`{}`, add `{}` as a bound",
819824
fr_name, suggestable_fr_name,
820825
),
821-
format!("{} + {}", snippet, suggestable_fr_name),
826+
suggestion,
822827
Applicability::MachineApplicable,
823828
);
824829
}

src/test/ui/impl-trait/multiple-lifetimes/error-handling.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn foo<'a, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> {
77
= help: consider replacing `'a` with `'static`
88
help: to allow this `impl Trait` to capture borrowed data with lifetime `'a`, add `'a` as a bound
99
|
10-
LL | type E<'a, 'b> = impl Sized; + 'a
10+
LL | type E<'a, 'b> = impl Sized + 'a;
1111
|
1212

1313
error: aborting due to previous error

0 commit comments

Comments
 (0)