forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#86264 - crlf0710:trait_upcasting_part1, r=nik…
…omatsakis Trait upcasting coercion (part1) This revives the first part of earlier PR rust-lang#60900 . It's not very clear to me which parts of that pr was design decisions, so i decide to cut it into pieces and land them incrementally. This allows more eyes on the details. This is the first part, it adds feature gates, adds feature gates tests, and implemented the unsize conversion part. (I hope i have dealt with the `ExistentialTraitRef` values correctly...) The next part will be implementing the pointer casting.
- Loading branch information
Showing
20 changed files
with
504 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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
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
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,13 @@ | ||
trait Foo {} | ||
|
||
trait Bar: Foo {} | ||
|
||
impl Foo for () {} | ||
|
||
impl Bar for () {} | ||
|
||
fn main() { | ||
let bar: &dyn Bar = &(); | ||
let foo: &dyn Foo = bar; | ||
//~^ ERROR trait upcasting coercion is experimental [E0658] | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/feature-gates/feature-gate-trait_upcasting.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[E0658]: trait upcasting coercion is experimental | ||
--> $DIR/feature-gate-trait_upcasting.rs:11:25 | ||
| | ||
LL | let foo: &dyn Foo = bar; | ||
| ^^^ | ||
| | ||
= note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information | ||
= help: add `#![feature(trait_upcasting)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#![feature(box_syntax)] | ||
|
||
struct Test { | ||
func: Box<dyn FnMut() + 'static> | ||
func: Box<dyn FnMut() + 'static>, | ||
} | ||
|
||
fn main() { | ||
let closure: Box<dyn Fn() + 'static> = Box::new(|| ()); | ||
let test = box Test { func: closure }; //~ ERROR mismatched types | ||
let test = box Test { func: closure }; //~ ERROR trait upcasting coercion is experimental [E0658] | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
error[E0308]: mismatched types | ||
error[E0658]: trait upcasting coercion is experimental | ||
--> $DIR/issue-11515.rs:9:33 | ||
| | ||
LL | let test = box Test { func: closure }; | ||
| ^^^^^^^ expected trait `FnMut`, found trait `Fn` | ||
| ^^^^^^^ | ||
| | ||
= note: expected struct `Box<(dyn FnMut() + 'static)>` | ||
found struct `Box<(dyn Fn() + 'static)>` | ||
= note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information | ||
= help: add `#![feature(trait_upcasting)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. | ||
For more information about this error, try `rustc --explain E0658`. |
13 changes: 13 additions & 0 deletions
13
src/test/ui/traits/trait-upcasting/issue-11515-upcast-fn_mut-fn.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,13 @@ | ||
// run-pass | ||
#![feature(box_syntax, trait_upcasting)] | ||
#![allow(incomplete_features)] | ||
|
||
struct Test { | ||
func: Box<dyn FnMut() + 'static>, | ||
} | ||
|
||
fn main() { | ||
let closure: Box<dyn Fn() + 'static> = Box::new(|| ()); | ||
let mut test = box Test { func: closure }; | ||
(test.func)(); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.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,22 @@ | ||
// check-fail | ||
#![feature(trait_upcasting)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Bar<T> { | ||
fn bar(&self, _: T) {} | ||
} | ||
|
||
trait Foo : Bar<i32> + Bar<u32> { | ||
fn foo(&self, _: ()) {} | ||
} | ||
|
||
struct S; | ||
|
||
impl Bar<i32> for S {} | ||
impl Bar<u32> for S {} | ||
impl Foo for S {} | ||
|
||
fn main() { | ||
let s: &dyn Foo = &S; | ||
let t: &dyn Bar<_> = s; //~ ERROR mismatched types | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.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[E0308]: mismatched types | ||
--> $DIR/multiple-occurence-ambiguousity.rs:21:26 | ||
| | ||
LL | let t: &dyn Bar<_> = s; | ||
| ----------- ^ expected trait `Bar`, found trait `Foo` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected reference `&dyn Bar<_>` | ||
found reference `&dyn Foo` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
33 changes: 33 additions & 0 deletions
33
src/test/ui/traits/trait-upcasting/type-checking-test-1.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,33 @@ | ||
#![feature(trait_upcasting)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Foo: Bar<i32> + Bar<u32> {} | ||
trait Bar<T> { | ||
fn bar(&self) -> Option<T> { | ||
None | ||
} | ||
} | ||
|
||
fn test_specific(x: &dyn Foo) { | ||
let _ = x as &dyn Bar<i32>; // FIXME: OK, eventually | ||
//~^ ERROR non-primitive cast | ||
//~^^ ERROR the trait bound `&dyn Foo: Bar<i32>` is not satisfied | ||
let _ = x as &dyn Bar<u32>; // FIXME: OK, eventually | ||
//~^ ERROR non-primitive cast | ||
//~^^ ERROR the trait bound `&dyn Foo: Bar<u32>` is not satisfied | ||
} | ||
|
||
fn test_unknown_version(x: &dyn Foo) { | ||
let _ = x as &dyn Bar<_>; // Ambiguous | ||
//~^ ERROR non-primitive cast | ||
//~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied | ||
} | ||
|
||
fn test_infer_version(x: &dyn Foo) { | ||
let a = x as &dyn Bar<_>; // FIXME: OK, eventually | ||
//~^ ERROR non-primitive cast | ||
//~^^ ERROR the trait bound `&dyn Foo: Bar<u32>` is not satisfied | ||
let _: Option<u32> = a.bar(); | ||
} | ||
|
||
fn main() {} |
80 changes: 80 additions & 0 deletions
80
src/test/ui/traits/trait-upcasting/type-checking-test-1.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,80 @@ | ||
error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<i32>` | ||
--> $DIR/type-checking-test-1.rs:12:13 | ||
| | ||
LL | let _ = x as &dyn Bar<i32>; // FIXME: OK, eventually | ||
| ^^^^^^^^^^^^^^^^^^ invalid cast | ||
| | ||
help: consider borrowing the value | ||
| | ||
LL | let _ = &x as &dyn Bar<i32>; // FIXME: OK, eventually | ||
| ^ | ||
|
||
error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<u32>` | ||
--> $DIR/type-checking-test-1.rs:15:13 | ||
| | ||
LL | let _ = x as &dyn Bar<u32>; // FIXME: OK, eventually | ||
| ^^^^^^^^^^^^^^^^^^ invalid cast | ||
| | ||
help: consider borrowing the value | ||
| | ||
LL | let _ = &x as &dyn Bar<u32>; // FIXME: OK, eventually | ||
| ^ | ||
|
||
error[E0277]: the trait bound `&dyn Foo: Bar<i32>` is not satisfied | ||
--> $DIR/type-checking-test-1.rs:12:13 | ||
| | ||
LL | let _ = x as &dyn Bar<i32>; // FIXME: OK, eventually | ||
| ^ the trait `Bar<i32>` is not implemented for `&dyn Foo` | ||
| | ||
= note: required for the cast to the object type `dyn Bar<i32>` | ||
|
||
error[E0277]: the trait bound `&dyn Foo: Bar<u32>` is not satisfied | ||
--> $DIR/type-checking-test-1.rs:15:13 | ||
| | ||
LL | let _ = x as &dyn Bar<u32>; // FIXME: OK, eventually | ||
| ^ the trait `Bar<u32>` is not implemented for `&dyn Foo` | ||
| | ||
= note: required for the cast to the object type `dyn Bar<u32>` | ||
|
||
error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>` | ||
--> $DIR/type-checking-test-1.rs:21:13 | ||
| | ||
LL | let _ = x as &dyn Bar<_>; // Ambiguous | ||
| ^^^^^^^^^^^^^^^^ invalid cast | ||
| | ||
help: consider borrowing the value | ||
| | ||
LL | let _ = &x as &dyn Bar<_>; // Ambiguous | ||
| ^ | ||
|
||
error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied | ||
--> $DIR/type-checking-test-1.rs:21:13 | ||
| | ||
LL | let _ = x as &dyn Bar<_>; // Ambiguous | ||
| ^ the trait `Bar<_>` is not implemented for `&dyn Foo` | ||
| | ||
= note: required for the cast to the object type `dyn Bar<_>` | ||
|
||
error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<u32>` | ||
--> $DIR/type-checking-test-1.rs:27:13 | ||
| | ||
LL | let a = x as &dyn Bar<_>; // FIXME: OK, eventually | ||
| ^^^^^^^^^^^^^^^^ invalid cast | ||
| | ||
help: consider borrowing the value | ||
| | ||
LL | let a = &x as &dyn Bar<_>; // FIXME: OK, eventually | ||
| ^ | ||
|
||
error[E0277]: the trait bound `&dyn Foo: Bar<u32>` is not satisfied | ||
--> $DIR/type-checking-test-1.rs:27:13 | ||
| | ||
LL | let a = x as &dyn Bar<_>; // FIXME: OK, eventually | ||
| ^ the trait `Bar<u32>` is not implemented for `&dyn Foo` | ||
| | ||
= note: required for the cast to the object type `dyn Bar<u32>` | ||
|
||
error: aborting due to 8 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0605. | ||
For more information about an error, try `rustc --explain E0277`. |
Oops, something went wrong.