about summary refs log tree commit diff
path: root/tests/ui/self
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-12-21 01:30:16 +0100
committerGitHub <noreply@github.com>2024-12-21 01:30:16 +0100
commit3201fe98933ee5363cd8fe77627a970bd862688b (patch)
tree68c60fb647566e7a7178d9504de17183a1ff8cfb /tests/ui/self
parentaea7c1d8d7681a22a29215ce16043a00a0898b05 (diff)
parentfae72074c69c2feea0ca7a1522d201a438f2f96b (diff)
downloadrust-3201fe98933ee5363cd8fe77627a970bd862688b.tar.gz
rust-3201fe98933ee5363cd8fe77627a970bd862688b.zip
Rollup merge of #134524 - adetaylor:getref, r=compiler-errors
Arbitrary self types v2: no deshadow pre feature.

The arbitrary self types v2 work introduces a check for shadowed methods, whereby a method in some "outer" smart pointer type may called in preference to a method in the inner referent. This is bad if the outer pointer adds a method later, as it may change behavior, so we ensure we error in this circumstance.

It was intended that this new shadowing detection system only comes into play for users who enable the `arbitrary_self_types` feature (or of course everyone later if it's stabilized). It was believed that the new deshadowing code couldn't be reached without building the custom smart pointers that `arbitrary_self_types` enables, and therefore there was no risk of this code impacting existing users.

However, it turns out that cunning use of `Pin::get_ref` can cause this type of shadowing error to be emitted now. This commit adds a test for this case.

As we want this test to pass without arbitrary_self_types, but fail with it, I've split it into two files (one with run-pass and one without). If there's a better way I can amend it.

Part of #44874

r? ```@wesleywiser```
Diffstat (limited to 'tests/ui/self')
-rw-r--r--tests/ui/self/arbitrary_self_types_pin_getref.feature.stderr16
-rw-r--r--tests/ui/self/arbitrary_self_types_pin_getref.rs25
2 files changed, 41 insertions, 0 deletions
diff --git a/tests/ui/self/arbitrary_self_types_pin_getref.feature.stderr b/tests/ui/self/arbitrary_self_types_pin_getref.feature.stderr
new file mode 100644
index 00000000000..52cf69f33a5
--- /dev/null
+++ b/tests/ui/self/arbitrary_self_types_pin_getref.feature.stderr
@@ -0,0 +1,16 @@
+error[E0034]: multiple applicable items in scope
+  --> $DIR/arbitrary_self_types_pin_getref.rs:23:22
+   |
+LL |     let _ = pinned_a.get_ref();
+   |                      ^^^^^^^ multiple `get_ref` found
+   |
+note: candidate #1 is defined in an impl for the type `A`
+  --> $DIR/arbitrary_self_types_pin_getref.rs:17:5
+   |
+LL |     fn get_ref(self: &Pin<&A>) {}  // note &Pin
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
+   = note: candidate #2 is defined in an impl for the type `Pin<&'a T>`
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0034`.
diff --git a/tests/ui/self/arbitrary_self_types_pin_getref.rs b/tests/ui/self/arbitrary_self_types_pin_getref.rs
new file mode 100644
index 00000000000..29dd907f7ff
--- /dev/null
+++ b/tests/ui/self/arbitrary_self_types_pin_getref.rs
@@ -0,0 +1,25 @@
+// Confirms that Pin::get_ref can no longer shadow methods in pointees
+// once arbitrary_self_types is enabled.
+//
+//@ revisions: default feature
+#![cfg_attr(feature, feature(arbitrary_self_types))]
+
+//@[default] check-pass
+
+#![allow(dead_code)]
+
+use std::pin::Pin;
+use std::pin::pin;
+
+struct A;
+
+impl A {
+    fn get_ref(self: &Pin<&A>) {}  // note &Pin
+}
+
+fn main() {
+    let pinned_a: Pin<&mut A> = pin!(A);
+    let pinned_a: Pin<&A> = pinned_a.as_ref();
+    let _ = pinned_a.get_ref();
+    //[feature]~^ ERROR: multiple applicable items
+}