about summary refs log tree commit diff
path: root/src/test/ui/impl-trait
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-05-29 00:19:53 +0200
committerGitHub <noreply@github.com>2019-05-29 00:19:53 +0200
commite19a229c88e39bc00f5672ff6a539aabe69bb545 (patch)
tree649a7534efe5d5d7e093fd566606f80ca5278018 /src/test/ui/impl-trait
parentd85e256a874b6f1082ef6d51d81630f2f2fd2769 (diff)
parent53e04742543c7034e1da912cc55cfa2a177b8c8b (diff)
downloadrust-e19a229c88e39bc00f5672ff6a539aabe69bb545.tar.gz
rust-e19a229c88e39bc00f5672ff6a539aabe69bb545.zip
Rollup merge of #60756 - matthewjasper:extra-impl-trait-tests, r=nikomatsakis
Add better tests for hidden lifetimes in impl trait

cc #60670
Diffstat (limited to 'src/test/ui/impl-trait')
-rw-r--r--src/test/ui/impl-trait/hidden-lifetimes.rs63
-rw-r--r--src/test/ui/impl-trait/hidden-lifetimes.stderr27
-rw-r--r--src/test/ui/impl-trait/multiple-lifetimes.rs12
3 files changed, 102 insertions, 0 deletions
diff --git a/src/test/ui/impl-trait/hidden-lifetimes.rs b/src/test/ui/impl-trait/hidden-lifetimes.rs
new file mode 100644
index 00000000000..2ee004a37a6
--- /dev/null
+++ b/src/test/ui/impl-trait/hidden-lifetimes.rs
@@ -0,0 +1,63 @@
+// Test to show what happens if we were not careful and allowed invariant
+// lifetimes to escape though an impl trait.
+//
+// Specifically we swap a long lived and short lived reference, giving us a
+// dangling pointer.
+
+use std::cell::RefCell;
+use std::rc::Rc;
+
+trait Swap: Sized {
+    fn swap(self, other: Self);
+}
+
+impl<T> Swap for &mut T {
+    fn swap(self, other: Self) {
+        std::mem::swap(self, other);
+    }
+}
+
+impl<T> Swap for Rc<RefCell<T>> {
+    fn swap(self, other: Self) {
+        <RefCell<T>>::swap(&self, &other);
+    }
+}
+
+// Here we are hiding `'b` making the caller believe that `&'a mut &'s T` and
+// `&'a mut &'l T` are the same type.
+fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
+    //~^ ERROR hidden type
+    x
+}
+
+fn dangle_ref() -> &'static [i32; 3] {
+    let mut res = &[4, 5, 6];
+    let x = [1, 2, 3];
+    hide_ref(&mut res).swap(hide_ref(&mut &x));
+    res
+}
+
+// Here we are hiding `'b` making the caller believe that `Rc<RefCell<&'s T>>`
+// and `Rc<RefCell<&'l T>>` are the same type.
+//
+// This is different to the previous example because the concrete return type
+// only has a single lifetime.
+fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {
+    //~^ ERROR hidden type
+    x
+}
+
+fn dangle_rc_refcell() -> &'static [i32; 3] {
+    let long = Rc::new(RefCell::new(&[4, 5, 6]));
+    let x = [1, 2, 3];
+    let short = Rc::new(RefCell::new(&x));
+    hide_rc_refcell(long.clone()).swap(hide_rc_refcell(short));
+    let res: &'static [i32; 3] = *long.borrow();
+    res
+}
+
+fn main() {
+    // both will print nonsense values.
+    println!("{:?}", dangle_ref());
+    println!("{:?}", dangle_rc_refcell())
+}
diff --git a/src/test/ui/impl-trait/hidden-lifetimes.stderr b/src/test/ui/impl-trait/hidden-lifetimes.stderr
new file mode 100644
index 00000000000..650161753d1
--- /dev/null
+++ b/src/test/ui/impl-trait/hidden-lifetimes.stderr
@@ -0,0 +1,27 @@
+error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
+  --> $DIR/hidden-lifetimes.rs:28:54
+   |
+LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
+   |                                                      ^^^^^^^^^^^^^^
+   |
+note: hidden type `&'a mut &'b T` captures the lifetime 'b as defined on the function body at 28:17
+  --> $DIR/hidden-lifetimes.rs:28:17
+   |
+LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
+   |                 ^^
+
+error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
+  --> $DIR/hidden-lifetimes.rs:45:70
+   |
+LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {
+   |                                                                      ^^^^^^^^^^^^^^
+   |
+note: hidden type `std::rc::Rc<std::cell::RefCell<&'b T>>` captures the lifetime 'b as defined on the function body at 45:24
+  --> $DIR/hidden-lifetimes.rs:45:24
+   |
+LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {
+   |                        ^^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0700`.
diff --git a/src/test/ui/impl-trait/multiple-lifetimes.rs b/src/test/ui/impl-trait/multiple-lifetimes.rs
new file mode 100644
index 00000000000..8346542135b
--- /dev/null
+++ b/src/test/ui/impl-trait/multiple-lifetimes.rs
@@ -0,0 +1,12 @@
+// Test that multiple liftimes are allowed in impl trait types.
+// compile-pass
+
+trait X<'x>: Sized {}
+
+impl<U> X<'_> for U {}
+
+fn multiple_lifeteimes<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl X<'b> + 'a {
+    x
+}
+
+fn main() {}