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
rust-lang#44874
and specifically the sub-issue exploring questions around
generics,
rust-lang#129147
  • Loading branch information
adetaylor committed Sep 4, 2024
1 parent ae9f501 commit 424067a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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: aborting due to 2 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,17 @@
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: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0271`.
38 changes: 38 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,38 @@
//@ 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
}
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 424067a

Please sign in to comment.