Skip to content

Commit 3761ada

Browse files
Rollup merge of #85240 - Aaron1011:no-suggest-static, r=davidtwco
Don't suggest adding `'static` lifetime to arguments Fixes #69350 This is almost always the wrong this to do
2 parents 16c8254 + bc6330d commit 3761ada

11 files changed

+10
-41
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
114114
);
115115

116116
diag.span_label(span, format!("lifetime `{}` required", named));
117-
diag.span_suggestion(
118-
new_ty_span,
119-
&format!("add explicit lifetime `{}` to {}", named, span_label_var),
120-
new_ty.to_string(),
121-
Applicability::Unspecified,
122-
);
117+
// Suggesting `'static` is nearly always incorrect, and can steer users
118+
// down the wrong path.
119+
if *named != ty::ReStatic {
120+
diag.span_suggestion(
121+
new_ty_span,
122+
&format!("add explicit lifetime `{}` to {}", named, span_label_var),
123+
new_ty.to_string(),
124+
Applicability::Unspecified,
125+
);
126+
}
123127

124128
Some(diag)
125129
}

src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5
33
|
4-
LL | fn foo(x: &()) {
5-
| --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
64
LL | / bar(|| {
75
LL | |
86
LL | | let _ = x;

src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5
33
|
4-
LL | fn foo(x: &()) {
5-
| --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
64
LL | bar(|| {
75
| ^^^ lifetime `'static` required
86

src/test/ui/generator/generator-region-requirements.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/generator-region-requirements.rs:12:51
33
|
4-
LL | fn dangle(x: &mut i32) -> &'static mut i32 {
5-
| -------- help: add explicit lifetime `'static` to the type of `x`: `&'static mut i32`
6-
...
74
LL | GeneratorState::Complete(c) => return c,
85
| ^ lifetime `'static` required
96

src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/projection-type-lifetime-mismatch.rs:18:5
33
|
4-
LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
5-
| ------------------------------- help: add explicit lifetime `'static` to the type of `x`: `&'static impl for<'a> X<Y<'a> = &'a ()>`
64
LL | x.m()
75
| ^^^^^ lifetime `'static` required
86

97
error[E0621]: explicit lifetime required in the type of `x`
108
--> $DIR/projection-type-lifetime-mismatch.rs:23:5
119
|
12-
LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
13-
| -- help: add explicit lifetime `'static` to the type of `x`: `&'static T`
1410
LL | x.m()
1511
| ^^^^^ lifetime `'static` required
1612

1713
error[E0621]: explicit lifetime required in the type of `x`
1814
--> $DIR/projection-type-lifetime-mismatch.rs:28:5
1915
|
20-
LL | fn h(x: &()) -> &'static () {
21-
| --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
2216
LL | x.m()
2317
| ^^^^^ lifetime `'static` required
2418

src/test/ui/issues/issue-46983.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/issue-46983.rs:2:5
33
|
4-
LL | fn foo(x: &u32) -> &'static u32 {
5-
| ---- help: add explicit lifetime `'static` to the type of `x`: `&'static u32`
64
LL | &*x
75
| ^^^ lifetime `'static` required
86

src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5
33
|
4-
LL | fn foo(x: &u32) -> &'static u32 {
5-
| ---- help: add explicit lifetime `ReStatic` to the type of `x`: `&ReStatic u32`
64
LL | &*x
75
| ^^^ lifetime `ReStatic` required
86

src/test/ui/nll/guarantor-issue-46974.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ LL | *x
1212
error[E0621]: explicit lifetime required in the type of `s`
1313
--> $DIR/guarantor-issue-46974.rs:15:5
1414
|
15-
LL | fn bar(s: &Box<(i32,)>) -> &'static i32 {
16-
| ------------ help: add explicit lifetime `'static` to the type of `s`: `&'static Box<(i32,)>`
17-
LL | // FIXME(#46983): error message should be better
1815
LL | &s.0
1916
| ^^^^ lifetime `'static` required
2017

src/test/ui/regions/regions-static-bound.migrate.nll.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ LL | t
1111
error[E0621]: explicit lifetime required in the type of `u`
1212
--> $DIR/regions-static-bound.rs:14:5
1313
|
14-
LL | fn error(u: &(), v: &()) {
15-
| --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()`
1614
LL | static_id(&u);
1715
| ^^^^^^^^^^^^^ lifetime `'static` required
1816

1917
error[E0621]: explicit lifetime required in the type of `v`
2018
--> $DIR/regions-static-bound.rs:16:5
2119
|
22-
LL | fn error(u: &(), v: &()) {
23-
| --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()`
24-
...
2520
LL | static_id_indirect(&v);
2621
| ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
2722

src/test/ui/regions/regions-static-bound.migrate.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
1414
error[E0621]: explicit lifetime required in the type of `u`
1515
--> $DIR/regions-static-bound.rs:14:5
1616
|
17-
LL | fn error(u: &(), v: &()) {
18-
| --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()`
1917
LL | static_id(&u);
2018
| ^^^^^^^^^ lifetime `'static` required
2119

2220
error[E0621]: explicit lifetime required in the type of `v`
2321
--> $DIR/regions-static-bound.rs:16:5
2422
|
25-
LL | fn error(u: &(), v: &()) {
26-
| --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()`
27-
...
2823
LL | static_id_indirect(&v);
2924
| ^^^^^^^^^^^^^^^^^^ lifetime `'static` required
3025

src/test/ui/regions/regions-static-bound.nll.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ LL | t
1111
error[E0621]: explicit lifetime required in the type of `u`
1212
--> $DIR/regions-static-bound.rs:14:5
1313
|
14-
LL | fn error(u: &(), v: &()) {
15-
| --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()`
1614
LL | static_id(&u);
1715
| ^^^^^^^^^^^^^ lifetime `'static` required
1816

1917
error[E0621]: explicit lifetime required in the type of `v`
2018
--> $DIR/regions-static-bound.rs:16:5
2119
|
22-
LL | fn error(u: &(), v: &()) {
23-
| --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()`
24-
...
2520
LL | static_id_indirect(&v);
2621
| ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
2722

0 commit comments

Comments
 (0)