Skip to content

Commit 337af8a

Browse files
committed
Arbitrary self types v2: generics test.
There's some discussion on the RFC about whether generic receivers should be allowed, but in the end the conclusion was that they should be blocked (at least for some definition of 'generic'). This blocking landed in an earlier PR; this commit adds additional tests to ensure the interaction with the rest of the Arbitrary Self Types v2 feature is as expected. This test may be a little duplicative but it seems better to land it than not.
1 parent a269b31 commit 337af8a

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#![feature(arbitrary_self_types)]
2+
3+
struct PtrA<T>(T);
4+
5+
impl<T> core::ops::Receiver for PtrA<T> {
6+
type Target = T;
7+
}
8+
9+
struct PtrB<T>(T);
10+
11+
trait SomePtr: core::ops::Receiver<Target=<Self as SomePtr>::SomeTarget> {
12+
type SomeTarget;
13+
}
14+
15+
impl<T> SomePtr for PtrB<T> {
16+
type SomeTarget = T;
17+
}
18+
19+
impl<T> core::ops::Receiver for PtrB<T> {
20+
type Target = T;
21+
}
22+
23+
struct Content;
24+
25+
impl Content {
26+
fn a<R: core::ops::Receiver<Target=Self>>(self: &R) {}
27+
//~^ ERROR invalid generic
28+
fn b<R: core::ops::Receiver<Target=Self>>(self: &mut R) {}
29+
//~^ ERROR invalid generic
30+
fn c<R: core::ops::Receiver<Target=Self>>(self: R) {}
31+
//~^ ERROR invalid generic
32+
fn d<R: SomePtr<SomeTarget=Self>>(self: R) {}
33+
//~^ ERROR invalid generic
34+
fn e(self: impl SomePtr<SomeTarget=Self>) {}
35+
//~^ ERROR invalid generic
36+
}
37+
38+
fn main() {
39+
PtrA(Content).a();
40+
PtrA(Content).b();
41+
PtrA(Content).c();
42+
std::rc::Rc::new(Content).a();
43+
std::rc::Rc::new(Content).b();
44+
std::rc::Rc::new(Content).c();
45+
PtrB(Content).a();
46+
PtrB(Content).b();
47+
PtrB(Content).c();
48+
PtrB(Content).d();
49+
PtrB(Content).e();
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0801]: invalid generic `self` parameter type: `&R`
2+
--> $DIR/arbitrary_self_types_generic_receiver.rs:26:53
3+
|
4+
LL | fn a<R: core::ops::Receiver<Target=Self>>(self: &R) {}
5+
| ^^
6+
|
7+
= note: type of `self` must not be a method generic parameter type
8+
= help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
9+
10+
error[E0801]: invalid generic `self` parameter type: `&mut R`
11+
--> $DIR/arbitrary_self_types_generic_receiver.rs:28:53
12+
|
13+
LL | fn b<R: core::ops::Receiver<Target=Self>>(self: &mut R) {}
14+
| ^^^^^^
15+
|
16+
= note: type of `self` must not be a method generic parameter type
17+
= help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
18+
19+
error[E0801]: invalid generic `self` parameter type: `R`
20+
--> $DIR/arbitrary_self_types_generic_receiver.rs:30:53
21+
|
22+
LL | fn c<R: core::ops::Receiver<Target=Self>>(self: R) {}
23+
| ^
24+
|
25+
= note: type of `self` must not be a method generic parameter type
26+
= help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
27+
28+
error[E0801]: invalid generic `self` parameter type: `R`
29+
--> $DIR/arbitrary_self_types_generic_receiver.rs:32:45
30+
|
31+
LL | fn d<R: SomePtr<SomeTarget=Self>>(self: R) {}
32+
| ^
33+
|
34+
= note: type of `self` must not be a method generic parameter type
35+
= help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
36+
37+
error[E0801]: invalid generic `self` parameter type: `impl SomePtr<SomeTarget = Self>`
38+
--> $DIR/arbitrary_self_types_generic_receiver.rs:34:16
39+
|
40+
LL | fn e(self: impl SomePtr<SomeTarget=Self>) {}
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
|
43+
= note: type of `self` must not be a method generic parameter type
44+
= help: use a concrete type such as `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
45+
46+
error: aborting due to 5 previous errors
47+
48+
For more information about this error, try `rustc --explain E0801`.

0 commit comments

Comments
 (0)