about summary refs log tree commit diff
path: root/tests/ui/nll
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-05-09 20:49:31 +0200
committerGitHub <noreply@github.com>2023-05-09 20:49:31 +0200
commit985ea2248977f90df2dcfbaef8281273ccb46fe7 (patch)
tree1f86aec71b45a49ac5e035cbed793453f5d6fe9c /tests/ui/nll
parente4c82501c2240a09bd082a3ddc03b68dab83f532 (diff)
parent02856110968903bf50a32184db200ccd9b6cc8b6 (diff)
downloadrust-985ea2248977f90df2dcfbaef8281273ccb46fe7.tar.gz
rust-985ea2248977f90df2dcfbaef8281273ccb46fe7.zip
Rollup merge of #111021 - c410-f3r:dqewdas, r=petrochenkov
Move some tests

r? ``@petrochenkov``
Diffstat (limited to 'tests/ui/nll')
-rw-r--r--tests/ui/nll/issue-30438-a.rs23
-rw-r--r--tests/ui/nll/issue-30438-a.stderr12
-rw-r--r--tests/ui/nll/issue-30438-b.rs24
-rw-r--r--tests/ui/nll/issue-30438-b.stderr12
-rw-r--r--tests/ui/nll/issue-30438-c.rs20
-rw-r--r--tests/ui/nll/issue-30438-c.stderr9
-rw-r--r--tests/ui/nll/issue-54302-cases.rs85
-rw-r--r--tests/ui/nll/issue-54302-cases.stderr38
-rw-r--r--tests/ui/nll/issue-54302.rs19
-rw-r--r--tests/ui/nll/issue-54302.stderr11
10 files changed, 253 insertions, 0 deletions
diff --git a/tests/ui/nll/issue-30438-a.rs b/tests/ui/nll/issue-30438-a.rs
new file mode 100644
index 00000000000..0d4eb796ad9
--- /dev/null
+++ b/tests/ui/nll/issue-30438-a.rs
@@ -0,0 +1,23 @@
+// Original regression test for Issue #30438.
+
+use std::ops::Index;
+
+struct Test<'a> {
+    s: &'a String
+}
+
+impl <'a> Index<usize> for Test<'a> {
+    type Output = Test<'a>;
+    fn index(&self, _: usize) -> &Self::Output {
+        return &Test { s: &self.s};
+        //~^ ERROR: cannot return reference to temporary value
+    }
+}
+
+fn main() {
+    let s = "Hello World".to_string();
+    let test = Test{s: &s};
+    let r = &test[0];
+    println!("{}", test.s); // OK since test is valid
+    println!("{}", r.s); // Segfault since value pointed by r has already been dropped
+}
diff --git a/tests/ui/nll/issue-30438-a.stderr b/tests/ui/nll/issue-30438-a.stderr
new file mode 100644
index 00000000000..53845af82fb
--- /dev/null
+++ b/tests/ui/nll/issue-30438-a.stderr
@@ -0,0 +1,12 @@
+error[E0515]: cannot return reference to temporary value
+  --> $DIR/issue-30438-a.rs:12:16
+   |
+LL |         return &Test { s: &self.s};
+   |                ^------------------
+   |                ||
+   |                |temporary value created here
+   |                returns a reference to data owned by the current function
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0515`.
diff --git a/tests/ui/nll/issue-30438-b.rs b/tests/ui/nll/issue-30438-b.rs
new file mode 100644
index 00000000000..79510cdb663
--- /dev/null
+++ b/tests/ui/nll/issue-30438-b.rs
@@ -0,0 +1,24 @@
+// Modified regression test for Issue #30438 that exposed an
+// independent issue (see discussion on ticket).
+
+use std::ops::Index;
+
+struct Test<'a> {
+    s: &'a String
+}
+
+impl <'a> Index<usize> for Test<'a> {
+    type Output = Test<'a>;
+    fn index(&self, _: usize) -> &Self::Output {
+        &Test { s: &self.s}
+        //~^ ERROR: cannot return reference to temporary value
+    }
+}
+
+fn main() {
+    let s = "Hello World".to_string();
+    let test = Test{s: &s};
+    let r = &test[0];
+    println!("{}", test.s); // OK since test is valid
+    println!("{}", r.s); // Segfault since value pointed by r has already been dropped
+}
diff --git a/tests/ui/nll/issue-30438-b.stderr b/tests/ui/nll/issue-30438-b.stderr
new file mode 100644
index 00000000000..fd6bd25b1da
--- /dev/null
+++ b/tests/ui/nll/issue-30438-b.stderr
@@ -0,0 +1,12 @@
+error[E0515]: cannot return reference to temporary value
+  --> $DIR/issue-30438-b.rs:13:9
+   |
+LL |         &Test { s: &self.s}
+   |         ^------------------
+   |         ||
+   |         |temporary value created here
+   |         returns a reference to data owned by the current function
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0515`.
diff --git a/tests/ui/nll/issue-30438-c.rs b/tests/ui/nll/issue-30438-c.rs
new file mode 100644
index 00000000000..813c1d3e2cc
--- /dev/null
+++ b/tests/ui/nll/issue-30438-c.rs
@@ -0,0 +1,20 @@
+// Simplified regression test for #30438, inspired by arielb1.
+
+trait Trait { type Out; }
+
+struct Test<'a> { s: &'a str }
+
+fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
+    let x = Test { s: "this cannot last" };
+    &x
+    //~^ ERROR: cannot return reference to local variable `x`
+}
+
+impl<'b> Trait for Test<'b> { type Out = Test<'b>; }
+
+fn main() {
+    let orig = Test { s: "Hello World" };
+    let r = silly(&orig);
+    println!("{}", orig.s); // OK since `orig` is valid
+    println!("{}", r.s); // Segfault (method does not return a sane value)
+}
diff --git a/tests/ui/nll/issue-30438-c.stderr b/tests/ui/nll/issue-30438-c.stderr
new file mode 100644
index 00000000000..7c001088097
--- /dev/null
+++ b/tests/ui/nll/issue-30438-c.stderr
@@ -0,0 +1,9 @@
+error[E0515]: cannot return reference to local variable `x`
+  --> $DIR/issue-30438-c.rs:9:5
+   |
+LL |     &x
+   |     ^^ returns a reference to data owned by the current function
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0515`.
diff --git a/tests/ui/nll/issue-54302-cases.rs b/tests/ui/nll/issue-54302-cases.rs
new file mode 100644
index 00000000000..faa116269ee
--- /dev/null
+++ b/tests/ui/nll/issue-54302-cases.rs
@@ -0,0 +1,85 @@
+trait Mirror {
+    type Image;
+    fn coerce(self) -> Self::Image;
+}
+
+impl<T> Mirror for T {
+    type Image = T;
+    fn coerce(self) -> Self { self }
+}
+
+trait Foo<'x, T> {
+    fn foo(self) -> &'x T;
+}
+
+impl<'s, 'x, T: 'x> Foo<'x, T> for &'s T where &'s T: Foo2<'x, T> {
+    fn foo(self) -> &'x T { self.foo2() }
+}
+
+trait Foo2<'x, T> {
+    fn foo2(self) -> &'x T;
+}
+
+// example 1 - fails leak check
+impl<'x> Foo2<'x, u32> for &'x u32
+{
+    fn foo2(self) -> &'x u32 { self }
+}
+
+// example 2 - OK with this issue
+impl<'x, 'a: 'x> Foo2<'x, i32> for &'a i32
+{
+    fn foo2(self) -> &'x i32 { self }
+}
+
+// example 3 - fails due to issue #XYZ + Leak-check
+impl<'x, T> Foo2<'x, u64> for T
+    where T: Mirror<Image=&'x u64>
+{
+    fn foo2(self) -> &'x u64 { self.coerce() }
+}
+
+// example 4 - fails due to issue #XYZ
+impl<'x, 'a: 'x, T> Foo2<'x, i64> for T
+    where T: Mirror<Image=&'a i64>
+{
+    fn foo2(self) -> &'x i64 { self.coerce() }
+}
+
+
+trait RefFoo<T> {
+    fn ref_foo(&self) -> &'static T;
+}
+
+impl<T> RefFoo<T> for T where for<'a> &'a T: Foo<'static, T> {
+    fn ref_foo(&self) -> &'static T {
+        self.foo()
+    }
+}
+
+
+fn coerce_lifetime1(a: &u32) -> &'static u32
+{
+    <u32 as RefFoo<u32>>::ref_foo(a)
+    //~^ ERROR not general enough
+}
+
+fn coerce_lifetime2(a: &i32) -> &'static i32
+{
+    <i32 as RefFoo<i32>>::ref_foo(a)
+    //~^ ERROR not general enough
+}
+
+fn coerce_lifetime3(a: &u64) -> &'static u64
+{
+    <u64 as RefFoo<u64>>::ref_foo(a)
+    //~^ ERROR not general enough
+}
+
+fn coerce_lifetime4(a: &i64) -> &'static i64
+{
+    <i64 as RefFoo<i64>>::ref_foo(a)
+    //~^ ERROR not general enough
+}
+
+fn main() {}
diff --git a/tests/ui/nll/issue-54302-cases.stderr b/tests/ui/nll/issue-54302-cases.stderr
new file mode 100644
index 00000000000..6e8b69c4bee
--- /dev/null
+++ b/tests/ui/nll/issue-54302-cases.stderr
@@ -0,0 +1,38 @@
+error: implementation of `Foo` is not general enough
+  --> $DIR/issue-54302-cases.rs:63:5
+   |
+LL |     <u32 as RefFoo<u32>>::ref_foo(a)
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
+   |
+   = note: `Foo<'static, u32>` would have to be implemented for the type `&'0 u32`, for any lifetime `'0`...
+   = note: ...but `Foo<'_, u32>` is actually implemented for the type `&'1 u32`, for some specific lifetime `'1`
+
+error: implementation of `Foo` is not general enough
+  --> $DIR/issue-54302-cases.rs:69:5
+   |
+LL |     <i32 as RefFoo<i32>>::ref_foo(a)
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
+   |
+   = note: `Foo<'static, i32>` would have to be implemented for the type `&'0 i32`, for any lifetime `'0`...
+   = note: ...but `Foo<'_, i32>` is actually implemented for the type `&'1 i32`, for some specific lifetime `'1`
+
+error: implementation of `Foo` is not general enough
+  --> $DIR/issue-54302-cases.rs:75:5
+   |
+LL |     <u64 as RefFoo<u64>>::ref_foo(a)
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
+   |
+   = note: `Foo<'static, u64>` would have to be implemented for the type `&'0 u64`, for any lifetime `'0`...
+   = note: ...but `Foo<'_, u64>` is actually implemented for the type `&'1 u64`, for some specific lifetime `'1`
+
+error: implementation of `Foo` is not general enough
+  --> $DIR/issue-54302-cases.rs:81:5
+   |
+LL |     <i64 as RefFoo<i64>>::ref_foo(a)
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
+   |
+   = note: `Foo<'static, i64>` would have to be implemented for the type `&'0 i64`, for any lifetime `'0`...
+   = note: ...but `Foo<'_, i64>` is actually implemented for the type `&'1 i64`, for some specific lifetime `'1`
+
+error: aborting due to 4 previous errors
+
diff --git a/tests/ui/nll/issue-54302.rs b/tests/ui/nll/issue-54302.rs
new file mode 100644
index 00000000000..1bfaebc3895
--- /dev/null
+++ b/tests/ui/nll/issue-54302.rs
@@ -0,0 +1,19 @@
+trait Deserialize<'de> {}
+
+trait DeserializeOwned: for<'de> Deserialize<'de> {}
+impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}
+
+// Based on this impl, `&'static str` only implements Deserialize<'static>.
+// It does not implement for<'de> Deserialize<'de>.
+impl<'de: 'a, 'a> Deserialize<'de> for &'a str {}
+
+fn main() {
+    // Then why does it implement DeserializeOwned? This compiles.
+    fn assert_deserialize_owned<T: DeserializeOwned>() {}
+    assert_deserialize_owned::<&'static str>();
+    //~^ ERROR not general enough
+
+    // It correctly does not implement for<'de> Deserialize<'de>.
+    //fn assert_hrtb<T: for<'de> Deserialize<'de>>() {}
+    //assert_hrtb::<&'static str>();
+}
diff --git a/tests/ui/nll/issue-54302.stderr b/tests/ui/nll/issue-54302.stderr
new file mode 100644
index 00000000000..26c46571f9c
--- /dev/null
+++ b/tests/ui/nll/issue-54302.stderr
@@ -0,0 +1,11 @@
+error: implementation of `Deserialize` is not general enough
+  --> $DIR/issue-54302.rs:13:5
+   |
+LL |     assert_deserialize_owned::<&'static str>();
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Deserialize` is not general enough
+   |
+   = note: `&'static str` must implement `Deserialize<'0>`, for any lifetime `'0`...
+   = note: ...but `&str` actually implements `Deserialize<'1>`, for some specific lifetime `'1`
+
+error: aborting due to previous error
+