about summary refs log tree commit diff
path: root/tests/ui/impl-trait
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-02-24 15:35:12 +0100
committerGitHub <noreply@github.com>2024-02-24 15:35:12 +0100
commitb10ef3c6bc8ba9cf5392f52387dd07879f4e5767 (patch)
tree61640be1c613841e750a0d4968c7a2d37226a520 /tests/ui/impl-trait
parent8f359beca4e58bc3ae795a666301a8f47023044c (diff)
parent5e6da720f621308a0f44e7c6bbd13a9fad68b240 (diff)
downloadrust-b10ef3c6bc8ba9cf5392f52387dd07879f4e5767.tar.gz
rust-b10ef3c6bc8ba9cf5392f52387dd07879f4e5767.zip
Rollup merge of #121435 - estebank:rpitit-static-119773, r=compiler-errors
Account for RPITIT in E0310 explicit lifetime constraint suggestion

When given

```rust
trait Original {
    fn f() -> impl Fn();
}

trait Erased {
    fn f(&self) -> Box<dyn Fn()>;
}

impl<T: Original> Erased for T {
    fn f(&self) -> Box<dyn Fn()> {
        Box::new(<T as Original>::f())
    }
}
```

emit do not emit an invalid suggestion restricting the `Trait::{opaque}` type in a `where` clause:

```
error[E0310]: the associated type `<T as Original>::{opaque#0}` may not live long enough
  --> $DIR/missing-static-bound-from-impl.rs:11:9
   |
LL |         Box::new(<T as Original>::f())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |         |
   |         the associated type `<T as Original>::{opaque#0}` must be valid for the static lifetime...
   |         ...so that the type `impl Fn()` will meet its required lifetime bounds
```

Partially address #119773. Ideally we'd suggest modifying `Erased::f` instead.

r? `@compiler-errors`
Diffstat (limited to 'tests/ui/impl-trait')
-rw-r--r--tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr2
-rw-r--r--tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs16
-rw-r--r--tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr12
3 files changed, 28 insertions, 2 deletions
diff --git a/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr b/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr
index 79a86b0a3ae..15aa3cf54bb 100644
--- a/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr
+++ b/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr
@@ -6,8 +6,6 @@ LL |     async fn foo() -> &'static impl T;
    |     |
    |     the associated type `<Self as MyTrait>::{opaque#0}` must be valid for the static lifetime...
    |     ...so that the reference type `&'static impl T` does not outlive the data it points at
-   |
-   = help: consider adding an explicit lifetime bound `<Self as MyTrait>::{opaque#0}: 'static`...
 
 error: aborting due to 1 previous error
 
diff --git a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs
new file mode 100644
index 00000000000..a36799c3ebd
--- /dev/null
+++ b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs
@@ -0,0 +1,16 @@
+trait Original {
+    fn f() -> impl Fn();
+}
+
+trait Erased {
+    fn f(&self) -> Box<dyn Fn()>;
+}
+
+impl<T: Original> Erased for T {
+    fn f(&self) -> Box<dyn Fn()> {
+        Box::new(<T as Original>::f())
+        //~^ ERROR the associated type `<T as Original>::{opaque#0}` may not live long enough
+    }
+}
+
+fn main () {}
diff --git a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr
new file mode 100644
index 00000000000..5ec0ee38347
--- /dev/null
+++ b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr
@@ -0,0 +1,12 @@
+error[E0310]: the associated type `<T as Original>::{opaque#0}` may not live long enough
+  --> $DIR/missing-static-bound-from-impl.rs:11:9
+   |
+LL |         Box::new(<T as Original>::f())
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         the associated type `<T as Original>::{opaque#0}` must be valid for the static lifetime...
+   |         ...so that the type `impl Fn()` will meet its required lifetime bounds
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0310`.