Skip to content

Commit deb750a

Browse files
committed
Arbitrary self types: tests for 'mismatched types'
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
1 parent ae9f501 commit deb750a

7 files changed

+195
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0658]: `R` cannot be used as the type of `self` without the `arbitrary_self_types` feature
2+
--> $DIR/arbitrary-self-from-method-substs-custom-types.rs:8:43
3+
|
4+
LL | fn get<R: Deref<Target = Self>>(self: R) -> u32 {
5+
| ^
6+
|
7+
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
8+
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
= 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`)
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/arbitrary-self-from-method-substs-custom-types.rs:16:5
3+
|
4+
LL | foo.get::<&Foo>();
5+
| ^^^ expected `&Foo`, found `Foo`
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ revisions: default feature
2+
#![cfg_attr(feature, feature(arbitrary_self_types))]
3+
4+
use std::ops::Deref;
5+
6+
struct Foo(u32);
7+
impl Foo {
8+
fn get<R: Deref<Target = Self>>(self: R) -> u32 {
9+
//[default]~^ ERROR: `R` cannot be used as the type of `self`
10+
self.0
11+
}
12+
}
13+
14+
fn main() {
15+
let mut foo = Foo(1);
16+
foo.get::<&Foo>();
17+
//[feature]~^ ERROR mismatched types
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0658]: `<FR as FindReceiver>::Receiver` cannot be used as the type of `self` without the `arbitrary_self_types` feature
2+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:13:36
3+
|
4+
LL | fn get<FR: FindReceiver>(self: FR::Receiver, other: FR) -> u32 {
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: see issue #44874 <https://github.com/rust-lang/rust/issues/44874> for more information
8+
= help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
= 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`)
11+
12+
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
13+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:36:9
14+
|
15+
LL | foo.get(Silly);
16+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
17+
|
18+
note: expected this to be `SmartPtr<Foo>`
19+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
20+
|
21+
LL | type Receiver = SmartPtr<Foo>;
22+
| ^^^^^^^^^^^^^
23+
= note: expected struct `SmartPtr<Foo>`
24+
found struct `Foo`
25+
26+
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
27+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:40:9
28+
|
29+
LL | foo.get(Silly);
30+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
31+
|
32+
note: expected this to be `SmartPtr<Foo>`
33+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
34+
|
35+
LL | type Receiver = SmartPtr<Foo>;
36+
| ^^^^^^^^^^^^^
37+
= note: expected struct `SmartPtr<Foo>`
38+
found reference `&Foo`
39+
40+
error: aborting due to 3 previous errors
41+
42+
Some errors have detailed explanations: E0271, E0658.
43+
For more information about an error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
2+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:36:9
3+
|
4+
LL | foo.get(Silly);
5+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
6+
|
7+
note: expected this to be `SmartPtr<Foo>`
8+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
9+
|
10+
LL | type Receiver = SmartPtr<Foo>;
11+
| ^^^^^^^^^^^^^
12+
= note: expected struct `SmartPtr<Foo>`
13+
found struct `Foo`
14+
15+
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
16+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:40:9
17+
|
18+
LL | foo.get(Silly);
19+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
20+
|
21+
note: expected this to be `SmartPtr<Foo>`
22+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:29:21
23+
|
24+
LL | type Receiver = SmartPtr<Foo>;
25+
| ^^^^^^^^^^^^^
26+
= note: expected struct `SmartPtr<Foo>`
27+
found reference `&Foo`
28+
29+
error: aborting due to 2 previous errors
30+
31+
For more information about this error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//@ revisions: default feature
2+
#![cfg_attr(feature, feature(arbitrary_self_types))]
3+
4+
use std::ops::Deref;
5+
use std::marker::PhantomData;
6+
7+
trait FindReceiver {
8+
type Receiver: Deref<Target = Foo>;
9+
}
10+
11+
struct Foo(u32);
12+
impl Foo {
13+
fn get<FR: FindReceiver>(self: FR::Receiver, other: FR) -> u32 {
14+
//[default]~^ ERROR: `<FR as FindReceiver>::Receiver` cannot be used as the type of `self`
15+
42
16+
}
17+
}
18+
19+
struct SmartPtr<T>(T);
20+
impl<T> Deref for SmartPtr<T> {
21+
type Target = T;
22+
fn deref(&self) -> &Self::Target {
23+
&self.0
24+
}
25+
}
26+
27+
struct Silly;
28+
impl FindReceiver for Silly {
29+
type Receiver = SmartPtr<Foo>;
30+
}
31+
32+
fn main() {
33+
let mut foo = Foo(1);
34+
// This test is slightly contrived in an attempt to generate a mismatched types
35+
// error for the self type below, without using the turbofish.
36+
foo.get(Silly);
37+
//~^ ERROR type mismatch
38+
let mut foo = Foo(1);
39+
let foo = &foo;
40+
foo.get(Silly);
41+
//~^ ERROR type mismatch
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(arbitrary_self_types)]
2+
3+
use std::ops::Deref;
4+
5+
struct SmartPtr<'a, T: ?Sized>(&'a T);
6+
7+
impl<'a, T: ?Sized> Deref for SmartPtr<'a, T> {
8+
type Target = T;
9+
fn deref(&self) -> &Self::Target {
10+
unimplemented!()
11+
}
12+
}
13+
14+
struct SmartPtr2<'a, T: ?Sized>(&'a T);
15+
16+
impl<'a, T: ?Sized> Deref for SmartPtr2<'a, T> {
17+
type Target = T;
18+
fn deref(&self) -> &Self::Target {
19+
unimplemented!()
20+
}
21+
}
22+
23+
24+
struct Foo(u32);
25+
impl Foo {
26+
fn a<R: Deref<Target=Self>>(self: R) { }
27+
}
28+
29+
fn main() {
30+
let foo = Foo(1);
31+
let smart_ptr = SmartPtr(&foo);
32+
let smart_ptr2 = SmartPtr2(&foo);
33+
smart_ptr.a(); // this compiles
34+
smart_ptr.a::<SmartPtr2<Foo>>();
35+
//~^ ERROR mismatched types
36+
smart_ptr.a::<&Foo>();
37+
//~^ ERROR mismatched types
38+
}

0 commit comments

Comments
 (0)