Skip to content

Commit f13ebc7

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 #44874 and specifically the sub-issue exploring questions around generics, #129147
1 parent ae9f501 commit f13ebc7

10 files changed

+289
-0
lines changed
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/arbitrary-self-from-method-substs-mismatches.rs:34:5
3+
|
4+
LL | smart_ptr.a::<SmartPtr2<Foo>>();
5+
| ^^^^^^^^^ expected `SmartPtr2<'_, Foo>`, found `SmartPtr<'_, Foo>`
6+
|
7+
= note: expected struct `SmartPtr2<'_, Foo>`
8+
found struct `SmartPtr<'_, Foo>`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/arbitrary-self-from-method-substs-mismatches.rs:36:5
12+
|
13+
LL | smart_ptr.a::<&Foo>();
14+
| ^^^^^^^^^ expected `&Foo`, found `SmartPtr<'_, Foo>`
15+
|
16+
= note: expected reference `&Foo`
17+
found struct `SmartPtr<'_, Foo, >`
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.
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:28:9
14+
|
15+
LL | foo.get(Silly);
16+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
17+
|
18+
note: expected this to be `Rc<Foo>`
19+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21
20+
|
21+
LL | type Receiver = std::rc::Rc<Foo>;
22+
| ^^^^^^^^^^^^^^^^
23+
= note: expected struct `Rc<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:32:9
28+
|
29+
LL | foo.get(Silly);
30+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
31+
|
32+
note: expected this to be `Rc<Foo>`
33+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21
34+
|
35+
LL | type Receiver = std::rc::Rc<Foo>;
36+
| ^^^^^^^^^^^^^^^^
37+
= note: expected struct `Rc<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:28:9
3+
|
4+
LL | foo.get(Silly);
5+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo`
6+
|
7+
note: expected this to be `Rc<Foo>`
8+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21
9+
|
10+
LL | type Receiver = std::rc::Rc<Foo>;
11+
| ^^^^^^^^^^^^^^^^
12+
= note: expected struct `Rc<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:32:9
17+
|
18+
LL | foo.get(Silly);
19+
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo`
20+
|
21+
note: expected this to be `Rc<Foo>`
22+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21
23+
|
24+
LL | type Receiver = std::rc::Rc<Foo>;
25+
| ^^^^^^^^^^^^^^^^
26+
= note: expected struct `Rc<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,34 @@
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 Silly;
20+
impl FindReceiver for Silly {
21+
type Receiver = std::rc::Rc<Foo>;
22+
}
23+
24+
fn main() {
25+
let mut foo = Foo(1);
26+
// This test is slightly contrived in an attempt to generate a mismatched types
27+
// error for the self type below, without using the turbofish.
28+
foo.get(Silly);
29+
//~^ ERROR type mismatch
30+
let mut foo = Foo(1);
31+
let foo = &foo;
32+
foo.get(Silly);
33+
//~^ ERROR type mismatch
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(arbitrary_self_types)]
2+
3+
struct Thing<R>(std::marker::PhantomData<R>);
4+
5+
impl<R: std::ops::Deref<Target = Self>> Thing<R> {
6+
fn foo(self: R) {}
7+
}
8+
9+
fn main() {
10+
let t = std::rc::Rc::new(Thing(std::marker::PhantomData));
11+
t.foo();
12+
//~^ ERROR its trait bounds were not satisfied
13+
let t = &t;
14+
// This is a further attempt at triggering 'type mismatch' errors
15+
// from arbitrary self types without resorting to the turbofish.
16+
// Ideally, here, t is Thing<Rc<Target=Self>> while we're going to call
17+
// it with a &t method receiver. However, this doesn't work since that
18+
// type of t becomes recursive and trait bounds can't be satisfied.
19+
t.foo();
20+
//~^ ERROR its trait bounds were not satisfied
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0599]: the method `foo` exists for struct `Rc<Thing<_>>`, but its trait bounds were not satisfied
2+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:11:7
3+
|
4+
LL | struct Thing<R>(std::marker::PhantomData<R>);
5+
| --------------- doesn't satisfy `Thing<_>: Deref`
6+
...
7+
LL | t.foo();
8+
| ^^^ method cannot be called on `Rc<Thing<_>>` due to unsatisfied trait bounds
9+
|
10+
note: the following trait bounds were not satisfied:
11+
`<&Rc<Thing<_>> as Deref>::Target = Thing<&Rc<Thing<_>>>`
12+
`<&Thing<_> as Deref>::Target = Thing<&Thing<_>>`
13+
`<&mut Rc<Thing<_>> as Deref>::Target = Thing<&mut Rc<Thing<_>>>`
14+
`<&mut Thing<_> as Deref>::Target = Thing<&mut Thing<_>>`
15+
`<Rc<Thing<_>> as Deref>::Target = Thing<Rc<Thing<_>>>`
16+
`Thing<_>: Deref`
17+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:5:9
18+
|
19+
LL | impl<R: std::ops::Deref<Target = Self>> Thing<R> {
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------
21+
| | |
22+
| | unsatisfied trait bound introduced here
23+
| unsatisfied trait bound introduced here
24+
note: the trait `Deref` must be implemented
25+
--> $SRC_DIR/core/src/ops/deref.rs:LL:COL
26+
27+
error[E0599]: the method `foo` exists for reference `&Rc<Thing<_>>`, but its trait bounds were not satisfied
28+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:19:7
29+
|
30+
LL | struct Thing<R>(std::marker::PhantomData<R>);
31+
| --------------- doesn't satisfy `Thing<_>: Deref`
32+
...
33+
LL | t.foo();
34+
| ^^^ method cannot be called on `&Rc<Thing<_>>` due to unsatisfied trait bounds
35+
|
36+
note: the following trait bounds were not satisfied:
37+
`<&&Rc<Thing<_>> as Deref>::Target = Thing<&&Rc<Thing<_>>>`
38+
`<&Rc<Thing<_>> as Deref>::Target = Thing<&Rc<Thing<_>>>`
39+
`<&Thing<_> as Deref>::Target = Thing<&Thing<_>>`
40+
`<&mut &Rc<Thing<_>> as Deref>::Target = Thing<&mut &Rc<Thing<_>>>`
41+
`<&mut Rc<Thing<_>> as Deref>::Target = Thing<&mut Rc<Thing<_>>>`
42+
`<&mut Thing<_> as Deref>::Target = Thing<&mut Thing<_>>`
43+
`<Rc<Thing<_>> as Deref>::Target = Thing<Rc<Thing<_>>>`
44+
`Thing<_>: Deref`
45+
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:5:9
46+
|
47+
LL | impl<R: std::ops::Deref<Target = Self>> Thing<R> {
48+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------
49+
| | |
50+
| | unsatisfied trait bound introduced here
51+
| unsatisfied trait bound introduced here
52+
note: the trait `Deref` must be implemented
53+
--> $SRC_DIR/core/src/ops/deref.rs:LL:COL
54+
55+
error: aborting due to 2 previous errors
56+
57+
For more information about this error, try `rustc --explain E0599`.
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-rc.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,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/arbitrary-self-from-method-substs-rc.rs:16:5
3+
|
4+
LL | foo.get::<std::rc::Rc<Foo>>();
5+
| ^^^ expected `Rc<Foo>`, found `Foo`
6+
|
7+
= note: expected struct `Rc<Foo>`
8+
found struct `Foo`
9+
10+
error: aborting due to 1 previous error
11+
12+
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::<std::rc::Rc<Foo>>();
17+
//[feature]~^ ERROR mismatched types
18+
}

0 commit comments

Comments
 (0)