Skip to content

Commit 18247c4

Browse files
authored
Rollup merge of rust-lang#56755 - estebank:impl-trait-lt-sugg, r=cramertj
Account for `impl Trait` when suggesting lifetime Fix rust-lang#56745
2 parents 3b0d7da + b9235ea commit 18247c4

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

src/librustc/infer/error_reporting/mod.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -1095,15 +1095,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10951095
let sp = hir.span(id);
10961096
// `sp` only covers `T`, change it so that it covers
10971097
// `T:` when appropriate
1098-
let sp = if has_bounds {
1098+
let is_impl_trait = bound_kind.to_string().starts_with("impl ");
1099+
let sp = if has_bounds && !is_impl_trait {
10991100
sp.to(self.tcx
11001101
.sess
11011102
.source_map()
11021103
.next_point(self.tcx.sess.source_map().next_point(sp)))
11031104
} else {
11041105
sp
11051106
};
1106-
(sp, has_bounds)
1107+
(sp, has_bounds, is_impl_trait)
11071108
})
11081109
} else {
11091110
None
@@ -1136,25 +1137,33 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11361137

11371138
fn binding_suggestion<'tcx, S: fmt::Display>(
11381139
err: &mut DiagnosticBuilder<'tcx>,
1139-
type_param_span: Option<(Span, bool)>,
1140+
type_param_span: Option<(Span, bool, bool)>,
11401141
bound_kind: GenericKind<'tcx>,
11411142
sub: S,
11421143
) {
1143-
let consider = &format!(
1144-
"consider adding an explicit lifetime bound `{}: {}`...",
1145-
bound_kind, sub
1144+
let consider = format!(
1145+
"consider adding an explicit lifetime bound {}",
1146+
if type_param_span.map(|(_, _, is_impl_trait)| is_impl_trait).unwrap_or(false) {
1147+
format!(" `{}` to `{}`...", sub, bound_kind)
1148+
} else {
1149+
format!("`{}: {}`...", bound_kind, sub)
1150+
},
11461151
);
1147-
if let Some((sp, has_lifetimes)) = type_param_span {
1148-
let tail = if has_lifetimes { " + " } else { "" };
1149-
let suggestion = format!("{}: {}{}", bound_kind, sub, tail);
1152+
if let Some((sp, has_lifetimes, is_impl_trait)) = type_param_span {
1153+
let suggestion = if is_impl_trait {
1154+
format!("{} + {}", bound_kind, sub)
1155+
} else {
1156+
let tail = if has_lifetimes { " + " } else { "" };
1157+
format!("{}: {}{}", bound_kind, sub, tail)
1158+
};
11501159
err.span_suggestion_short_with_applicability(
11511160
sp,
1152-
consider,
1161+
&consider,
11531162
suggestion,
11541163
Applicability::MaybeIncorrect, // Issue #41966
11551164
);
11561165
} else {
1157-
err.help(consider);
1166+
err.help(&consider);
11581167
}
11591168
}
11601169

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
3+
use std::fmt::Debug;
4+
5+
fn foo(d: impl Debug + 'static) {
6+
//~^ HELP consider adding an explicit lifetime bound `'static` to `impl Debug`
7+
bar(d);
8+
//~^ ERROR the parameter type `impl Debug` may not live long enough
9+
//~| NOTE ...so that the type `impl Debug` will meet its required lifetime bounds
10+
}
11+
12+
fn bar(d: impl Debug + 'static) {
13+
println!("{:?}", d)
14+
}
15+
16+
fn main() {
17+
foo("hi");
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
3+
use std::fmt::Debug;
4+
5+
fn foo(d: impl Debug) {
6+
//~^ HELP consider adding an explicit lifetime bound `'static` to `impl Debug`
7+
bar(d);
8+
//~^ ERROR the parameter type `impl Debug` may not live long enough
9+
//~| NOTE ...so that the type `impl Debug` will meet its required lifetime bounds
10+
}
11+
12+
fn bar(d: impl Debug + 'static) {
13+
println!("{:?}", d)
14+
}
15+
16+
fn main() {
17+
foo("hi");
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0310]: the parameter type `impl Debug` may not live long enough
2+
--> $DIR/suggest-impl-trait-lifetime.rs:7:5
3+
|
4+
LL | bar(d);
5+
| ^^^
6+
|
7+
note: ...so that the type `impl Debug` will meet its required lifetime bounds
8+
--> $DIR/suggest-impl-trait-lifetime.rs:7:5
9+
|
10+
LL | bar(d);
11+
| ^^^
12+
help: consider adding an explicit lifetime bound `'static` to `impl Debug`...
13+
|
14+
LL | fn foo(d: impl Debug + 'static) {
15+
| ^^^^^^^^^^^^^^^^^^^^
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0310`.

0 commit comments

Comments
 (0)