-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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 #66178 - Aaron1011:fix/opaque-normalize, r=varkor
Fix opaque types resulting from projections in function signature When we normalize the types in a function signature, we may end up resolving a projection to an opaque type (e.g. `Self::MyType` when we have `type MyType = impl SomeTrait`). When the projection is resolved, we will instantiate the generic parameters into fresh inference variables. While we do want to normalize projections to opaque types, we don't want to replace the explicit generic parameters (e.g. `T` in `impl MyTrait<T>`) with inference variables. We want the opaque type in the function signature to be eligible to be a defining use of that opaque type - adding inference variables prevents this, since the opaque type substs now appears to refer to some specific type, rather than a generic type. To resolve this issue, we inspect the opaque types in the function signature after normalization. Any inference variables in the substs are replaced with the corresponding generic parameter in the identity substs (e.g. `T` in `impl MyTrait<T>`). Note that normalization is the only way that we can end up with inference variables in opaque substs in a function signature - users have no way of getting inference variables into a function signature. Note that all of this refers to the opaque type (ty::Opaque) and its subst - *not* to the underlying type. Fixes #59342
- Loading branch information
Showing
13 changed files
with
226 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Regression test for issue #59342 | ||
// Checks that we properly detect defining uses of opaque | ||
// types in 'item' position when generic parameters are involved | ||
// | ||
// run-pass | ||
#![feature(type_alias_impl_trait)] | ||
|
||
trait Meow { | ||
type MeowType; | ||
fn meow(self) -> Self::MeowType; | ||
} | ||
|
||
impl<T, I> Meow for I | ||
where I: Iterator<Item = T> | ||
{ | ||
type MeowType = impl Iterator<Item = T>; | ||
fn meow(self) -> Self::MeowType { | ||
self | ||
} | ||
} | ||
|
||
fn main() {} |
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,36 @@ | ||
// Tests that we properly detect defining usages when using | ||
// const generics in an associated opaque type | ||
// check-pass | ||
|
||
#![feature(type_alias_impl_trait)] | ||
#![feature(const_generics)] | ||
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash | ||
|
||
trait UnwrapItemsExt<const C: usize> { | ||
type Iter; | ||
fn unwrap_items(self) -> Self::Iter; | ||
} | ||
|
||
struct MyStruct<const C: usize> {} | ||
|
||
trait MyTrait<'a, const C: usize> { | ||
type MyItem; | ||
const MY_CONST: usize; | ||
} | ||
|
||
impl<'a, const C: usize> MyTrait<'a, {C}> for MyStruct<{C}> { | ||
type MyItem = u8; | ||
const MY_CONST: usize = C; | ||
} | ||
|
||
impl<'a, I, const C: usize> UnwrapItemsExt<{C}> for I | ||
where | ||
{ | ||
type Iter = impl MyTrait<'a, {C}>; | ||
|
||
fn unwrap_items(self) -> Self::Iter { | ||
MyStruct::<{C}> {} | ||
} | ||
} | ||
|
||
fn main() {} |
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,8 @@ | ||
warning: the feature `const_generics` is incomplete and may cause the compiler to crash | ||
--> $DIR/assoc-type-const.rs:6:12 | ||
| | ||
LL | #![feature(const_generics)] | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(incomplete_features)]` on by default | ||
|
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 @@ | ||
// Tests that we still detect defining usages when | ||
// lifetimes are used in an associated opaque type | ||
// check-pass | ||
|
||
#![feature(type_alias_impl_trait)] | ||
|
||
trait UnwrapItemsExt { | ||
type Iter; | ||
fn unwrap_items(self) -> Self::Iter; | ||
} | ||
|
||
struct MyStruct {} | ||
|
||
trait MyTrait<'a> {} | ||
|
||
impl<'a> MyTrait<'a> for MyStruct {} | ||
|
||
impl<'a, I> UnwrapItemsExt for I | ||
where | ||
{ | ||
type Iter = impl MyTrait<'a>; | ||
|
||
fn unwrap_items(self) -> Self::Iter { | ||
MyStruct {} | ||
} | ||
} | ||
|
||
fn main() {} |
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 was deleted.
Oops, something went wrong.
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