From deebe8b9c7008b2b947b48142dcf9a46d75989fe Mon Sep 17 00:00:00 2001 From: Andrew Trick Date: Mon, 29 Jan 2018 15:22:28 -0800 Subject: [PATCH 1/2] performTypeChecking: Defer verifyAllLoadedModules in WMO mode. Suggested by Jordan Rose. Defer per-file verification in whole-module mode. Verifying imports between files could cause the importer to cache declarations without adding them to the ASTContext. This happens when the importer registers a declaration without a valid TypeChecker instance, as is the case during verification. A subsequent file may require that declaration to be fully imported (e.g. to synthesized a function body), but since it has already been cached, it will never be added to the ASTContext. The solution is to skip verification and avoid caching it. Instead, loaded modules are now verified in WMO mode once during performWholeModuleTypeChecking. Fixes [Edge][swift 4.1] SIL verification failed: external declarations of SILFunctions with shared visibility is not allowed. --- lib/Sema/TypeChecker.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Sema/TypeChecker.cpp b/lib/Sema/TypeChecker.cpp index e9118d04cc4fb..08f3072fd9238 100644 --- a/lib/Sema/TypeChecker.cpp +++ b/lib/Sema/TypeChecker.cpp @@ -675,8 +675,18 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC, verify(SF); // Verify imported modules. + // + // Skip per-file verification in whole-module mode. Verifying imports + // between files could cause the importer to cache declarations without + // adding them to the ASTContext. This happens when the importer registers a + // declaration without a valid TypeChecker instance, as is the case during + // verification. A subsequent file may require that declaration to be fully + // imported (e.g. to synthesized a function body), but since it has already + // been cached, it will never be added to the ASTContext. The solution is to + // skip verification and avoid caching it. #ifndef NDEBUG - if (SF.Kind != SourceFileKind::REPL && + if (!(Options & TypeCheckingFlags::DelayWholeModuleChecking) && + SF.Kind != SourceFileKind::REPL && SF.Kind != SourceFileKind::SIL && !Ctx.LangOpts.DebuggerSupport) { Ctx.verifyAllLoadedModules(); @@ -692,6 +702,16 @@ void swift::performWholeModuleTypeChecking(SourceFile &SF) { Ctx.diagnoseObjCMethodConflicts(SF); Ctx.diagnoseObjCUnsatisfiedOptReqConflicts(SF); Ctx.diagnoseUnintendedObjCMethodOverrides(SF); + + // In whole-module mode, import verification is deferred until all files have + // been type checked. This avoids caching imported declarations when a valid + // type checker is not present. The same declaration may need to be fully + // imported by subsequent files. + if (SF.Kind != SourceFileKind::REPL && + SF.Kind != SourceFileKind::SIL && + !Ctx.LangOpts.DebuggerSupport) { + Ctx.verifyAllLoadedModules(); + } } bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T, From d885078e6f1f3be22c7908216702738fa83b0499 Mon Sep 17 00:00:00 2001 From: Andrew Trick Date: Mon, 29 Jan 2018 17:29:57 -0800 Subject: [PATCH 2/2] Avoid enabling loaded module verification after WMO until playgrounds is fixed. playground_lvalues.swift: use of unresolved identifier '$builtin_log_with_id' Additional verification could expose this problem elsewhere. It will be enable on master later, but not as part of this PR. --- lib/Sema/TypeChecker.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Sema/TypeChecker.cpp b/lib/Sema/TypeChecker.cpp index 08f3072fd9238..b2bbe27ca6b01 100644 --- a/lib/Sema/TypeChecker.cpp +++ b/lib/Sema/TypeChecker.cpp @@ -707,11 +707,16 @@ void swift::performWholeModuleTypeChecking(SourceFile &SF) { // been type checked. This avoids caching imported declarations when a valid // type checker is not present. The same declaration may need to be fully // imported by subsequent files. + // + // FIXME: some playgrounds tests (playground_lvalues.swift) fail with + // verification enabled. +#if 0 if (SF.Kind != SourceFileKind::REPL && SF.Kind != SourceFileKind::SIL && !Ctx.LangOpts.DebuggerSupport) { Ctx.verifyAllLoadedModules(); } +#endif } bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,