Skip to content

Commit 18c5dfe

Browse files
committed
Tweak lifetime error
1 parent 322b8df commit 18c5dfe

File tree

6 files changed

+13
-22
lines changed

6 files changed

+13
-22
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
712712
}
713713
}
714714

715+
let mut new_primary_span = false;
715716
// When we have the HIR `Node` at hand, see if we can identify an
716717
// implicit `'static` bound in an `impl dyn Trait {}` and if that's
717718
// the only restriction, suggest relaxing it.
@@ -736,21 +737,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
736737
}
737738
hir::LifetimeName::Static if predicates.is_empty() => {
738739
// `impl dyn Trait + 'static {}`
739-
Some((lt.ident.span, "consider replacing this `'static` requirement", "'_"))
740+
Some((lt.ident.span, "consider relaxing this `'static` requirement", "'_"))
740741
}
741742
_ => None,
742743
};
743744
if let Some((span, msg, sugg)) = suggestion {
744745
// We only emit the suggestion to write `impl dyn Trait + '_ {}` if that's the only
745746
// thing needed.
746747
diag.span_suggestion_verbose(span, msg, sugg, Applicability::MachineApplicable);
747-
// This is redundant but needed because we won't enter the section with the
748-
// additional note, so we point at the method call here too.
749-
diag.replace_span_with(*call_span, false);
750-
diag.span_label(
751-
*call_span,
752-
"calling this method introduces a `'static` lifetime requirement",
753-
);
748+
new_primary_span = true;
754749
} else if let hir::LifetimeName::ImplicitObjectLifetimeDefault
755750
| hir::LifetimeName::Static = lt.res
756751
{
@@ -767,11 +762,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
767762
predicates.push(tcx.def_span(parent));
768763
}
769764
if !predicates.is_empty() {
770-
diag.replace_span_with(*call_span, false);
771-
diag.span_label(
772-
*call_span,
773-
"calling this method introduces a `'static` lifetime requirement",
774-
);
765+
new_primary_span = true;
775766
let a_static_lt = if predicates.len() == 1 {
776767
"a `'static` lifetime requirement"
777768
} else {
@@ -780,6 +771,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
780771
let span: MultiSpan = predicates.into();
781772
diag.span_note(span, format!("the `impl` on `{ty}` has {a_static_lt}"));
782773
}
774+
if new_primary_span && diag.span.primary_span() != Some(*call_span) {
775+
diag.replace_span_with(*call_span, false);
776+
diag.span_label(
777+
*call_span,
778+
"calling this method introduces a `'static` lifetime requirement",
779+
);
780+
}
783781
}
784782

785783
/// Report a specialized error when `FnMut` closures return a reference to a captured variable.

tests/ui/lifetimes/static-impl-obligation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod d {
4545
mod e {
4646
trait Foo {}
4747
impl<'a> Foo for &'a u32 {}
48-
impl dyn Foo + 'static { //~ HELP consider replacing this `'static` requirement
48+
impl dyn Foo + 'static { //~ HELP consider relaxing this `'static` requirement
4949
fn hello(&self) {}
5050
}
5151
fn bar<'a>(x: &'a &'a u32) {

tests/ui/lifetimes/static-impl-obligation.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ LL | let y: &dyn Foo = x;
7777
LL | y.hello();
7878
| ^^^^^^^^^ calling this method introduces a `'static` lifetime requirement
7979
|
80-
help: consider replacing this `'static` requirement
80+
help: consider relaxing this `'static` requirement
8181
|
8282
LL | impl dyn Foo + '_ {
8383
| ~~
@@ -288,7 +288,6 @@ LL | x.hello();
288288
| |
289289
| `x` escapes the function body here
290290
| argument requires that `'a` must outlive `'static`
291-
| calling this method introduces a `'static` lifetime requirement
292291
|
293292
note: the `impl` on `q::Foo` has a `'static` lifetime requirement
294293
--> $DIR/static-impl-obligation.rs:180:18
@@ -308,7 +307,6 @@ LL | x.hello();
308307
| |
309308
| `x` escapes the function body here
310309
| argument requires that `'a` must outlive `'static`
311-
| calling this method introduces a `'static` lifetime requirement
312310
|
313311
note: the `impl` on `r::Foo` has `'static` lifetime requirements
314312
--> $DIR/static-impl-obligation.rs:189:18

tests/ui/regions/issue-78262.base.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | let f = |x: &dyn TT| x.func();
66
| | | |
77
| | | `x` escapes the closure body here
88
| | | argument requires that `'1` must outlive `'static`
9-
| | | calling this method introduces a `'static` lifetime requirement
109
| | let's call the lifetime of this reference `'1`
1110
| `x` is a reference that is only valid in the closure body
1211
|

tests/ui/regions/issue-78262.polonius.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | let f = |x: &dyn TT| x.func();
66
| | | |
77
| | | `x` escapes the closure body here
88
| | | argument requires that `'1` must outlive `'static`
9-
| | | calling this method introduces a `'static` lifetime requirement
109
| | let's call the lifetime of this reference `'1`
1110
| `x` is a reference that is only valid in the closure body
1211
|

tests/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LL | val.use_self::<T>()
1010
| |
1111
| `val` escapes the function body here
1212
| argument requires that `'a` must outlive `'static`
13-
| calling this method introduces a `'static` lifetime requirement
1413
|
1514
note: the used `impl` has a `'static` requirement
1615
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:15:32
@@ -36,7 +35,6 @@ LL | val.use_self()
3635
| |
3736
| `val` escapes the function body here
3837
| argument requires that `'a` must outlive `'static`
39-
| calling this method introduces a `'static` lifetime requirement
4038
|
4139
note: the used `impl` has a `'static` requirement
4240
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:67:14
@@ -106,7 +104,6 @@ LL | val.use_self()
106104
| |
107105
| `val` escapes the function body here
108106
| argument requires that `'a` must outlive `'static`
109-
| calling this method introduces a `'static` lifetime requirement
110107
|
111108
note: the used `impl` has a `'static` requirement
112109
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:32:26

0 commit comments

Comments
 (0)