Skip to content

Commit f43a15e

Browse files
authored
Merge pull request swiftlang#31983 from hamishknight/import-regulations
2 parents fc63682 + 437da8c commit f43a15e

File tree

6 files changed

+52
-30
lines changed

6 files changed

+52
-30
lines changed

lib/AST/ASTVerifier.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,18 @@ class Verifier : public ASTWalker {
27322732
PrettyStackTraceDecl debugStack("verifying GenericTypeParamDecl", GTPD);
27332733

27342734
const DeclContext *DC = GTPD->getDeclContext();
2735-
if (!GTPD->getDeclContext()->isInnermostContextGeneric()) {
2735+
2736+
// Skip verification of deserialized generic param decls that have the
2737+
// the file set as their parent. This happens when they have not yet had
2738+
// their correct parent set.
2739+
// FIXME: This is a hack to workaround the fact that we don't necessarily
2740+
// parent a GenericTypeParamDecl if we just deserialize its type.
2741+
if (auto *fileDC = dyn_cast<FileUnit>(DC)) {
2742+
if (fileDC->getKind() == FileUnitKind::SerializedAST)
2743+
return;
2744+
}
2745+
2746+
if (!DC->isInnermostContextGeneric()) {
27362747
Out << "DeclContext of GenericTypeParamDecl does not have "
27372748
"generic params\n";
27382749
abort();

lib/Frontend/Frontend.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,10 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage,
744744
if (LimitStage > SourceFile::Unprocessed &&
745745
Invocation.getImplicitStdlibKind() == ImplicitStdlibKind::Stdlib
746746
&& !loadStdlib()) {
747+
// If we failed to load the stdlib, mark the main module as having
748+
// "failed to load", as it will contain no files.
749+
// FIXME: We need to better handle a missing stdlib.
750+
mainModule->setFailedToLoad();
747751
return;
748752
}
749753

@@ -757,8 +761,12 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage,
757761
// If we aren't in a parse-only context, load the remaining serialized inputs
758762
// and resolve implicit imports.
759763
if (LimitStage > SourceFile::Unprocessed &&
760-
loadPartialModulesAndImplicitImports())
764+
loadPartialModulesAndImplicitImports()) {
765+
// If we failed to load a partial module, mark the main module as having
766+
// "failed to load", as it may contain no files.
767+
mainModule->setFailedToLoad();
761768
return;
769+
}
762770

763771
// Then parse all the input files.
764772
// FIXME: This is the only demand point for InputSourceCodeBufferIDs. We

lib/FrontendTool/FrontendTool.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,20 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
12091209
return hadAnyError;
12101210
}
12111211

1212+
/// Perform any actions that must have access to the ASTContext, and need to be
1213+
/// delayed until the Swift compile pipeline has finished. This may be called
1214+
/// before or after LLVM depending on when the ASTContext gets freed.
1215+
static void performEndOfPipelineActions(CompilerInstance &Instance) {
1216+
assert(Instance.hasASTContext());
1217+
1218+
// Verify the AST for all the modules we've loaded.
1219+
Instance.getASTContext().verifyAllLoadedModules();
1220+
1221+
// Emit dependencies and index data.
1222+
emitReferenceDependenciesForAllPrimaryInputsIfNeeded(Instance);
1223+
emitIndexData(Instance);
1224+
}
1225+
12121226
/// Performs the compile requested by the user.
12131227
/// \param Instance Will be reset after performIRGeneration when the verifier
12141228
/// mode is NoVerify and there were no errors.
@@ -1301,15 +1315,12 @@ static bool performCompile(CompilerInstance &Instance,
13011315
emitCompiledSourceForAllPrimaryInputsIfNeeded(Instance);
13021316

13031317
SWIFT_DEFER {
1304-
// We might have freed the ASTContext already, but in that case we must have
1305-
// emitted the dependencies and index first.
1306-
if (Instance.hasASTContext()) {
1307-
emitReferenceDependenciesForAllPrimaryInputsIfNeeded(Instance);
1308-
emitIndexData(Instance);
1309-
}
1318+
// We might have freed the ASTContext already, but in that case we would
1319+
// have already performed these actions.
1320+
if (Instance.hasASTContext())
1321+
performEndOfPipelineActions(Instance);
13101322
};
13111323

1312-
13131324
if (Context.hadError())
13141325
return true;
13151326

@@ -1504,10 +1515,9 @@ static void freeASTContextIfPossible(CompilerInstance &Instance) {
15041515
return;
15051516
}
15061517

1507-
// Make sure we emit dependencies and index now, because we can't do it after
1508-
// the context is gone.
1509-
emitReferenceDependenciesForAllPrimaryInputsIfNeeded(Instance);
1510-
emitIndexData(Instance);
1518+
// Make sure to perform the end of pipeline actions now, because they need
1519+
// access to the ASTContext.
1520+
performEndOfPipelineActions(Instance);
15111521

15121522
Instance.freeASTContext();
15131523
}

lib/Sema/TypeChecker.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,20 +379,6 @@ void swift::performWholeModuleTypeChecking(SourceFile &SF) {
379379
diagnoseObjCMethodConflicts(SF);
380380
diagnoseObjCUnsatisfiedOptReqConflicts(SF);
381381
diagnoseUnintendedObjCMethodOverrides(SF);
382-
383-
// In whole-module mode, import verification is deferred until all files have
384-
// been type checked. This avoids caching imported declarations when a valid
385-
// type checker is not present. The same declaration may need to be fully
386-
// imported by subsequent files.
387-
//
388-
// FIXME: some playgrounds tests (playground_lvalues.swift) fail with
389-
// verification enabled.
390-
#if 0
391-
if (SF.Kind != SourceFileKind::SIL &&
392-
!Ctx.LangOpts.DebuggerSupport) {
393-
Ctx.verifyAllLoadedModules();
394-
}
395-
#endif
396382
}
397383

398384
bool swift::isAdditiveArithmeticConformanceDerivationEnabled(SourceFile &SF) {

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,9 +2487,9 @@ class DeclDeserializer {
24872487
depth,
24882488
index);
24892489

2490-
// Always create GenericTypeParamDecls in the associated module;
2491-
// the real context will reparent them.
2492-
auto DC = MF.getAssociatedModule();
2490+
// Always create GenericTypeParamDecls in the associated file; the real
2491+
// context will reparent them.
2492+
auto *DC = MF.getFile();
24932493
auto genericParam = MF.createDecl<GenericTypeParamDecl>(
24942494
DC, MF.getIdentifier(nameID), SourceLoc(), depth, index);
24952495
declOrOffset = genericParam;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// Make sure we don't crash if we fail to load a partial module.
4+
// RUN: %target-swift-frontend -emit-module -primary-file %s -module-name A -o %t/a.partial.swiftmodule
5+
// RUN: not %target-swift-frontend -emit-module -merge-modules %t/a.partial.swiftmodule -module-name Unrelated 2>&1 | %FileCheck %s
6+
7+
// CHECK: error: cannot load module 'A' as 'Unrelated'

0 commit comments

Comments
 (0)