Skip to content

Commit 99aa063

Browse files
committed
Sema: Try harder to avoid tautological inference by looking at reduced types
If a requirement `f(_: Self.Element)` has a default implementation `f(_: Self.Iterator.Element)` where `Self: Sequence` for example, we don't want to consider the witness as a source of inference, because `Self.Element == Self.Iterator.Element` is always true. Compute the reduced type of the witness, so that we can rule out such cases using the existing logic.
1 parent 335d6ff commit 99aa063

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,18 @@ static Type getWitnessTypeForMatching(NormalProtocolConformance *conformance,
662662
genericFn->getExtInfo());
663663
}
664664

665+
auto &ctx = conformance->getDeclContext()->getASTContext();
666+
667+
// Get the reduced type of the witness. This rules our certain tautological
668+
// inferences below.
669+
if (ctx.LangOpts.EnableExperimentalAssociatedTypeInference) {
670+
if (auto genericSig = witness->getInnermostDeclContext()
671+
->getGenericSignatureOfContext()) {
672+
type = genericSig.getReducedType(type);
673+
type = genericSig->getSugaredType(type);
674+
}
675+
}
676+
665677
// Remap associated types that reference other protocols into this
666678
// protocol.
667679
auto proto = conformance->getProtocol();

0 commit comments

Comments
 (0)