Skip to content

Commit

Permalink
Arbitrary self types v2: generics test.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adetaylor committed Dec 11, 2024
1 parent a269b31 commit 337af8a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/ui/self/arbitrary_self_types_generic_receiver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![feature(arbitrary_self_types)]

struct PtrA<T>(T);

impl<T> core::ops::Receiver for PtrA<T> {
type Target = T;
}

struct PtrB<T>(T);

trait SomePtr: core::ops::Receiver<Target=<Self as SomePtr>::SomeTarget> {
type SomeTarget;
}

impl<T> SomePtr for PtrB<T> {
type SomeTarget = T;
}

impl<T> core::ops::Receiver for PtrB<T> {
type Target = T;
}

struct Content;

impl Content {
fn a<R: core::ops::Receiver<Target=Self>>(self: &R) {}
//~^ ERROR invalid generic
fn b<R: core::ops::Receiver<Target=Self>>(self: &mut R) {}
//~^ ERROR invalid generic
fn c<R: core::ops::Receiver<Target=Self>>(self: R) {}
//~^ ERROR invalid generic
fn d<R: SomePtr<SomeTarget=Self>>(self: R) {}
//~^ ERROR invalid generic
fn e(self: impl SomePtr<SomeTarget=Self>) {}
//~^ ERROR invalid generic
}

fn main() {
PtrA(Content).a();
PtrA(Content).b();
PtrA(Content).c();
std::rc::Rc::new(Content).a();
std::rc::Rc::new(Content).b();
std::rc::Rc::new(Content).c();
PtrB(Content).a();
PtrB(Content).b();
PtrB(Content).c();
PtrB(Content).d();
PtrB(Content).e();
}
48 changes: 48 additions & 0 deletions tests/ui/self/arbitrary_self_types_generic_receiver.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error[E0801]: invalid generic `self` parameter type: `&R`
--> $DIR/arbitrary_self_types_generic_receiver.rs:26:53
|
LL | fn a<R: core::ops::Receiver<Target=Self>>(self: &R) {}
| ^^
|
= note: type of `self` must not be a method generic parameter type
= 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`)

error[E0801]: invalid generic `self` parameter type: `&mut R`
--> $DIR/arbitrary_self_types_generic_receiver.rs:28:53
|
LL | fn b<R: core::ops::Receiver<Target=Self>>(self: &mut R) {}
| ^^^^^^
|
= note: type of `self` must not be a method generic parameter type
= 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`)

error[E0801]: invalid generic `self` parameter type: `R`
--> $DIR/arbitrary_self_types_generic_receiver.rs:30:53
|
LL | fn c<R: core::ops::Receiver<Target=Self>>(self: R) {}
| ^
|
= note: type of `self` must not be a method generic parameter type
= 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`)

error[E0801]: invalid generic `self` parameter type: `R`
--> $DIR/arbitrary_self_types_generic_receiver.rs:32:45
|
LL | fn d<R: SomePtr<SomeTarget=Self>>(self: R) {}
| ^
|
= note: type of `self` must not be a method generic parameter type
= 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`)

error[E0801]: invalid generic `self` parameter type: `impl SomePtr<SomeTarget = Self>`
--> $DIR/arbitrary_self_types_generic_receiver.rs:34:16
|
LL | fn e(self: impl SomePtr<SomeTarget=Self>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: type of `self` must not be a method generic parameter type
= 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`)

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0801`.

0 comments on commit 337af8a

Please sign in to comment.