about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorXiangfei Ding <dingxiangfei2009@protonmail.ch>2024-05-26 17:57:13 +0800
committerDing Xiang Fei <dingxiangfei2009@protonmail.ch>2024-06-24 03:03:34 +0800
commitf1be59fa72b06c4a28acc59a0f0dff1484db0778 (patch)
tree2abe2b107ae2a78d206e33c0fed974ea5e5a291b /tests
parentaabbf84b45a5e7b868c33e959d7e5cc985097d19 (diff)
downloadrust-f1be59fa72b06c4a28acc59a0f0dff1484db0778.tar.gz
rust-f1be59fa72b06c4a28acc59a0f0dff1484db0778.zip
SmartPointer derive-macro
Co-authored-by: Wedson Almeida Filho <walmeida@microsoft.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/deriving/deriving-smart-pointer.rs55
-rw-r--r--tests/ui/feature-gates/feature-gate-derive-smart-pointer.rs9
-rw-r--r--tests/ui/feature-gates/feature-gate-derive-smart-pointer.stderr33
3 files changed, 97 insertions, 0 deletions
diff --git a/tests/ui/deriving/deriving-smart-pointer.rs b/tests/ui/deriving/deriving-smart-pointer.rs
new file mode 100644
index 00000000000..cfc3369850b
--- /dev/null
+++ b/tests/ui/deriving/deriving-smart-pointer.rs
@@ -0,0 +1,55 @@
+//@ run-pass
+#![feature(derive_smart_pointer, arbitrary_self_types)]
+
+use std::marker::SmartPointer;
+
+#[derive(SmartPointer)]
+struct MyPointer<'a, #[pointee] T: ?Sized> {
+    ptr: &'a T,
+}
+
+impl<T: ?Sized> Copy for MyPointer<'_, T> {}
+impl<T: ?Sized> Clone for MyPointer<'_, T> {
+    fn clone(&self) -> Self {
+        Self { ptr: self.ptr }
+    }
+}
+
+impl<'a, T: ?Sized> core::ops::Deref for MyPointer<'a, T> {
+    type Target = T;
+    fn deref(&self) -> &'a T {
+        self.ptr
+    }
+}
+
+struct MyValue(u32);
+impl MyValue {
+    fn through_pointer(self: MyPointer<'_, Self>) -> u32 {
+        self.ptr.0
+    }
+}
+
+trait MyTrait {
+    fn through_trait(&self) -> u32;
+    fn through_trait_and_pointer(self: MyPointer<'_, Self>) -> u32;
+}
+
+impl MyTrait for MyValue {
+    fn through_trait(&self) -> u32 {
+        self.0
+    }
+
+    fn through_trait_and_pointer(self: MyPointer<'_, Self>) -> u32 {
+        self.ptr.0
+    }
+}
+
+pub fn main() {
+    let v = MyValue(10);
+    let ptr = MyPointer { ptr: &v };
+    assert_eq!(v.0, ptr.through_pointer());
+    assert_eq!(v.0, ptr.through_pointer());
+    let dptr = ptr as MyPointer<dyn MyTrait>;
+    assert_eq!(v.0, dptr.through_trait());
+    assert_eq!(v.0, dptr.through_trait_and_pointer());
+}
diff --git a/tests/ui/feature-gates/feature-gate-derive-smart-pointer.rs b/tests/ui/feature-gates/feature-gate-derive-smart-pointer.rs
new file mode 100644
index 00000000000..ae8005592cd
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-derive-smart-pointer.rs
@@ -0,0 +1,9 @@
+use std::marker::SmartPointer; //~ ERROR use of unstable library feature 'derive_smart_pointer'
+
+#[derive(SmartPointer)] //~ ERROR use of unstable library feature 'derive_smart_pointer'
+struct MyPointer<'a, #[pointee] T: ?Sized> {
+    //~^ ERROR the `#[pointee]` attribute is an experimental feature
+    ptr: &'a T,
+}
+
+fn main() {}
diff --git a/tests/ui/feature-gates/feature-gate-derive-smart-pointer.stderr b/tests/ui/feature-gates/feature-gate-derive-smart-pointer.stderr
new file mode 100644
index 00000000000..0ffd82fb9e1
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-derive-smart-pointer.stderr
@@ -0,0 +1,33 @@
+error[E0658]: use of unstable library feature 'derive_smart_pointer'
+  --> $DIR/feature-gate-derive-smart-pointer.rs:3:10
+   |
+LL | #[derive(SmartPointer)]
+   |          ^^^^^^^^^^^^
+   |
+   = note: see issue #123430 <https://github.com/rust-lang/rust/issues/123430> for more information
+   = help: add `#![feature(derive_smart_pointer)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[E0658]: the `#[pointee]` attribute is an experimental feature
+  --> $DIR/feature-gate-derive-smart-pointer.rs:4:22
+   |
+LL | struct MyPointer<'a, #[pointee] T: ?Sized> {
+   |                      ^^^^^^^^^^
+   |
+   = note: see issue #123430 <https://github.com/rust-lang/rust/issues/123430> for more information
+   = help: add `#![feature(derive_smart_pointer)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[E0658]: use of unstable library feature 'derive_smart_pointer'
+  --> $DIR/feature-gate-derive-smart-pointer.rs:1:5
+   |
+LL | use std::marker::SmartPointer;
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #123430 <https://github.com/rust-lang/rust/issues/123430> for more information
+   = help: add `#![feature(derive_smart_pointer)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0658`.