Skip to content

Commit

Permalink
Arbitrary self types: tests for 'mismatched types'
Browse files Browse the repository at this point in the history
Adding tests for cases where diagnostics aren't as good as they should
be in the case of arbitrary self types.

These are slightly duplicative of the existing
arbitrary-self-from-method-substs.rs
test, but test more permutations so it seems worth adding to the
coverage as we explore improving the diagnostics here.

Restritions here were suggested in commit 05c5caa

This is a part of the arbitrary self types v2 project,
rust-lang/rfcs#3519
#44874
and specifically the sub-issue exploring questions around
generics,
#129147
  • Loading branch information
adetaylor committed Sep 4, 2024
1 parent ae9f501 commit deb750a
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0658]: `R` cannot be used as the type of `self` without the `arbitrary_self_types` feature
--> $DIR/arbitrary-self-from-method-substs-custom-types.rs:8:43
|
LL | fn get<R: Deref<Target = Self>>(self: R) -> u32 {
| ^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= help: consider changing to `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 1 previous error

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/arbitrary-self-from-method-substs-custom-types.rs:16:5
|
LL | foo.get::<&Foo>();
| ^^^ expected `&Foo`, found `Foo`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
18 changes: 18 additions & 0 deletions tests/ui/self/arbitrary-self-from-method-substs-custom-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ revisions: default feature
#![cfg_attr(feature, feature(arbitrary_self_types))]

use std::ops::Deref;

struct Foo(u32);
impl Foo {
fn get<R: Deref<Target = Self>>(self: R) -> u32 {
//[default]~^ ERROR: `R` cannot be used as the type of `self`
self.0
}
}

fn main() {
let mut foo = Foo(1);
foo.get::<&Foo>();
//[feature]~^ ERROR mismatched types
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0658]: `<FR as FindReceiver>::Receiver` cannot be used as the type of `self` without the `arbitrary_self_types` feature
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:13:36
|
LL | fn get<FR: FindReceiver>(self: FR::Receiver, other: FR) -> u32 {
| ^^^^^^^^^^^^
|
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= help: consider changing to `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[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:36:9
|
LL | foo.get(Silly);
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
|
note: expected this to be `SmartPtr<Foo>`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
|
LL | type Receiver = SmartPtr<Foo>;
| ^^^^^^^^^^^^^
= note: expected struct `SmartPtr<Foo>`
found struct `Foo`

error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:40:9
|
LL | foo.get(Silly);
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
|
note: expected this to be `SmartPtr<Foo>`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
|
LL | type Receiver = SmartPtr<Foo>;
| ^^^^^^^^^^^^^
= note: expected struct `SmartPtr<Foo>`
found reference `&Foo`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0271, E0658.
For more information about an error, try `rustc --explain E0271`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:36:9
|
LL | foo.get(Silly);
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
|
note: expected this to be `SmartPtr<Foo>`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
|
LL | type Receiver = SmartPtr<Foo>;
| ^^^^^^^^^^^^^
= note: expected struct `SmartPtr<Foo>`
found struct `Foo`

error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:40:9
|
LL | foo.get(Silly);
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
|
note: expected this to be `SmartPtr<Foo>`
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
|
LL | type Receiver = SmartPtr<Foo>;
| ^^^^^^^^^^^^^
= note: expected struct `SmartPtr<Foo>`
found reference `&Foo`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0271`.
42 changes: 42 additions & 0 deletions tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//@ revisions: default feature
#![cfg_attr(feature, feature(arbitrary_self_types))]

use std::ops::Deref;
use std::marker::PhantomData;

trait FindReceiver {
type Receiver: Deref<Target = Foo>;
}

struct Foo(u32);
impl Foo {
fn get<FR: FindReceiver>(self: FR::Receiver, other: FR) -> u32 {
//[default]~^ ERROR: `<FR as FindReceiver>::Receiver` cannot be used as the type of `self`
42
}
}

struct SmartPtr<T>(T);
impl<T> Deref for SmartPtr<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}

struct Silly;
impl FindReceiver for Silly {
type Receiver = SmartPtr<Foo>;
}

fn main() {
let mut foo = Foo(1);
// This test is slightly contrived in an attempt to generate a mismatched types
// error for the self type below, without using the turbofish.
foo.get(Silly);
//~^ ERROR type mismatch
let mut foo = Foo(1);
let foo = &foo;
foo.get(Silly);
//~^ ERROR type mismatch
}
38 changes: 38 additions & 0 deletions tests/ui/self/arbitrary_self_types_mismatched.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![feature(arbitrary_self_types)]

use std::ops::Deref;

struct SmartPtr<'a, T: ?Sized>(&'a T);

impl<'a, T: ?Sized> Deref for SmartPtr<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unimplemented!()
}
}

struct SmartPtr2<'a, T: ?Sized>(&'a T);

impl<'a, T: ?Sized> Deref for SmartPtr2<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unimplemented!()
}
}


struct Foo(u32);
impl Foo {
fn a<R: Deref<Target=Self>>(self: R) { }
}

fn main() {
let foo = Foo(1);
let smart_ptr = SmartPtr(&foo);
let smart_ptr2 = SmartPtr2(&foo);
smart_ptr.a(); // this compiles
smart_ptr.a::<SmartPtr2<Foo>>();
//~^ ERROR mismatched types
smart_ptr.a::<&Foo>();
//~^ ERROR mismatched types
}

0 comments on commit deb750a

Please sign in to comment.