-
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.
Auto merge of #56863 - arielb1:supertrait-self-4, r=nikomatsakis
fix trait objects with a Self-containing projection values Fixes #56288. This follows ALT2 in the issue. beta-nominating since this is a regression. r? @nikomatsakis
- Loading branch information
Showing
6 changed files
with
193 additions
and
3 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
50 changes: 50 additions & 0 deletions
50
src/test/ui/traits/trait-object-with-self-in-projection-output-bad.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,50 @@ | ||
// Regression test for #56288. Checks that if a supertrait defines an associated type | ||
// projection that references `Self`, then that associated type must still be explicitly | ||
// specified in the `dyn Trait` variant, since we don't know what `Self` is anymore. | ||
|
||
trait Base { | ||
type Output; | ||
} | ||
|
||
trait Helper: Base<Output=<Self as Helper>::Target> { | ||
type Target; | ||
} | ||
|
||
impl Base for u32 | ||
{ | ||
type Output = i32; | ||
} | ||
|
||
impl Helper for u32 | ||
{ | ||
type Target = i32; | ||
} | ||
|
||
trait ConstI32 { | ||
type Out; | ||
} | ||
|
||
impl<T: ?Sized> ConstI32 for T { | ||
type Out = i32; | ||
} | ||
|
||
// Test that you still need to manually give a projection type if the Output type | ||
// is normalizable. | ||
trait NormalizableHelper: | ||
Base<Output=<Self as ConstI32>::Out> | ||
{ | ||
type Target; | ||
} | ||
|
||
impl NormalizableHelper for u32 | ||
{ | ||
type Target = i32; | ||
} | ||
|
||
fn main() { | ||
let _x: Box<dyn Helper<Target=i32>> = Box::new(2u32); | ||
//~^ ERROR the value of the associated type `Output` (from the trait `Base`) must be specified | ||
|
||
let _y: Box<dyn NormalizableHelper<Target=i32>> = Box::new(2u32); | ||
//~^ ERROR the value of the associated type `Output` (from the trait `Base`) must be specified | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/traits/trait-object-with-self-in-projection-output-bad.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[E0191]: the value of the associated type `Output` (from the trait `Base`) must be specified | ||
--> $DIR/trait-object-with-self-in-projection-output-bad.rs:45:17 | ||
| | ||
LL | type Output; | ||
| ------------ `Output` defined here | ||
... | ||
LL | let _x: Box<dyn Helper<Target=i32>> = Box::new(2u32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ associated type `Output` must be specified | ||
|
||
error[E0191]: the value of the associated type `Output` (from the trait `Base`) must be specified | ||
--> $DIR/trait-object-with-self-in-projection-output-bad.rs:48:17 | ||
| | ||
LL | type Output; | ||
| ------------ `Output` defined here | ||
... | ||
LL | let _y: Box<dyn NormalizableHelper<Target=i32>> = Box::new(2u32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `Output` must be specified | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0191`. |
28 changes: 28 additions & 0 deletions
28
src/test/ui/traits/trait-object-with-self-in-projection-output-good.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,28 @@ | ||
// compile-pass | ||
|
||
// Regression test related to #56288. Check that a supertrait projection (of | ||
// `Output`) that references `Self` can be ok if it is referencing a projection (of | ||
// `Self::Target`, in this case). Note that we still require the user to manually | ||
// specify both `Target` and `Output` for now. | ||
|
||
trait Base { | ||
type Output; | ||
} | ||
|
||
trait Helper: Base<Output=<Self as Helper>::Target> { | ||
type Target; | ||
} | ||
|
||
impl Base for u32 | ||
{ | ||
type Output = i32; | ||
} | ||
|
||
impl Helper for u32 | ||
{ | ||
type Target = i32; | ||
} | ||
|
||
fn main() { | ||
let _x: Box<dyn Helper<Target=i32, Output=i32>> = Box::new(2u32); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/ui/traits/trait-object-with-self-in-projection-output-repeated-supertrait.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,48 @@ | ||
// compile-pass | ||
|
||
// Regression test related to #56288. Check that a supertrait projection (of | ||
// `Output`) that references `Self` is ok if there is another occurence of | ||
// the same supertrait that specifies the projection explicitly, even if | ||
// the projection's associated type is not explicitly specified in the object type. | ||
// | ||
// Note that in order for this to compile, we need the `Self`-referencing projection | ||
// to normalize fairly directly to a concrete type, otherwise the trait resolver | ||
// will hate us. | ||
// | ||
// There is a test in `trait-object-with-self-in-projection-output-bad.rs` that | ||
// having a normalizing, but `Self`-containing projection does not *by itself* | ||
// allow you to avoid writing the projected type (`Output`, in this example) | ||
// explicitly. | ||
|
||
trait ConstI32 { | ||
type Out; | ||
} | ||
|
||
impl<T: ?Sized> ConstI32 for T { | ||
type Out = i32; | ||
} | ||
|
||
trait Base { | ||
type Output; | ||
} | ||
|
||
trait NormalizingHelper: Base<Output=<Self as ConstI32>::Out> + Base<Output=i32> { | ||
type Target; | ||
} | ||
|
||
impl Base for u32 | ||
{ | ||
type Output = i32; | ||
} | ||
|
||
impl NormalizingHelper for u32 | ||
{ | ||
type Target = i32; | ||
} | ||
|
||
fn main() { | ||
// Make sure this works both with and without the associated type | ||
// being specified. | ||
let _x: Box<dyn NormalizingHelper<Target=i32>> = Box::new(2u32); | ||
let _y: Box<dyn NormalizingHelper<Target=i32, Output=i32>> = Box::new(2u32); | ||
} |