Skip to content

Commit 1fd23f5

Browse files
committed
improve tests as suggested by review comments
1 parent baf9f01 commit 1fd23f5

4 files changed

+92
-2
lines changed

src/test/ui/traits/trait-object-with-self-in-projection-output-bad.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Regression test for #56288. Checks that if a supertrait defines an associated type
2+
// projection that references `Self`, then that associated type must still be explicitly
3+
// specified in the `dyn Trait` variant, since we don't know what `Self` is anymore.
4+
15
trait Base {
26
type Output;
37
}
@@ -16,7 +20,31 @@ impl Helper for u32
1620
type Target = i32;
1721
}
1822

23+
trait ConstI32 {
24+
type Out;
25+
}
26+
27+
impl<T: ?Sized> ConstI32 for T {
28+
type Out = i32;
29+
}
30+
31+
// Test that you still need to manually give a projection type if the Output type
32+
// is normalizable.
33+
trait NormalizableHelper:
34+
Base<Output=<Self as ConstI32>::Out>
35+
{
36+
type Target;
37+
}
38+
39+
impl NormalizableHelper for u32
40+
{
41+
type Target = i32;
42+
}
43+
1944
fn main() {
2045
let _x: Box<dyn Helper<Target=i32>> = Box::new(2u32);
2146
//~^ ERROR the value of the associated type `Output` (from the trait `Base`) must be specified
47+
48+
let _y: Box<dyn NormalizableHelper<Target=i32>> = Box::new(2u32);
49+
//~^ ERROR the value of the associated type `Output` (from the trait `Base`) must be specified
2250
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
error[E0191]: the value of the associated type `Output` (from the trait `Base`) must be specified
2-
--> $DIR/trait-object-with-self-in-projection-output-bad.rs:20:17
2+
--> $DIR/trait-object-with-self-in-projection-output-bad.rs:45:17
33
|
44
LL | type Output;
55
| ------------ `Output` defined here
66
...
77
LL | let _x: Box<dyn Helper<Target=i32>> = Box::new(2u32);
88
| ^^^^^^^^^^^^^^^^^^^^^^ associated type `Output` must be specified
99

10-
error: aborting due to previous error
10+
error[E0191]: the value of the associated type `Output` (from the trait `Base`) must be specified
11+
--> $DIR/trait-object-with-self-in-projection-output-bad.rs:48:17
12+
|
13+
LL | type Output;
14+
| ------------ `Output` defined here
15+
...
16+
LL | let _y: Box<dyn NormalizableHelper<Target=i32>> = Box::new(2u32);
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `Output` must be specified
18+
19+
error: aborting due to 2 previous errors
1120

1221
For more information about this error, try `rustc --explain E0191`.

src/test/ui/traits/trait-object-with-self-in-projection-output-good.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// compile-pass
22

3+
// Regression test related to #56288. Check that a supertrait projection (of
4+
// `Output`) that references `Self` can be ok if it is referencing a projection (of
5+
// `Self::Target`, in this case). Note that we still require the user to manually
6+
// specify both `Target` and `Output` for now.
7+
38
trait Base {
49
type Output;
510
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// compile-pass
2+
3+
// Regression test related to #56288. Check that a supertrait projection (of
4+
// `Output`) that references `Self` is ok if there is another occurence of
5+
// the same supertrait that specifies the projection explicitly, even if
6+
// the projection's associated type is not explicitly specified in the object type.
7+
//
8+
// Note that in order for this to compile, we need the `Self`-referencing projection
9+
// to normalize fairly directly to a concrete type, otherwise the trait resolver
10+
// will hate us.
11+
//
12+
// There is a test in `trait-object-with-self-in-projection-output-bad.rs` that
13+
// having a normalizing, but `Self`-containing projection does not *by itself*
14+
// allow you to avoid writing the projected type (`Output`, in this example)
15+
// explicitly.
16+
17+
trait ConstI32 {
18+
type Out;
19+
}
20+
21+
impl<T: ?Sized> ConstI32 for T {
22+
type Out = i32;
23+
}
24+
25+
trait Base {
26+
type Output;
27+
}
28+
29+
trait NormalizingHelper: Base<Output=<Self as ConstI32>::Out> + Base<Output=i32> {
30+
type Target;
31+
}
32+
33+
impl Base for u32
34+
{
35+
type Output = i32;
36+
}
37+
38+
impl NormalizingHelper for u32
39+
{
40+
type Target = i32;
41+
}
42+
43+
fn main() {
44+
// Make sure this works both with and without the associated type
45+
// being specified.
46+
let _x: Box<dyn NormalizingHelper<Target=i32>> = Box::new(2u32);
47+
let _y: Box<dyn NormalizingHelper<Target=i32, Output=i32>> = Box::new(2u32);
48+
}

0 commit comments

Comments
 (0)