-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only walk the identity substituted version of struct fields
- Loading branch information
Showing
5 changed files
with
126 additions
and
1 deletion.
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
28 changes: 28 additions & 0 deletions
28
tests/ui/type-alias-impl-trait/hidden_behind_projection_behind_struct_field.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 @@ | ||
//! This test shows that a field type that is a projection that resolves to an opaque, | ||
//! is not a defining use. While we could substitute the struct generics, that would | ||
//! mean we would have to walk all substitutions of an `Foo`, which can quickly | ||
//! degenerate into looking at an exponential number of types depending on the complexity | ||
//! of a program. | ||
#![feature(impl_trait_in_assoc_type)] | ||
|
||
struct Bar; | ||
|
||
trait Trait: Sized { | ||
type Assoc; | ||
fn foo() -> Foo<Self>; | ||
} | ||
|
||
impl Trait for Bar { | ||
type Assoc = impl std::fmt::Debug; | ||
fn foo() -> Foo<Bar> { | ||
Foo { field: () } | ||
//~^ ERROR: mismatched types | ||
} | ||
} | ||
|
||
struct Foo<T: Trait> { | ||
field: <T as Trait>::Assoc, | ||
} | ||
|
||
fn main() {} |
20 changes: 20 additions & 0 deletions
20
tests/ui/type-alias-impl-trait/hidden_behind_projection_behind_struct_field.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,20 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/hidden_behind_projection_behind_struct_field.rs:19:22 | ||
| | ||
LL | type Assoc = impl std::fmt::Debug; | ||
| -------------------- the expected opaque type | ||
LL | fn foo() -> Foo<Bar> { | ||
LL | Foo { field: () } | ||
| ^^ expected opaque type, found `()` | ||
| | ||
= note: expected opaque type `<Bar as Trait>::Assoc` | ||
found unit type `()` | ||
note: this item must have the opaque type in its signature in order to be able to register hidden types | ||
--> $DIR/hidden_behind_projection_behind_struct_field.rs:18:8 | ||
| | ||
LL | fn foo() -> Foo<Bar> { | ||
| ^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
30 changes: 30 additions & 0 deletions
30
tests/ui/type-alias-impl-trait/hidden_behind_struct_field.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,30 @@ | ||
//! This test shows that the appearance of an opaque type | ||
//! in the substs of a struct are enough to make it count | ||
//! for making the function a defining use. It doesn't matter | ||
//! if the opaque type is actually used in the field. | ||
#![feature(impl_trait_in_assoc_type)] | ||
// check-pass | ||
|
||
use std::marker::PhantomData; | ||
|
||
struct Bar; | ||
|
||
trait Trait: Sized { | ||
type Assoc; | ||
fn foo() -> Foo<Self::Assoc>; | ||
} | ||
|
||
impl Trait for Bar { | ||
type Assoc = impl std::fmt::Debug; | ||
fn foo() -> Foo<Self::Assoc> { | ||
let foo: Foo<()> = Foo { field: PhantomData }; | ||
foo | ||
} | ||
} | ||
|
||
struct Foo<T> { | ||
field: PhantomData<T>, | ||
} | ||
|
||
fn main() {} |
26 changes: 26 additions & 0 deletions
26
tests/ui/type-alias-impl-trait/hidden_behind_struct_field2.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,26 @@ | ||
//! This test shows that we can even follow projections | ||
//! into associated types of the same impl if they are | ||
//! indirectly mentioned in a struct field. | ||
#![feature(impl_trait_in_assoc_type)] | ||
// check-pass | ||
|
||
struct Bar; | ||
|
||
trait Trait: Sized { | ||
type Assoc; | ||
fn foo() -> Foo; | ||
} | ||
|
||
impl Trait for Bar { | ||
type Assoc = impl std::fmt::Debug; | ||
fn foo() -> Foo { | ||
Foo { field: () } | ||
} | ||
} | ||
|
||
struct Foo { | ||
field: <Bar as Trait>::Assoc, | ||
} | ||
|
||
fn main() {} |