From bc6330d47a7a81e25f492a837822121a3cd88158 Mon Sep 17 00:00:00 2001
From: Aaron Hill <aa1ronham@gmail.com>
Date: Wed, 12 May 2021 21:39:37 -0400
Subject: [PATCH] Don't suggest adding `'static` lifetime to arguments

Fixes #69350

This is almost always the wrong this to do
---
 .../nice_region_error/named_anon_conflict.rs     | 16 ++++++++++------
 ...ounds-static-cant-capture-borrowed.nll.stderr |  2 --
 ...re-bounds-static-cant-capture-borrowed.stderr |  2 --
 .../generator-region-requirements.stderr         |  3 ---
 .../projection-type-lifetime-mismatch.stderr     |  6 ------
 src/test/ui/issues/issue-46983.stderr            |  2 --
 ...egion-lbr-anon-does-not-outlive-static.stderr |  2 --
 src/test/ui/nll/guarantor-issue-46974.stderr     |  3 ---
 .../regions-static-bound.migrate.nll.stderr      |  5 -----
 .../regions/regions-static-bound.migrate.stderr  |  5 -----
 .../ui/regions/regions-static-bound.nll.stderr   |  5 -----
 11 files changed, 10 insertions(+), 41 deletions(-)

diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
index 2f3c0d6957a..0878f8550da 100644
--- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
+++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs
@@ -114,12 +114,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
         );
 
         diag.span_label(span, format!("lifetime `{}` required", named));
-        diag.span_suggestion(
-            new_ty_span,
-            &format!("add explicit lifetime `{}` to {}", named, span_label_var),
-            new_ty.to_string(),
-            Applicability::Unspecified,
-        );
+        // Suggesting `'static` is nearly always incorrect, and can steer users
+        // down the wrong path.
+        if *named != ty::ReStatic {
+            diag.span_suggestion(
+                new_ty_span,
+                &format!("add explicit lifetime `{}` to {}", named, span_label_var),
+                new_ty.to_string(),
+                Applicability::Unspecified,
+            );
+        }
 
         Some(diag)
     }
diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr
index 1d12e2f585e..1b0e0902d67 100644
--- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr
+++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr
@@ -1,8 +1,6 @@
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5
    |
