Skip to content

Commit 4b57d22

Browse files
committed
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
1 parent 5a1d028 commit 4b57d22

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/librustc_typeck/check/mod.rs

+55-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ use rustc::ty::{
114114
use rustc::ty::adjustment::{
115115
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast
116116
};
117-
use rustc::ty::fold::TypeFoldable;
117+
use rustc::ty::fold::{TypeFoldable, TypeFolder};
118118
use rustc::ty::query::Providers;
119119
use rustc::ty::subst::{
120120
GenericArgKind, Subst, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts,
@@ -872,6 +872,58 @@ fn used_trait_imports(tcx: TyCtxt<'_>, def_id: DefId) -> &DefIdSet {
872872
&*tcx.typeck_tables_of(def_id).used_trait_imports
873873
}
874874

875+
/// Inspects the substs of opaque types, replacing any inference variables
876+
/// with proper generic parameter from the identity substs.
877+
///
878+
/// This is run after we normalize the function signature, to fix any inference
879+
/// variables introduced by the projection of associated types. This ensures that
880+
/// any opaque types used in the signature continue to refer to generic parameters,
881+
/// allowing them to be considered for defining uses in the function body
882+
fn fixup_opaque_types<'tcx, T>(tcx: TyCtxt<'tcx>, val: &T) -> T where T: TypeFoldable<'tcx> {
883+
struct FixupFolder<'tcx> {
884+
tcx: TyCtxt<'tcx>
885+
}
886+
887+
impl<'tcx> TypeFolder<'tcx> for FixupFolder<'tcx> {
888+
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
889+
self.tcx
890+
}
891+
892+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
893+
match ty.kind {
894+
ty::Opaque(def_id, substs) => {
895+
debug!("fixup_opaque_types: found type {:?}", ty);
896+
if ty.has_infer_types() {
897+
let new_substs = InternalSubsts::for_item(self.tcx, def_id, |param, _| {
898+
let old_param = substs[param.index as usize];
899+
match old_param.unpack() {
900+
GenericArgKind::Type(old_ty) => {
901+
if let ty::Infer(_) = old_ty.kind {
902+
// Replace inference type with a generic parameter
903+
self.tcx.mk_param_from_def(param)
904+
} else {
905+
old_param.fold_with(self)
906+
}
907+
},
908+
_ => old_param
909+
}
910+
});
911+
let new_ty = self.tcx.mk_opaque(def_id, new_substs);
912+
debug!("fixup_opaque_types: new type: {:?}", new_ty);
913+
new_ty
914+
} else {
915+
ty
916+
}
917+
},
918+
_ => ty.super_fold_with(self)
919+
}
920+
}
921+
}
922+
923+
debug!("fixup_opaque_types({:?})", val);
924+
val.fold_with(&mut FixupFolder { tcx })
925+
}
926+
875927
fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
876928
// Closures' tables come from their outermost function,
877929
// as they are part of the same "inference environment".
@@ -911,6 +963,8 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
911963
param_env,
912964
&fn_sig);
913965

966+
let fn_sig = fixup_opaque_types(tcx, &fn_sig);
967+
914968
let fcx = check_fn(&inh, param_env, fn_sig, decl, id, body, None).0;
915969
fcx
916970
} else {

src/librustc_typeck/collect.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,9 @@ fn explicit_predicates_of(
20592059
ty::print::with_no_queries(|| {
20602060
let substs = InternalSubsts::identity_for_item(tcx, def_id);
20612061
let opaque_ty = tcx.mk_opaque(def_id, substs);
2062+
debug!("explicit_predicates_of({:?}): created opaque type {:?}",
2063+
def_id, opaque_ty);
2064+
20622065

20632066
// Collect the bounds, i.e., the `A + B + 'c` in `impl A + B + 'c`.
20642067
let bounds = AstConv::compute_bounds(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Regression test for issue #59342
2+
// Checks that we properly detect defining uses of opaque
3+
// types in 'item' position when generic parameters are involved
4+
//
5+
// run-pass
6+
#![feature(type_alias_impl_trait)]
7+
8+
trait Meow {
9+
type MeowType;
10+
fn meow(self) -> Self::MeowType;
11+
}
12+
13+
impl<T, I> Meow for I
14+
where I: Iterator<Item = T>
15+
{
16+
type MeowType = impl Iterator<Item = T>;
17+
fn meow(self) -> Self::MeowType {
18+
self
19+
}
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)