@@ -2316,7 +2316,7 @@ AssociatedTypeInference::computeAbstractTypeWitness(
2316
2316
continue ;
2317
2317
2318
2318
if (gp->getName () == assocType->getName ())
2319
- return AbstractTypeWitness (assocType, gp );
2319
+ return AbstractTypeWitness (assocType, dc-> mapTypeIntoContext (gp) );
2320
2320
}
2321
2321
}
2322
2322
@@ -2348,7 +2348,8 @@ void AssociatedTypeInference::collectAbstractTypeWitnesses(
2348
2348
continue ;
2349
2349
2350
2350
if (gp->getName () == assocType->getName ()) {
2351
- system.addTypeWitness (assocType->getName (), gp);
2351
+ system.addTypeWitness (assocType->getName (),
2352
+ dc->mapTypeIntoContext (gp));
2352
2353
}
2353
2354
}
2354
2355
}
@@ -2661,6 +2662,8 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
2661
2662
// not resolve otherwise.
2662
2663
llvm::SmallVector<AbstractTypeWitness, 2 > abstractTypeWitnesses;
2663
2664
2665
+ auto selfTypeInContext = dc->getSelfTypeInContext ();
2666
+
2664
2667
if (ctx.LangOpts .EnableExperimentalAssociatedTypeInference ) {
2665
2668
TypeWitnessSystem system (unresolvedAssocTypes);
2666
2669
collectAbstractTypeWitnesses (system, unresolvedAssocTypes);
@@ -2683,12 +2686,9 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
2683
2686
2684
2687
resolvedTy = resolvedTy.transformRec ([&](Type ty) -> llvm::Optional<Type> {
2685
2688
if (auto *gp = ty->getAs <GenericTypeParamType>()) {
2686
- // FIXME: 'computeFixedTypeWitness' uses 'getReducedType',
2687
- // so if a generic parameter is canonical here, it's 'Self'.
2688
- if (gp->isCanonical () ||
2689
- isa<ProtocolDecl>(gp->getDecl ()->getDeclContext ()->getAsDecl ())) {
2690
- return adoptee;
2691
- }
2689
+ assert (gp->getDepth () == 0 );
2690
+ assert (gp->getIndex () == 0 );
2691
+ return selfTypeInContext;
2692
2692
}
2693
2693
2694
2694
return llvm::None;
@@ -2715,12 +2715,9 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
2715
2715
2716
2716
resolvedTy = resolvedTy.transformRec ([&](Type ty) -> llvm::Optional<Type> {
2717
2717
if (auto *gp = ty->getAs <GenericTypeParamType>()) {
2718
- // FIXME: 'computeFixedTypeWitness' uses 'getReducedType',
2719
- // so if a generic parameter is canonical here, it's 'Self'.
2720
- if (gp->isCanonical () ||
2721
- isa<ProtocolDecl>(gp->getDecl ()->getDeclContext ()->getAsDecl ())) {
2722
- return adoptee;
2723
- }
2718
+ assert (gp->getDepth () == 0 );
2719
+ assert (gp->getIndex () == 0 );
2720
+ return selfTypeInContext;
2724
2721
}
2725
2722
2726
2723
return llvm::None;
@@ -2763,8 +2760,10 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
2763
2760
LLVM_DEBUG (llvm::dbgs () << " Checking witness for " << assocType->getName ()
2764
2761
<< " " << type << " \n " ;);
2765
2762
2763
+ assert (!type->hasTypeParameter ());
2764
+
2766
2765
// Replace type parameters with other known or tentative type witnesses.
2767
- if (type->hasDependentMember () || type-> hasTypeParameter () ) {
2766
+ if (type->hasDependentMember ()) {
2768
2767
// FIXME: We should find a better way to detect and reason about these
2769
2768
// cyclic solutions so that we can spot them earlier and express them in
2770
2769
// diagnostics.
@@ -2826,27 +2825,20 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
2826
2825
Type tyWitness;
2827
2826
if (assocTy->getProtocol () == proto && typeWitnesses.count (assocTy)) {
2828
2827
tyWitness = typeWitnesses.begin (assocTy)->first ;
2828
+ assert (!tyWitness->hasTypeParameter ());
2829
2829
2830
2830
// A tentative type witness may contain a 'Self'-rooted type
2831
2831
// parameter,
2832
2832
// FIXME: or a weird concrete-type-rooted dependent member type
2833
2833
// coming from inference via a value witness. Make sure we sort these
2834
2834
// out so that we don't break any subst() invariants.
2835
- if (tyWitness->hasTypeParameter () ||
2836
- tyWitness->hasDependentMember ()) {
2835
+ if (tyWitness->hasDependentMember ()) {
2837
2836
tyWitness = tyWitness.transformRec (substCurrentTypeWitnesses);
2838
2837
}
2839
2838
2840
2839
if (tyWitness) {
2841
- // HACK: Those inferred via value witnesses are eagerly mapped into
2842
- // context. For now, do the same for abstract type witnesses and
2843
- // handle archetypes.
2844
- if (tyWitness->hasArchetype ()) {
2845
- tyWitness = tyWitness->mapTypeOutOfContext ();
2846
- }
2847
-
2848
2840
// If the transformed base is specialized, apply substitutions.
2849
- if (tyWitness->hasTypeParameter ()) {
2841
+ if (tyWitness->hasArchetype ()) {
2850
2842
const auto conf = dc->getParentModule ()->lookupConformance (
2851
2843
substBase, assocTy->getProtocol (), /* allowMissing=*/ true );
2852
2844
if (auto *specialized = dyn_cast<SpecializedProtocolConformance>(
@@ -2878,8 +2870,6 @@ AssociatedTypeDecl *AssociatedTypeInference::inferAbstractTypeWitnesses(
2878
2870
// If any unresolved dependent member types remain, give up.
2879
2871
assert (!containsConcreteDependentMemberType (type));
2880
2872
2881
- type = dc->mapTypeIntoContext (type);
2882
-
2883
2873
LLVM_DEBUG (llvm::dbgs () << " Simplified witness type is " << type << " \n " ;);
2884
2874
}
2885
2875
0 commit comments