summaryrefslogtreecommitdiffstats
path: root/tests/ui/nll/outlives-suggestion-simple.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/nll/outlives-suggestion-simple.rs
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/nll/outlives-suggestion-simple.rs')
-rw-r--r--tests/ui/nll/outlives-suggestion-simple.rs75
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/ui/nll/outlives-suggestion-simple.rs b/tests/ui/nll/outlives-suggestion-simple.rs
new file mode 100644
index 000000000..2a5c31e3a
--- /dev/null
+++ b/tests/ui/nll/outlives-suggestion-simple.rs
@@ -0,0 +1,75 @@
+// Test the simplest of outlives suggestions.
+
+fn foo1<'a, 'b>(x: &'a usize) -> &'b usize {
+ x //~ERROR lifetime may not live long enough
+}
+
+fn foo2<'a>(x: &'a usize) -> &'static usize {
+ x //~ERROR lifetime may not live long enough
+}
+
+fn foo3<'a, 'b>(x: &'a usize, y: &'b usize) -> (&'b usize, &'a usize) {
+ (x, y) //~ERROR lifetime may not live long enough
+ //~^ERROR lifetime may not live long enough
+}
+
+fn foo4<'a, 'b, 'c>(x: &'a usize) -> (&'b usize, &'c usize) {
+ // FIXME: ideally, we suggest 'a: 'b + 'c, but as of today (may 04, 2019), the null error
+ // reporting stops after the first error in a MIR def so as not to produce too many errors, so
+ // currently we only report 'a: 'b. The user would then re-run and get another error.
+ (x, x) //~ERROR lifetime may not live long enough
+}
+
+struct Foo<'a> {
+ x: &'a usize,
+}
+
+impl Foo<'static> {
+ pub fn foo<'a>(x: &'a usize) -> Self {
+ Foo { x } //~ERROR lifetime may not live long enough
+ }
+}
+
+struct Bar<'a> {
+ x: &'a usize,
+}
+
+impl<'a> Bar<'a> {
+ pub fn get<'b>(&self) -> &'b usize {
+ self.x //~ERROR lifetime may not live long enough
+ }
+}
+
+// source: https://stackoverflow.com/questions/41417057/why-do-i-get-a-lifetime-error-when-i-use-a-mutable-reference-in-a-struct-instead
+struct Baz<'a> {
+ x: &'a mut i32,
+}
+
+impl<'a> Baz<'a> {
+ fn get<'b>(&'b self) -> &'a i32 {
+ self.x //~ERROR lifetime may not live long enough
+ }
+}
+
+// source: https://stackoverflow.com/questions/41204134/rust-lifetime-error
+struct Bar2<'a> {
+ bar: &'a str,
+}
+impl<'a> Bar2<'a> {
+ fn new(foo: &'a Foo2<'a>) -> Bar2<'a> {
+ Bar2 { bar: foo.raw }
+ }
+}
+
+pub struct Foo2<'a> {
+ raw: &'a str,
+ cell: std::cell::Cell<&'a str>,
+}
+impl<'a> Foo2<'a> {
+ // should not produce outlives suggestions to name 'self
+ fn get_bar(&self) -> Bar2 {
+ Bar2::new(&self) //~ERROR lifetime may not live long enough
+ }
+}
+
+fn main() {}