summaryrefslogtreecommitdiffstats
path: root/src/test/ui/suggestions/lifetimes
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/ui/suggestions/lifetimes')
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.rs26
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr29
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.fixed10
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.rs10
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.stderr15
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs111
-rw-r--r--src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr166
-rw-r--r--src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.rs72
-rw-r--r--src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.stderr78
9 files changed, 517 insertions, 0 deletions
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.rs b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.rs
new file mode 100644
index 000000000..c6802ac6c
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.rs
@@ -0,0 +1,26 @@
+// Regression test for #81650
+
+struct Foo<'a> {
+ x: &'a mut &'a i32,
+}
+
+impl<'a> Foo<'a> {
+ fn bar<F, T>(&self, f: F)
+ where
+ F: FnOnce(&Foo<'a>) -> T,
+ F: 'a,
+ {}
+}
+
+trait Test {
+ fn test(&self);
+}
+
+fn func<T: Test>(foo: &Foo, t: T) {
+ foo.bar(move |_| {
+ //~^ ERROR the parameter type `T` may not live long enough
+ t.test();
+ });
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr
new file mode 100644
index 000000000..0212c2d71
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr
@@ -0,0 +1,29 @@
+error[E0311]: the parameter type `T` may not live long enough
+ --> $DIR/missing-lifetimes-in-signature-2.rs:20:5
+ |
+LL | / foo.bar(move |_| {
+LL | |
+LL | | t.test();
+LL | | });
+ | |______^
+ |
+note: the parameter type `T` must be valid for the anonymous lifetime defined here...
+ --> $DIR/missing-lifetimes-in-signature-2.rs:19:24
+ |
+LL | fn func<T: Test>(foo: &Foo, t: T) {
+ | ^^^
+note: ...so that the type `T` will meet its required lifetime bounds
+ --> $DIR/missing-lifetimes-in-signature-2.rs:20:5
+ |
+LL | / foo.bar(move |_| {
+LL | |
+LL | | t.test();
+LL | | });
+ | |______^
+help: consider adding an explicit lifetime bound...
+ |
+LL | fn func<T: Test + 'a>(foo: &Foo, t: T) {
+ | ++++
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.fixed b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.fixed
new file mode 100644
index 000000000..3c06f4f88
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.fixed
@@ -0,0 +1,10 @@
+// run-rustfix
+// https://github.com/rust-lang/rust/issues/95616
+
+fn buggy_const<'a, const N: usize>(_a: &'a Option<[u8; N]>, _f: &'a str) -> &'a str { //~ERROR [E0106]
+ return "";
+}
+
+fn main() {
+ buggy_const(&Some([69,69,69,69,0]), "test");
+}
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.rs b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.rs
new file mode 100644
index 000000000..110468cbb
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.rs
@@ -0,0 +1,10 @@
+// run-rustfix
+// https://github.com/rust-lang/rust/issues/95616
+
+fn buggy_const<const N: usize>(_a: &Option<[u8; N]>, _f: &str) -> &str { //~ERROR [E0106]
+ return "";
+}
+
+fn main() {
+ buggy_const(&Some([69,69,69,69,0]), "test");
+}
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.stderr
new file mode 100644
index 000000000..7b126c90e
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-before-const.stderr
@@ -0,0 +1,15 @@
+error[E0106]: missing lifetime specifier
+ --> $DIR/missing-lifetimes-in-signature-before-const.rs:4:67
+ |
+LL | fn buggy_const<const N: usize>(_a: &Option<[u8; N]>, _f: &str) -> &str {
+ | ---------------- ---- ^ expected named lifetime parameter
+ |
+ = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `_a` or `_f`
+help: consider introducing a named lifetime parameter
+ |
+LL | fn buggy_const<'a, const N: usize>(_a: &'a Option<[u8; N]>, _f: &'a str) -> &'a str {
+ | +++ ++ ++ ++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0106`.
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs
new file mode 100644
index 000000000..b641f5941
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.rs
@@ -0,0 +1,111 @@
+pub trait Get<T> {
+ fn get(self) -> T;
+}
+
+struct Foo {
+ x: usize,
+}
+
+impl Get<usize> for Foo {
+ fn get(self) -> usize {
+ self.x
+ }
+}
+
+fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
+where
+ G: Get<T>,
+{
+ move || {
+ //~^ ERROR hidden type for `impl FnOnce()` captures lifetime
+ *dest = g.get();
+ }
+}
+
+// After applying suggestion for `foo`:
+fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+where
+ G: Get<T>,
+{
+ move || {
+ //~^ ERROR the parameter type `G` may not live long enough
+ *dest = g.get();
+ }
+}
+
+// After applying suggestion for `bar`:
+fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+//~^ ERROR undeclared lifetime name `'a`
+where
+ G: Get<T>,
+{
+ move || {
+ *dest = g.get();
+ }
+}
+
+// After applying suggestion for `baz`:
+fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+where
+ G: Get<T>,
+{
+ move || {
+ //~^ ERROR the parameter type `G` may not live long enough
+ *dest = g.get();
+ }
+}
+
+// Same as above, but show that we pay attention to lifetime names from parent item
+impl<'a> Foo {
+ fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
+ move || {
+ //~^ ERROR the parameter type `G` may not live long enough
+ *dest = g.get();
+ }
+ }
+}
+
+// After applying suggestion for `qux`:
+fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
+where
+ G: Get<T>,
+{
+ move || {
+ //~^ ERROR the parameter type `G` may not live long enough
+ //~| ERROR explicit lifetime required
+ *dest = g.get();
+ }
+}
+
+// Potential incorrect attempt:
+fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
+where
+ G: Get<T>,
+{
+ move || {
+ //~^ ERROR the parameter type `G` may not live long enough
+ *dest = g.get();
+ }
+}
+
+// We need to tie the lifetime of `G` with the lifetime of `&mut T` and the returned closure:
+fn ok<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a
+where
+ G: Get<T>,
+{
+ move || {
+ *dest = g.get();
+ }
+}
+
+// This also works. The `'_` isn't necessary but it's where we arrive to following the suggestions:
+fn ok2<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + '_ + 'a
+where
+ G: Get<T>,
+{
+ move || {
+ *dest = g.get();
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr
new file mode 100644
index 000000000..0d749f04b
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr
@@ -0,0 +1,166 @@
+error[E0261]: use of undeclared lifetime name `'a`
+ --> $DIR/missing-lifetimes-in-signature.rs:37:11
+ |
+LL | fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+ | - ^^ undeclared lifetime
+ | |
+ | help: consider introducing lifetime `'a` here: `'a,`
+
+error[E0700]: hidden type for `impl FnOnce()` captures lifetime that does not appear in bounds
+ --> $DIR/missing-lifetimes-in-signature.rs:19:5
+ |
+LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
+ | ------ hidden type `[closure@$DIR/missing-lifetimes-in-signature.rs:19:5: 19:12]` captures the anonymous lifetime defined here
+...
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+ |
+help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
+ |
+LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+ | ++++
+
+error[E0311]: the parameter type `G` may not live long enough
+ --> $DIR/missing-lifetimes-in-signature.rs:30:5
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+ |
+note: the parameter type `G` must be valid for the anonymous lifetime defined here...
+ --> $DIR/missing-lifetimes-in-signature.rs:26:26
+ |
+LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+ | ^^^^^^
+note: ...so that the type `G` will meet its required lifetime bounds
+ --> $DIR/missing-lifetimes-in-signature.rs:30:5
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+help: consider adding an explicit lifetime bound...
+ |
+LL | G: Get<T> + 'a,
+ | ++++
+
+error[E0311]: the parameter type `G` may not live long enough
+ --> $DIR/missing-lifetimes-in-signature.rs:52:5
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+ |
+note: the parameter type `G` must be valid for the anonymous lifetime defined here...
+ --> $DIR/missing-lifetimes-in-signature.rs:48:34
+ |
+LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+ | ^^^^^^
+note: ...so that the type `G` will meet its required lifetime bounds
+ --> $DIR/missing-lifetimes-in-signature.rs:52:5
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+help: consider adding an explicit lifetime bound...
+ |
+LL | fn qux<'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
+ | ++++
+
+error[E0311]: the parameter type `G` may not live long enough
+ --> $DIR/missing-lifetimes-in-signature.rs:61:9
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_________^
+ |
+note: the parameter type `G` must be valid for the anonymous lifetime defined here...
+ --> $DIR/missing-lifetimes-in-signature.rs:60:47
+ |
+LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
+ | ^^^^^^
+note: ...so that the type `G` will meet its required lifetime bounds
+ --> $DIR/missing-lifetimes-in-signature.rs:61:9
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_________^
+help: consider adding an explicit lifetime bound...
+ |
+LL | fn qux<'b, G: Get<T> + 'b + 'c, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
+ | ++++
+
+error[E0311]: the parameter type `G` may not live long enough
+ --> $DIR/missing-lifetimes-in-signature.rs:73:5
+ |
+LL | / move || {
+LL | |
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+ |
+note: the parameter type `G` must be valid for the anonymous lifetime defined here...
+ --> $DIR/missing-lifetimes-in-signature.rs:69:34
+ |
+LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
+ | ^^^^^^
+note: ...so that the type `G` will meet its required lifetime bounds
+ --> $DIR/missing-lifetimes-in-signature.rs:73:5
+ |
+LL | / move || {
+LL | |
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^
+help: consider adding an explicit lifetime bound...
+ |
+LL | fn bat<'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
+ | ++++
+
+error[E0621]: explicit lifetime required in the type of `dest`
+ --> $DIR/missing-lifetimes-in-signature.rs:73:5
+ |
+LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
+ | ------ help: add explicit lifetime `'a` to the type of `dest`: `&'a mut T`
+...
+LL | / move || {
+LL | |
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^ lifetime `'a` required
+
+error[E0309]: the parameter type `G` may not live long enough
+ --> $DIR/missing-lifetimes-in-signature.rs:85:5
+ |
+LL | / move || {
+LL | |
+LL | | *dest = g.get();
+LL | | }
+ | |_____^ ...so that the type `G` will meet its required lifetime bounds
+ |
+help: consider adding an explicit lifetime bound...
+ |
+LL | G: Get<T> + 'a,
+ | ++++
+
+error: aborting due to 8 previous errors
+
+Some errors have detailed explanations: E0261, E0309, E0621, E0700.
+For more information about an error, try `rustc --explain E0261`.
diff --git a/src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.rs b/src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.rs
new file mode 100644
index 000000000..ff27011f8
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.rs
@@ -0,0 +1,72 @@
+trait Foo {}
+impl<'a, T: Foo> Foo for &'a T {}
+impl<T: Foo + ?Sized> Foo for Box<T> {}
+
+struct Iter<'a, T> {
+ current: Option<Box<dyn Foo + 'a>>,
+ remaining: T,
+}
+
+impl<'a, T> Iterator for Iter<'a, T>
+where
+ T: Iterator,
+ T::Item: Foo + 'a,
+{
+ type Item = Box<dyn Foo + 'a>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let result = self.current.take();
+ self.current = Box::new(self.remaining.next()).map(|f| Box::new(f) as _);
+ result
+ }
+}
+
+struct Bar(Vec<Box<dyn Foo>>);
+
+impl Bar {
+ fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> {
+ Iter {
+ //~^ ERROR lifetime may not live long enough
+ current: None,
+ remaining: self.0.iter(),
+ }
+ }
+}
+
+struct Baz(Vec<Box<dyn Foo>>);
+
+impl Baz {
+ fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> + '_ {
+ Iter {
+ //~^ ERROR lifetime may not live long enough
+ current: None,
+ remaining: self.0.iter(),
+ }
+ }
+}
+
+struct Bat(Vec<Box<dyn Foo>>);
+
+impl Bat {
+ fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> + 'a {
+ Iter {
+ //~^ ERROR lifetime may not live long enough
+ current: None,
+ remaining: self.0.iter(),
+ }
+ }
+}
+
+struct Ban(Vec<Box<dyn Foo>>);
+
+impl Ban {
+ fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> {
+ Iter {
+ //~^ ERROR lifetime may not live long enough
+ current: None,
+ remaining: self.0.iter(),
+ }
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.stderr b/src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.stderr
new file mode 100644
index 000000000..f49876bcd
--- /dev/null
+++ b/src/test/ui/suggestions/lifetimes/trait-object-nested-in-impl-trait.stderr
@@ -0,0 +1,78 @@
+error: lifetime may not live long enough
+ --> $DIR/trait-object-nested-in-impl-trait.rs:28:9
+ |
+LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> {
+ | - let's call the lifetime of this reference `'1`
+LL | / Iter {
+LL | |
+LL | | current: None,
+LL | | remaining: self.0.iter(),
+LL | | }
+ | |_________^ returning this value requires that `'1` must outlive `'static`
+ |
+help: to declare that the `impl Trait` captures data from argument `self`, you can add an explicit `'_` lifetime bound
+ |
+LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> + '_ {
+ | ++++
+help: to declare that the trait object captures data from argument `self`, you can add an explicit `'_` lifetime bound
+ |
+LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo + '_>> {
+ | ++++
+
+error: lifetime may not live long enough
+ --> $DIR/trait-object-nested-in-impl-trait.rs:40:9
+ |
+LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo>> + '_ {
+ | - let's call the lifetime of this reference `'1`
+LL | / Iter {
+LL | |
+LL | | current: None,
+LL | | remaining: self.0.iter(),
+LL | | }
+ | |_________^ returning this value requires that `'1` must outlive `'static`
+ |
+help: to declare that the trait object captures data from argument `self`, you can add an explicit `'_` lifetime bound
+ |
+LL | fn iter(&self) -> impl Iterator<Item = Box<dyn Foo + '_>> + '_ {
+ | ++++
+
+error: lifetime may not live long enough
+ --> $DIR/trait-object-nested-in-impl-trait.rs:52:9
+ |
+LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> + 'a {
+ | -- lifetime `'a` defined here
+LL | / Iter {
+LL | |
+LL | | current: None,
+LL | | remaining: self.0.iter(),
+LL | | }
+ | |_________^ returning this value requires that `'a` must outlive `'static`
+ |
+help: to declare that the trait object captures data from argument `self`, you can add an explicit `'a` lifetime bound
+ |
+LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo + 'a>> + 'a {
+ | ++++
+
+error: lifetime may not live long enough
+ --> $DIR/trait-object-nested-in-impl-trait.rs:64:9
+ |
+LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> {
+ | -- lifetime `'a` defined here
+LL | / Iter {
+LL | |
+LL | | current: None,
+LL | | remaining: self.0.iter(),
+LL | | }
+ | |_________^ returning this value requires that `'a` must outlive `'static`
+ |
+help: to declare that the `impl Trait` captures data from argument `self`, you can add an explicit `'a` lifetime bound
+ |
+LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo>> + 'a {
+ | ++++
+help: to declare that the trait object captures data from argument `self`, you can add an explicit `'a` lifetime bound
+ |
+LL | fn iter<'a>(&'a self) -> impl Iterator<Item = Box<dyn Foo + 'a>> {
+ | ++++
+
+error: aborting due to 4 previous errors
+