-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
10 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
tests/ui/self/arbitrary-self-from-method-substs-mismatches.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/ui/self/arbitrary-self-from-method-substs-mismatches.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/arbitrary-self-from-method-substs-mismatches.rs:34:5 | ||
| | ||
LL | smart_ptr.a::<SmartPtr2<Foo>>(); | ||
| ^^^^^^^^^ expected `SmartPtr2<'_, Foo>`, found `SmartPtr<'_, Foo>` | ||
| | ||
= note: expected struct `SmartPtr2<'_, Foo>` | ||
found struct `SmartPtr<'_, Foo>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/arbitrary-self-from-method-substs-mismatches.rs:36:5 | ||
| | ||
LL | smart_ptr.a::<&Foo>(); | ||
| ^^^^^^^^^ expected `&Foo`, found `SmartPtr<'_, Foo>` | ||
| | ||
= note: expected reference `&Foo` | ||
found struct `SmartPtr<'_, Foo, >` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
43 changes: 43 additions & 0 deletions
43
tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.default.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:28:9 | ||
| | ||
LL | foo.get(Silly); | ||
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo` | ||
| | ||
note: expected this to be `Rc<Foo>` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21 | ||
| | ||
LL | type Receiver = std::rc::Rc<Foo>; | ||
| ^^^^^^^^^^^^^^^^ | ||
= note: expected struct `Rc<Foo>` | ||
found struct `Foo` | ||
|
||
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:32:9 | ||
| | ||
LL | foo.get(Silly); | ||
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo` | ||
| | ||
note: expected this to be `Rc<Foo>` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21 | ||
| | ||
LL | type Receiver = std::rc::Rc<Foo>; | ||
| ^^^^^^^^^^^^^^^^ | ||
= note: expected struct `Rc<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`. |
31 changes: 31 additions & 0 deletions
31
tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.feature.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:28:9 | ||
| | ||
LL | foo.get(Silly); | ||
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == Foo` | ||
| | ||
note: expected this to be `Rc<Foo>` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21 | ||
| | ||
LL | type Receiver = std::rc::Rc<Foo>; | ||
| ^^^^^^^^^^^^^^^^ | ||
= note: expected struct `Rc<Foo>` | ||
found struct `Foo` | ||
|
||
error[E0271]: type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:32:9 | ||
| | ||
LL | foo.get(Silly); | ||
| ^^^ type mismatch resolving `<Silly as FindReceiver>::Receiver == &Foo` | ||
| | ||
note: expected this to be `Rc<Foo>` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish.rs:21:21 | ||
| | ||
LL | type Receiver = std::rc::Rc<Foo>; | ||
| ^^^^^^^^^^^^^^^^ | ||
= note: expected struct `Rc<Foo>` | ||
found reference `&Foo` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0271`. |
34 changes: 34 additions & 0 deletions
34
tests/ui/self/arbitrary-self-from-method-substs-no-turbofish.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//@ 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 Silly; | ||
impl FindReceiver for Silly { | ||
type Receiver = std::rc::Rc<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 | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/ui/self/arbitrary-self-from-method-substs-no-turbofish2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![feature(arbitrary_self_types)] | ||
|
||
struct Thing<R>(std::marker::PhantomData<R>); | ||
|
||
impl<R: std::ops::Deref<Target = Self>> Thing<R> { | ||
fn foo(self: R) {} | ||
} | ||
|
||
fn main() { | ||
let t = std::rc::Rc::new(Thing(std::marker::PhantomData)); | ||
t.foo(); | ||
//~^ ERROR its trait bounds were not satisfied | ||
let t = &t; | ||
// This is a further attempt at triggering 'type mismatch' errors | ||
// from arbitrary self types without resorting to the turbofish. | ||
// Ideally, here, t is Thing<Rc<Target=Self>> while we're going to call | ||
// it with a &t method receiver. However, this doesn't work since that | ||
// type of t becomes recursive and trait bounds can't be satisfied. | ||
t.foo(); | ||
//~^ ERROR its trait bounds were not satisfied | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/ui/self/arbitrary-self-from-method-substs-no-turbofish2.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
error[E0599]: the method `foo` exists for struct `Rc<Thing<_>>`, but its trait bounds were not satisfied | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:11:7 | ||
| | ||
LL | struct Thing<R>(std::marker::PhantomData<R>); | ||
| --------------- doesn't satisfy `Thing<_>: Deref` | ||
... | ||
LL | t.foo(); | ||
| ^^^ method cannot be called on `Rc<Thing<_>>` due to unsatisfied trait bounds | ||
| | ||
note: the following trait bounds were not satisfied: | ||
`<&Rc<Thing<_>> as Deref>::Target = Thing<&Rc<Thing<_>>>` | ||
`<&Thing<_> as Deref>::Target = Thing<&Thing<_>>` | ||
`<&mut Rc<Thing<_>> as Deref>::Target = Thing<&mut Rc<Thing<_>>>` | ||
`<&mut Thing<_> as Deref>::Target = Thing<&mut Thing<_>>` | ||
`<Rc<Thing<_>> as Deref>::Target = Thing<Rc<Thing<_>>>` | ||
`Thing<_>: Deref` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:5:9 | ||
| | ||
LL | impl<R: std::ops::Deref<Target = Self>> Thing<R> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -------- | ||
| | | | ||
| | unsatisfied trait bound introduced here | ||
| unsatisfied trait bound introduced here | ||
note: the trait `Deref` must be implemented | ||
--> $SRC_DIR/core/src/ops/deref.rs:LL:COL | ||
|
||
error[E0599]: the method `foo` exists for reference `&Rc<Thing<_>>`, but its trait bounds were not satisfied | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:19:7 | ||
| | ||
LL | struct Thing<R>(std::marker::PhantomData<R>); | ||
| --------------- doesn't satisfy `Thing<_>: Deref` | ||
... | ||
LL | t.foo(); | ||
| ^^^ method cannot be called on `&Rc<Thing<_>>` due to unsatisfied trait bounds | ||
| | ||
note: the following trait bounds were not satisfied: | ||
`<&&Rc<Thing<_>> as Deref>::Target = Thing<&&Rc<Thing<_>>>` | ||
`<&Rc<Thing<_>> as Deref>::Target = Thing<&Rc<Thing<_>>>` | ||
`<&Thing<_> as Deref>::Target = Thing<&Thing<_>>` | ||
`<&mut &Rc<Thing<_>> as Deref>::Target = Thing<&mut &Rc<Thing<_>>>` | ||
`<&mut Rc<Thing<_>> as Deref>::Target = Thing<&mut Rc<Thing<_>>>` | ||
`<&mut Thing<_> as Deref>::Target = Thing<&mut Thing<_>>` | ||
`<Rc<Thing<_>> as Deref>::Target = Thing<Rc<Thing<_>>>` | ||
`Thing<_>: Deref` | ||
--> $DIR/arbitrary-self-from-method-substs-no-turbofish2.rs:5:9 | ||
| | ||
LL | impl<R: std::ops::Deref<Target = Self>> Thing<R> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -------- | ||
| | | | ||
| | unsatisfied trait bound introduced here | ||
| unsatisfied trait bound introduced here | ||
note: the trait `Deref` must be implemented | ||
--> $SRC_DIR/core/src/ops/deref.rs:LL:COL | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
14 changes: 14 additions & 0 deletions
14
tests/ui/self/arbitrary-self-from-method-substs-rc.default.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rc.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`. |
12 changes: 12 additions & 0 deletions
12
tests/ui/self/arbitrary-self-from-method-substs-rc.feature.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/arbitrary-self-from-method-substs-rc.rs:16:5 | ||
| | ||
LL | foo.get::<std::rc::Rc<Foo>>(); | ||
| ^^^ expected `Rc<Foo>`, found `Foo` | ||
| | ||
= note: expected struct `Rc<Foo>` | ||
found struct `Foo` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::<std::rc::Rc<Foo>>(); | ||
//[feature]~^ ERROR mismatched types | ||
} |