-LL |   fn foo(x: &()) {
-   |             --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
 LL | /     bar(|| {
 LL | |
 LL | |         let _ = x;
diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr
index f50c3e3b531..a9add6184f1 100644
--- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr
+++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr
@@ -1,8 +1,6 @@
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5
    |
-LL | fn foo(x: &()) {
-   |           --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
 LL |     bar(|| {
    |     ^^^ lifetime `'static` required
 
diff --git a/src/test/ui/generator/generator-region-requirements.stderr b/src/test/ui/generator/generator-region-requirements.stderr
index 53d48bc4f56..de90a599e76 100644
--- a/src/test/ui/generator/generator-region-requirements.stderr
+++ b/src/test/ui/generator/generator-region-requirements.stderr
@@ -1,9 +1,6 @@
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/generator-region-requirements.rs:12:51
    |
-LL | fn dangle(x: &mut i32) -> &'static mut i32 {
-   |              -------- help: add explicit lifetime `'static` to the type of `x`: `&'static mut i32`
-...
 LL |             GeneratorState::Complete(c) => return c,
    |                                                   ^ lifetime `'static` required
 
diff --git a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr
index 13b765dfa57..aec87862566 100644
--- a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr
+++ b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr
@@ -1,24 +1,18 @@
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/projection-type-lifetime-mismatch.rs:18:5
    |
-LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
-   |         ------------------------------- help: add explicit lifetime `'static` to the type of `x`: `&'static impl for<'a> X<Y<'a> = &'a ()>`
 LL |     x.m()
    |     ^^^^^ lifetime `'static` required
 
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/projection-type-lifetime-mismatch.rs:23:5
    |
-LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
-   |                                       -- help: add explicit lifetime `'static` to the type of `x`: `&'static T`
 LL |     x.m()
    |     ^^^^^ lifetime `'static` required
 
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/projection-type-lifetime-mismatch.rs:28:5
    |
-LL | fn h(x: &()) -> &'static () {
-   |         --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
 LL |     x.m()
    |     ^^^^^ lifetime `'static` required
 
diff --git a/src/test/ui/issues/issue-46983.stderr b/src/test/ui/issues/issue-46983.stderr
index 8a4a6bdb39f..d328329edad 100644
--- a/src/test/ui/issues/issue-46983.stderr
+++ b/src/test/ui/issues/issue-46983.stderr
@@ -1,8 +1,6 @@
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/issue-46983.rs:2:5
    |
-LL | fn foo(x: &u32) -> &'static u32 {
-   |           ---- help: add explicit lifetime `'static` to the type of `x`: `&'static u32`
 LL |     &*x
    |     ^^^ lifetime `'static` required
 
diff --git a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr
index e813f21e01d..4c302d935db 100644
--- a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr
+++ b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr
@@ -1,8 +1,6 @@
 error[E0621]: explicit lifetime required in the type of `x`
   --> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5
    |
-LL | fn foo(x: &u32) -> &'static u32 {
-   |           ---- help: add explicit lifetime `ReStatic` to the type of `x`: `&ReStatic u32`
 LL |     &*x
    |     ^^^ lifetime `ReStatic` required
 
diff --git a/src/test/ui/nll/guarantor-issue-46974.stderr b/src/test/ui/nll/guarantor-issue-46974.stderr
index 361466c4d54..eabc3105c02 100644
--- a/src/test/ui/nll/guarantor-issue-46974.stderr
+++ b/src/test/ui/nll/guarantor-issue-46974.stderr
@@ -12,9 +12,6 @@ LL |     *x
 error[E0621]: explicit lifetime required in the type of `s`
   --> $DIR/guarantor-issue-46974.rs:15:5
    |
-LL | fn bar(s: &Box<(i32,)>) -> &'static i32 {
-   |           ------------ help: add explicit lifetime `'static` to the type of `s`: `&'static Box<(i32,)>`
-LL |     // FIXME(#46983): error message should be better
 LL |     &s.0
    |     ^^^^ lifetime `'static` required
 
diff --git a/src/test/ui/regions/regions-static-bound.migrate.nll.stderr b/src/test/ui/regions/regions-static-bound.migrate.nll.stderr
index 6f2c75d2eba..a4c8e721145 100644
--- a/src/test/ui/regions/regions-static-bound.migrate.nll.stderr
+++ b/src/test/ui/regions/regions-static-bound.migrate.nll.stderr
@@ -11,17 +11,12 @@ LL |     t
 error[E0621]: explicit lifetime required in the type of `u`
   --> $DIR/regions-static-bound.rs:14:5
    |
-LL | fn error(u: &(), v: &()) {
-   |             --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()`
 LL |     static_id(&u);
    |     ^^^^^^^^^^^^^ lifetime `'static` required
 
 error[E0621]: explicit lifetime required in the type of `v`
   --> $DIR/regions-static-bound.rs:16:5
    |
-LL | fn error(u: &(), v: &()) {
-   |                     --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()`
-...
 LL |     static_id_indirect(&v);
    |     ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
 
diff --git a/src/test/ui/regions/regions-static-bound.migrate.stderr b/src/test/ui/regions/regions-static-bound.migrate.stderr
index 6e631d40d45..644458e2063 100644
--- a/src/test/ui/regions/regions-static-bound.migrate.stderr
+++ b/src/test/ui/regions/regions-static-bound.migrate.stderr
@@ -14,17 +14,12 @@ LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
 error[E0621]: explicit lifetime required in the type of `u`
   --> $DIR/regions-static-bound.rs:14:5
    |
-LL | fn error(u: &(), v: &()) {
-   |             --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()`
 LL |     static_id(&u);
    |     ^^^^^^^^^ lifetime `'static` required
 
 error[E0621]: explicit lifetime required in the type of `v`
   --> $DIR/regions-static-bound.rs:16:5
    |
-LL | fn error(u: &(), v: &()) {
-   |                     --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()`
-...
 LL |     static_id_indirect(&v);
    |     ^^^^^^^^^^^^^^^^^^ lifetime `'static` required
 
diff --git a/src/test/ui/regions/regions-static-bound.nll.stderr b/src/test/ui/regions/regions-static-bound.nll.stderr
index 6f2c75d2eba..a4c8e721145 100644
--- a/src/test/ui/regions/regions-static-bound.nll.stderr
+++ b/src/test/ui/regions/regions-static-bound.nll.stderr
@@ -11,17 +11,12 @@ LL |     t
 error[E0621]: explicit lifetime required in the type of `u`
   --> $DIR/regions-static-bound.rs:14:5
    |
-LL | fn error(u: &(), v: &()) {
-   |             --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()`
 LL |     static_id(&u);
    |     ^^^^^^^^^^^^^ lifetime `'static` required
 
 error[E0621]: explicit lifetime required in the type of `v`
   --> $DIR/regions-static-bound.rs:16:5
    |
-LL | fn error(u: &(), v: &()) {
-   |                     --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()`
-...
 LL |     static_id_indirect(&v);
    |     ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required