Skip to content

Commit d7f0bdd

Browse files
committed
Tweak lifetime error
1 parent 465de33 commit d7f0bdd

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
@@ -702,6 +702,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
702702
}
703703
}
704704

705+
let mut new_primary_span = false;
705706
// When we have the HIR `Node` at hand, see if we can identify an
706707
// implicit `'static` bound in an `impl dyn Trait {}` and if that's
707708
// the only restriction, suggest relaxing it.
@@ -726,21 +727,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
726727
}
727728
hir::LifetimeName::Static if predicates.is_empty() => {
728729
// `impl dyn Trait + 'static {}`
729-
Some((lt.ident.span, "consider replacing this `'static` requirement", "'_"))
730+
Some((lt.ident.span, "consider relaxing this `'static` requirement", "'_"))
730731
}
731732
_ => None,
732733
};
733734
if let Some((span, msg, sugg)) = suggestion {
734735
// We only emit the suggestion to write `impl dyn Trait + '_ {}` if that's the only
735736
// thing needed.
736737
diag.span_suggestion_verbose(span, msg, sugg, Applicability::MachineApplicable);
737-
// This is redundant but needed because we won't enter the section with the
738-
// additional note, so we point at the method call here too.
739-
diag.replace_span_with(*call_span, false);
740-
diag.span_label(
741-
*call_span,
742-
"calling this method introduces a `'static` lifetime requirement",
743-
);
738+
new_primary_span = true;
744739
} else if let hir::LifetimeName::ImplicitObjectLifetimeDefault
745740
| hir::LifetimeName::Static = lt.res
746741
{
@@ -757,11 +752,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
757752
predicates.push(tcx.def_span(parent));
758753
}
759754
if !predicates.is_empty() {
760-
diag.replace_span_with(*call_span, false);
761-
diag.span_label(
762-
*call_span,
763-
"calling this method introduces a `'static` lifetime requirement",
764-
);
755+
new_primary_span = true;
765756
let a_static_lt = if predicates.len() == 1 {
766757
"a `'static` lifetime requirement"
767758
} else {
@@ -770,6 +761,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
770761
let span: MultiSpan = predicates.into();
771762
diag.span_note(span, format!("the `impl` on `{ty}` has {a_static_lt}"));
772763
}
764+
if new_primary_span && diag.span.primary_span() != Some(*call_span) {
765+
diag.replace_span_with(*call_span, false);
766+
diag.span_label(
767+
*call_span,
768+
"calling this method introduces a `'static` lifetime requirement",
769+
);
770+
}
773771
}
774772

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