mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 09:23:05 +00:00
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
This commit is contained in:
parent
5a1d028d4c
commit
4b57d2228b
@ -114,7 +114,7 @@ use rustc::ty::{
|
||||
use rustc::ty::adjustment::{
|
||||
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast
|
||||
};
|
||||
use rustc::ty::fold::TypeFoldable;
|
||||
use rustc::ty::fold::{TypeFoldable, TypeFolder};
|
||||
use rustc::ty::query::Providers;
|
||||
use rustc::ty::subst::{
|
||||
GenericArgKind, Subst, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts,
|
||||
@ -872,6 +872,58 @@ fn used_trait_imports(tcx: TyCtxt<'_>, def_id: DefId) -> &DefIdSet {
|
||||
&*tcx.typeck_tables_of(def_id).used_trait_imports
|
||||
}
|
||||
|
||||
/// Inspects the substs of opaque types, replacing any inference variables
|
||||
/// with proper generic parameter from the identity substs.
|
||||
///
|
||||
/// This is run after we normalize the function signature, to fix any inference
|
||||
/// variables introduced by the projection of associated types. This ensures that
|
||||
/// any opaque types used in the signature continue to refer to generic parameters,
|
||||
/// allowing them to be considered for defining uses in the function body
|
||||
fn fixup_opaque_types<'tcx, T>(tcx: TyCtxt<'tcx>, val: &T) -> T where T: TypeFoldable<'tcx> {
|
||||
struct FixupFolder<'tcx> {
|
||||
tcx: TyCtxt<'tcx>
|
||||
}
|
||||
|
||||
impl<'tcx> TypeFolder<'tcx> for FixupFolder<'tcx> {
|
||||
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
|
||||
self.tcx
|
||||
}
|
||||
|
||||
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
||||
match ty.kind {
|
||||
ty::Opaque(def_id, substs) => {
|
||||
debug!("fixup_opaque_types: found type {:?}", ty);
|
||||
if ty.has_infer_types() {
|
||||
let new_substs = InternalSubsts::for_item(self.tcx, def_id, |param, _| {
|
||||
let old_param = substs[param.index as usize];
|
||||
match old_param.unpack() {
|
||||
GenericArgKind::Type(old_ty) => {
|
||||
if let ty::Infer(_) = old_ty.kind {
|
||||
// Replace inference type with a generic parameter
|
||||
self.tcx.mk_param_from_def(param)
|
||||
} else {
|
||||
old_param.fold_with(self)
|
||||
}
|
||||
},
|
||||
_ => old_param
|
||||
}
|
||||
});
|
||||
let new_ty = self.tcx.mk_opaque(def_id, new_substs);
|
||||
debug!("fixup_opaque_types: new type: {:?}", new_ty);
|
||||
new_ty
|
||||
} else {
|
||||
ty
|
||||
}
|
||||
},
|
||||
_ => ty.super_fold_with(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("fixup_opaque_types({:?})", val);
|
||||
val.fold_with(&mut FixupFolder { tcx })
|
||||
}
|
||||
|
||||
fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
|
||||
// Closures' tables come from their outermost function,
|
||||
// as they are part of the same "inference environment".
|
||||
@ -911,6 +963,8 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
|
||||
param_env,
|
||||
&fn_sig);
|
||||
|
||||
let fn_sig = fixup_opaque_types(tcx, &fn_sig);
|
||||
|
||||
let fcx = check_fn(&inh, param_env, fn_sig, decl, id, body, None).0;
|
||||
fcx
|
||||
} else {
|
||||
|
@ -2059,6 +2059,9 @@ fn explicit_predicates_of(
|
||||
ty::print::with_no_queries(|| {
|
||||
let substs = InternalSubsts::identity_for_item(tcx, def_id);
|
||||
let opaque_ty = tcx.mk_opaque(def_id, substs);
|
||||
debug!("explicit_predicates_of({:?}): created opaque type {:?}",
|
||||
def_id, opaque_ty);
|
||||
|
||||
|
||||
// Collect the bounds, i.e., the `A + B + 'c` in `impl A + B + 'c`.
|
||||
let bounds = AstConv::compute_bounds(
|
||||
|
22
src/test/ui/impl-trait/type-alias-generic-param.rs
Normal file
22
src/test/ui/impl-trait/type-alias-generic-param.rs
Normal file
@ -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() {}
|
Loading…
Reference in New Issue
Block a user