@@ -2646,15 +2646,19 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
2646
2646
// TODO [OPAQUE SUPPORT]: diagnose multiple opaque types
2647
2647
SubstitutionMap underlyingSubs = Candidates.front ().second ;
2648
2648
if (Candidates.size () > 1 ) {
2649
- unsigned mismatchIndex = OpaqueDecl->getOpaqueGenericParams ().size ();
2650
- for (auto genericParam : OpaqueDecl->getOpaqueGenericParams ()) {
2651
- unsigned index = genericParam->getIndex ();
2652
- Type underlyingType = Candidates[0 ].second .getReplacementTypes ()[index];
2649
+ Optional<std::pair<unsigned , GenericTypeParamType *>> mismatch;
2650
+
2651
+ auto opaqueParams = OpaqueDecl->getOpaqueGenericParams ();
2652
+ for (auto index : indices (opaqueParams)) {
2653
+ auto *genericParam = opaqueParams[index];
2654
+
2655
+ Type underlyingType = Type (genericParam).subst (underlyingSubs);
2653
2656
bool found = false ;
2654
2657
for (const auto &candidate : Candidates) {
2655
- Type otherType = candidate.second .getReplacementTypes ()[index];
2658
+ Type otherType = Type (genericParam).subst (candidate.second );
2659
+
2656
2660
if (!underlyingType->isEqual (otherType)) {
2657
- mismatchIndex = index;
2661
+ mismatch. emplace ( index, genericParam) ;
2658
2662
found = true ;
2659
2663
break ;
2660
2664
}
@@ -2663,28 +2667,27 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
2663
2667
if (found)
2664
2668
break ;
2665
2669
}
2666
- assert (mismatchIndex < OpaqueDecl-> getOpaqueGenericParams (). size ());
2670
+ assert (mismatch. hasValue ());
2667
2671
2668
2672
if (auto genericParam =
2669
- OpaqueDecl->getExplicitGenericParam (mismatchIndex )) {
2673
+ OpaqueDecl->getExplicitGenericParam (mismatch-> first )) {
2670
2674
Implementation->diagnose (
2671
2675
diag::opaque_type_mismatched_underlying_type_candidates_named,
2672
2676
genericParam->getName ())
2673
2677
.highlight (genericParam->getLoc ());
2674
2678
} else {
2675
2679
TypeRepr *opaqueRepr =
2676
- OpaqueDecl->getOpaqueReturnTypeReprs ()[mismatchIndex ];
2680
+ OpaqueDecl->getOpaqueReturnTypeReprs ()[mismatch-> first ];
2677
2681
Implementation->diagnose (
2678
2682
diag::opaque_type_mismatched_underlying_type_candidates,
2679
2683
opaqueRepr)
2680
2684
.highlight (opaqueRepr->getSourceRange ());
2681
2685
}
2682
2686
2683
2687
for (auto candidate : Candidates) {
2684
- Ctx.Diags .diagnose (
2685
- candidate.first ->getLoc (),
2686
- diag::opaque_type_underlying_type_candidate_here,
2687
- candidate.second .getReplacementTypes ()[mismatchIndex]);
2688
+ Ctx.Diags .diagnose (candidate.first ->getLoc (),
2689
+ diag::opaque_type_underlying_type_candidate_here,
2690
+ Type (mismatch->second ).subst (candidate.second ));
2688
2691
}
2689
2692
return ;
2690
2693
}
0 commit comments