Skip to content

Commit fae7207

Browse files
committed
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.
1 parent eedc229 commit fae7207

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
@@ -1337,6 +1337,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13371337
mutbl: hir::Mutability,
13381338
track_unstable_candidates: bool,
13391339
) -> Result<(), MethodError<'tcx>> {
1340+
// The errors emitted by this function are part of
1341+
// the arbitrary self types work, and should not impact
1342+
// other users.
1343+
if !self.tcx.features().arbitrary_self_types()
1344+
&& !self.tcx.features().arbitrary_self_types_pointers()
1345+
{
1346+
return Ok(());
1347+
}
1348+
13401349
// We don't want to remember any of the diagnostic hints from this
13411350
// shadow search, but we do need to provide Some/None for the
13421351
// 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)