Skip to content

Commit aea7c1d

Browse files
authored
Rollup merge of #134509 - adetaylor:niche-deshadowing-tests, r=wesleywiser
Arbitrary self types v2: niche deshadowing test Arbitrary self types v2 attempts to detect cases where methods in an "outer" type (e.g. a smart pointer) might "shadow" methods in the referent. There are a couple of cases where the current code makes no attempt to detect such shadowing. Both of these cases only apply if other unstable features are enabled. Add a test, mostly for illustrative purposes, so we can see the shadowing cases that can occur. Part of #44874 r? ```@wesleywiser```
2 parents f3b19f5 + cb88030 commit aea7c1d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//@ run-pass
2+
3+
#![allow(dead_code)]
4+
#![allow(incomplete_features)]
5+
6+
#![feature(arbitrary_self_types)]
7+
#![feature(arbitrary_self_types_pointers)]
8+
#![feature(pin_ergonomics)]
9+
10+
use std::pin::Pin;
11+
use std::pin::pin;
12+
use std::marker::PhantomData;
13+
14+
struct A;
15+
16+
impl A {
17+
fn m(self: *const SmartPtr<Self>) -> usize { 2 }
18+
fn n(self: *const SmartPtr<Self>) -> usize { 2 }
19+
20+
fn o(self: Pin<&SmartPtr2<Self>>) -> usize { 2 }
21+
fn p(self: Pin<&SmartPtr2<Self>>) -> usize { 2 }
22+
}
23+
24+
struct SmartPtr<T>(T);
25+
26+
impl<T> core::ops::Receiver for SmartPtr<T> {
27+
type Target = *mut T;
28+
}
29+
30+
impl<T> SmartPtr<T> {
31+
// In general we try to detect cases where a method in a smart pointer
32+
// "shadows" a method in the referent (in this test, A).
33+
// This method "shadows" the 'n' method in the inner type A
34+
// We do not attempt to produce an error in these shadowing cases
35+
// since the type signature of this method and the corresponding
36+
// method in A are pretty unlikely to occur in practice,
37+
// and because it shows up conflicts between *const::cast and *mut::cast.
38+
fn n(self: *mut Self) -> usize { 1 }
39+
}
40+
41+
struct SmartPtr2<'a, T>(T, PhantomData<&'a T>);
42+
43+
impl<'a, T> core::ops::Receiver for SmartPtr2<'a, T> {
44+
type Target = Pin<&'a mut T>;
45+
}
46+
47+
impl<T> SmartPtr2<'_, T> {
48+
// Similarly, this method shadows the method in A
49+
// Can only happen with the unstable feature pin_ergonomics
50+
fn p(self: Pin<&mut Self>) -> usize { 1 }
51+
}
52+
53+
fn main() {
54+
let mut sm = SmartPtr(A);
55+
let smp: *mut SmartPtr<A> = &mut sm as *mut SmartPtr<A>;
56+
assert_eq!(smp.m(), 2);
57+
assert_eq!(smp.n(), 1);
58+
59+
let smp: Pin<&mut SmartPtr2<A>> = pin!(SmartPtr2(A, PhantomData));
60+
assert_eq!(smp.o(), 2);
61+
let smp: Pin<&mut SmartPtr2<A>> = pin!(SmartPtr2(A, PhantomData));
62+
assert_eq!(smp.p(), 1);
63+
}

0 commit comments

Comments
 (0)