Skip to content

Commit 3c0de63

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. Improved diagnostics were suggested in commit 05c5caa This is a part of the arbitrary self types v2 project, rust-lang/rfcs#3519 and specifically the sub-issue exploring questions around generics, rust-lang#129147
1 parent ae9f501 commit 3c0de63

4 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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: aborting due to 2 previous errors
27+
28+
Some errors have detailed explanations: E0271, E0658.
29+
For more information about an error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
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)