Skip to content

Commit afa5995

Browse files
committed
Tweak lifetime error
1 parent 8949695 commit afa5995

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
@@ -711,6 +711,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
711711
}
712712
}
713713

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

784782
/// 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)