Skip to content

Commit 3201fe9

Browse files
authored
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```
2 parents aea7c1d + fae7207 commit 3201fe9

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Diff for: compiler/rustc_hir_typeck/src/method/probe.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13291329
mutbl: hir::Mutability,
13301330
track_unstable_candidates: bool,
13311331
) -> Result<(), MethodError<'tcx>> {
1332+
// The errors emitted by this function are part of
1333+
// the arbitrary self types work, and should not impact
1334+
// other users.
1335+
if !self.tcx.features().arbitrary_self_types()
1336+
&& !self.tcx.features().arbitrary_self_types_pointers()
1337+
{
1338+
return Ok(());
1339+
}
1340+
13321341
// We don't want to remember any of the diagnostic hints from this
13331342
// shadow search, but we do need to provide Some/None for the
13341343
// unstable_candidates in order to reflect the behavior of the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0034]: multiple applicable items in scope
2+
--> $DIR/arbitrary_self_types_pin_getref.rs:23:22
3+
|
4+
LL | let _ = pinned_a.get_ref();
5+
| ^^^^^^^ multiple `get_ref` found
6+
|
7+
note: candidate #1 is defined in an impl for the type `A`
8+
--> $DIR/arbitrary_self_types_pin_getref.rs:17:5
9+
|
10+
LL | fn get_ref(self: &Pin<&A>) {} // note &Pin
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: candidate #2 is defined in an impl for the type `Pin<&'a T>`
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0034`.

Diff for: tests/ui/self/arbitrary_self_types_pin_getref.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Confirms that Pin::get_ref can no longer shadow methods in pointees
2+
// once arbitrary_self_types is enabled.
3+
//
4+
//@ revisions: default feature
5+
#![cfg_attr(feature, feature(arbitrary_self_types))]
6+
7+
//@[default] check-pass
8+
9+
#![allow(dead_code)]
10+
11+
use std::pin::Pin;
12+
use std::pin::pin;
13+
14+
struct A;
15+
16+
impl A {
17+
fn get_ref(self: &Pin<&A>) {} // note &Pin
18+
}
19+
20+
fn main() {
21+
let pinned_a: Pin<&mut A> = pin!(A);
22+
let pinned_a: Pin<&A> = pinned_a.as_ref();
23+
let _ = pinned_a.get_ref();
24+
//[feature]~^ ERROR: multiple applicable items
25+
}

0 commit comments

Comments
 (0)