Skip to content

Commit c347b3e

Browse files
committed
AST: Factor out maybeEmitFallbackConformanceDiagnostic() from addDelayedConformanceDiag()
1 parent 6306884 commit c347b3e

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

lib/AST/ASTContext.cpp

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,48 +2780,58 @@ ASTContext::MissingWitness::MissingWitness(ValueDecl *requirement,
27802780
: requirement(requirement),
27812781
matches(matches.begin(), matches.end()) { }
27822782

2783-
void ASTContext::addDelayedConformanceDiag(
2784-
NormalProtocolConformance *conformance, bool isError,
2785-
std::function<void(NormalProtocolConformance *)> callback) {
2786-
if (isError)
2787-
conformance->setInvalid();
2788-
2789-
auto &diagnostics = getImpl().DelayedConformanceDiags[conformance];
2783+
static void maybeEmitFallbackConformanceDiagnostic(
2784+
ASTContext &ctx,
2785+
NormalProtocolConformance *conformance,
2786+
DelayedConformanceDiags &diagnostics) {
27902787

2791-
if (isError && !diagnostics.HadError) {
2792-
diagnostics.HadError = true;
2788+
if (diagnostics.HadError)
2789+
return;
27932790

2794-
auto *proto = conformance->getProtocol();
2795-
auto *dc = conformance->getDeclContext();
2796-
auto *sf = dc->getParentSourceFile();
2797-
auto *mod = sf->getParentModule();
2798-
assert(mod->isMainModule());
2791+
diagnostics.HadError = true;
27992792

2800-
// If we have at least one primary file and the conformance is declared in a
2801-
// non-primary file, emit a fallback diagnostic.
2802-
if ((!sf->isPrimary() && !mod->getPrimarySourceFiles().empty()) ||
2803-
TypeCheckerOpts.EnableLazyTypecheck) {
2804-
auto complainLoc = evaluator.getInnermostSourceLoc([&](SourceLoc loc) {
2805-
if (loc.isInvalid())
2806-
return false;
2793+
auto *proto = conformance->getProtocol();
2794+
auto *dc = conformance->getDeclContext();
2795+
auto *sf = dc->getParentSourceFile();
2796+
auto *mod = sf->getParentModule();
2797+
assert(mod->isMainModule());
28072798

2808-
auto *otherSF = mod->getSourceFileContainingLocation(loc);
2809-
if (otherSF == nullptr)
2810-
return false;
2799+
// If we have at least one primary file and the conformance is declared in a
2800+
// non-primary file, emit a fallback diagnostic.
2801+
if ((!sf->isPrimary() && !mod->getPrimarySourceFiles().empty()) ||
2802+
ctx.TypeCheckerOpts.EnableLazyTypecheck) {
2803+
auto complainLoc = ctx.evaluator.getInnermostSourceLoc([&](SourceLoc loc) {
2804+
if (loc.isInvalid())
2805+
return false;
28112806

2812-
return otherSF->isPrimary();
2813-
});
2807+
auto *otherSF = mod->getSourceFileContainingLocation(loc);
2808+
if (otherSF == nullptr)
2809+
return false;
28142810

2815-
if (complainLoc.isInvalid()) {
2816-
complainLoc = conformance->getLoc();
2817-
}
2811+
return otherSF->isPrimary();
2812+
});
28182813

2819-
Diags.diagnose(complainLoc,
2820-
diag::type_does_not_conform,
2821-
dc->getSelfInterfaceType(),
2822-
proto->getDeclaredInterfaceType());
2814+
if (complainLoc.isInvalid()) {
2815+
complainLoc = conformance->getLoc();
28232816
}
2817+
2818+
ctx.Diags.diagnose(complainLoc,
2819+
diag::type_does_not_conform,
2820+
dc->getSelfInterfaceType(),
2821+
proto->getDeclaredInterfaceType());
28242822
}
2823+
}
2824+
2825+
void ASTContext::addDelayedConformanceDiag(
2826+
NormalProtocolConformance *conformance, bool isError,
2827+
std::function<void(NormalProtocolConformance *)> callback) {
2828+
if (isError)
2829+
conformance->setInvalid();
2830+
2831+
auto &diagnostics = getImpl().DelayedConformanceDiags[conformance];
2832+
2833+
if (isError)
2834+
maybeEmitFallbackConformanceDiagnostic(*this, conformance, diagnostics);
28252835

28262836
diagnostics.Diags.push_back({isError, callback});
28272837
}

0 commit comments

Comments
 (0)