Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serialization] Pass along allow errors/skip bodies flags #59739

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/Frontend/ModuleInterfaceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
static bool buildSwiftModuleFromSwiftInterface(
SourceManager &SourceMgr, DiagnosticEngine &Diags,
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
const ClangImporterOptions &ClangOpts, StringRef CacheDir,
const ClangImporterOptions &ClangOpts,
const TypeCheckerOptions &TypeCheckerOpts, StringRef CacheDir,
StringRef PrebuiltCacheDir, StringRef BackupInterfaceDir,
StringRef ModuleName, StringRef InPath,
StringRef OutPath, StringRef ABIOutputPath,
Expand Down Expand Up @@ -468,6 +469,7 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
void
inheritOptionsForBuildingInterface(const SearchPathOptions &SearchPathOpts,
const LangOptions &LangOpts,
const TypeCheckerOptions &TypeCheckerOpts,
RequireOSSAModules_t requireOSSAModules);
bool extractSwiftInterfaceVersionAndArgs(CompilerInvocation &subInvocation,
SmallVectorImpl<const char *> &SubArgs,
Expand All @@ -479,6 +481,7 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
SourceManager &SM, DiagnosticEngine *Diags,
const SearchPathOptions &searchPathOpts, const LangOptions &langOpts,
const ClangImporterOptions &clangImporterOpts,
const TypeCheckerOptions &typeCheckerOpts,
ModuleInterfaceLoaderOptions LoaderOpts, bool buildModuleCacheDirIfAbsent,
StringRef moduleCachePath, StringRef prebuiltCachePath,
StringRef backupModuleInterfaceDir,
Expand Down
6 changes: 3 additions & 3 deletions lib/DependencyScan/ScanDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
InterfaceSubContextDelegateImpl ASTDelegate(
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
ctx.ClangImporterOpts, LoaderOpts,
ctx.ClangImporterOpts, ctx.TypeCheckerOpts, LoaderOpts,
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
FEOpts.PrebuiltModuleCachePath,
FEOpts.BackupModuleInterfaceDir,
Expand Down Expand Up @@ -1516,7 +1516,7 @@ swift::dependencies::performBatchModuleScan(
allModules;
InterfaceSubContextDelegateImpl ASTDelegate(
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
ctx.ClangImporterOpts, LoaderOpts,
ctx.ClangImporterOpts, ctx.TypeCheckerOpts, LoaderOpts,
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
FEOpts.PrebuiltModuleCachePath,
FEOpts.BackupModuleInterfaceDir,
Expand Down Expand Up @@ -1586,7 +1586,7 @@ swift::dependencies::performBatchModulePrescan(
allModules;
InterfaceSubContextDelegateImpl ASTDelegate(
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
ctx.ClangImporterOpts, LoaderOpts,
ctx.ClangImporterOpts, ctx.TypeCheckerOpts, LoaderOpts,
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
FEOpts.PrebuiltModuleCachePath,
FEOpts.BackupModuleInterfaceDir,
Expand Down
5 changes: 3 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ bool CompilerInstance::setUpModuleLoaders() {
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
InterfaceSubContextDelegateImpl ASTDelegate(
Context->SourceMgr, &Context->Diags, Context->SearchPathOpts,
Context->LangOpts, Context->ClangImporterOpts, LoaderOpts,
Context->LangOpts, Context->ClangImporterOpts,
Context->TypeCheckerOpts, LoaderOpts,
/*buildModuleCacheDirIfAbsent*/ false, ModuleCachePath,
FEOpts.PrebuiltModuleCachePath,
FEOpts.BackupModuleInterfaceDir,
Expand Down Expand Up @@ -1318,7 +1319,7 @@ static void countStatsPostSILOpt(UnifiedStatsReporter &Stats,

bool CompilerInstance::performSILProcessing(SILModule *silModule) {
if (performMandatorySILPasses(Invocation, silModule) &&
!Invocation.getFrontendOptions().AllowModuleWithCompilerErrors)
!Invocation.getLangOptions().AllowModuleWithCompilerErrors)
return true;

{
Expand Down
6 changes: 4 additions & 2 deletions lib/Frontend/ModuleInterfaceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
};

SubInstance.performSema();
if (SubInstance.getASTContext().hadError()) {
if (SubInstance.getASTContext().hadError() &&
!SubInstance.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
LLVM_DEBUG(llvm::dbgs() << "encountered errors\n");
return std::make_error_code(std::errc::not_supported);
}
Expand Down Expand Up @@ -309,7 +310,8 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
LLVM_DEBUG(llvm::dbgs() << "encountered errors\n");
return std::make_error_code(std::errc::not_supported);
}
if (SubInstance.getDiags().hadAnyError()) {
if (SubInstance.getDiags().hadAnyError() &&
!SubInstance.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
return std::make_error_code(std::errc::not_supported);
}
return std::error_code();
Expand Down
22 changes: 17 additions & 5 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ class ModuleInterfaceLoaderImpl {
}
InterfaceSubContextDelegateImpl astDelegate(
ctx.SourceMgr, &ctx.Diags, ctx.SearchPathOpts, ctx.LangOpts,
ctx.ClangImporterOpts, Opts,
ctx.ClangImporterOpts, ctx.TypeCheckerOpts, Opts,
/*buildModuleCacheDirIfAbsent*/ true, cacheDir, prebuiltCacheDir,
backupInterfaceDir,
/*serializeDependencyHashes*/ false, trackSystemDependencies,
Expand Down Expand Up @@ -1226,16 +1226,17 @@ bool ModuleInterfaceCheckerImpl::tryEmitForwardingModule(
bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
SourceManager &SourceMgr, DiagnosticEngine &Diags,
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
const ClangImporterOptions &ClangOpts, StringRef CacheDir,
const ClangImporterOptions &ClangOpts,
const TypeCheckerOptions &TypeCheckerOpts, StringRef CacheDir,
StringRef PrebuiltCacheDir, StringRef BackupInterfaceDir,
StringRef ModuleName, StringRef InPath,
StringRef OutPath, StringRef ABIOutputPath,
bool SerializeDependencyHashes,
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions LoaderOpts,
RequireOSSAModules_t RequireOSSAModules) {
InterfaceSubContextDelegateImpl astDelegate(
SourceMgr, &Diags, SearchPathOpts, LangOpts, ClangOpts, LoaderOpts,
/*CreateCacheDirIfAbsent*/ true, CacheDir, PrebuiltCacheDir,
SourceMgr, &Diags, SearchPathOpts, LangOpts, ClangOpts, TypeCheckerOpts,
LoaderOpts, /*CreateCacheDirIfAbsent*/ true, CacheDir, PrebuiltCacheDir,
BackupInterfaceDir,
SerializeDependencyHashes, TrackSystemDependencies, RequireOSSAModules);
ModuleInterfaceBuilder builder(SourceMgr, &Diags, astDelegate, InPath,
Expand Down Expand Up @@ -1278,6 +1279,7 @@ void ModuleInterfaceLoader::collectVisibleTopLevelModuleNames(

void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
const SearchPathOptions &SearchPathOpts, const LangOptions &LangOpts,
const TypeCheckerOptions &TypeCheckerOpts,
RequireOSSAModules_t RequireOSSAModules) {
GenericArgs.push_back("-frontend");
// Start with a genericSubInvocation that copies various state from our
Expand Down Expand Up @@ -1364,6 +1366,15 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
std::string pair = (llvm::Twine(lhs) + "=" + rhs).str();
GenericArgs.push_back(ArgSaver.save(pair));
});

genericSubInvocation.getTypeCheckerOptions().SkipFunctionBodies =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to limit this to All only. Also need to consider how this impacts the hash - modules without skip/errors should be used by compilations without, but definitely not the other way around.

TypeCheckerOpts.SkipFunctionBodies;

// It could be the case that building a swiftinterface depends on a local
// module, which may have errors. This will *always* crash unless allowing
// errors is passed down.
genericSubInvocation.getLangOptions().AllowModuleWithCompilerErrors =
LangOpts.AllowModuleWithCompilerErrors;
}

bool InterfaceSubContextDelegateImpl::extractSwiftInterfaceVersionAndArgs(
Expand Down Expand Up @@ -1439,14 +1450,15 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
SourceManager &SM, DiagnosticEngine *Diags,
const SearchPathOptions &searchPathOpts, const LangOptions &langOpts,
const ClangImporterOptions &clangImporterOpts,
const TypeCheckerOptions &typeCheckerOpts,
ModuleInterfaceLoaderOptions LoaderOpts, bool buildModuleCacheDirIfAbsent,
StringRef moduleCachePath, StringRef prebuiltCachePath,
StringRef backupModuleInterfaceDir,
bool serializeDependencyHashes, bool trackSystemDependencies,
RequireOSSAModules_t requireOSSAModules)
: SM(SM), Diags(Diags), ArgSaver(Allocator) {
genericSubInvocation.setMainExecutablePath(LoaderOpts.mainExecutablePath);
inheritOptionsForBuildingInterface(searchPathOpts, langOpts,
inheritOptionsForBuildingInterface(searchPathOpts, langOpts, typeCheckerOpts,
requireOSSAModules);
// Configure front-end input.
auto &SubFEOpts = genericSubInvocation.getFrontendOptions();
Expand Down
2 changes: 1 addition & 1 deletion lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
return ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
Instance.getSourceMgr(), Instance.getDiags(),
Invocation.getSearchPathOptions(), Invocation.getLangOptions(),
Invocation.getClangImporterOptions(),
Invocation.getClangImporterOptions(), Invocation.getTypeCheckerOptions(),
Invocation.getClangModuleCachePath(), PrebuiltCachePath,
FEOpts.BackupModuleInterfaceDir,
Invocation.getModuleName(), InputPath, Invocation.getOutputFilename(), ABIPath,
Expand Down
6 changes: 5 additions & 1 deletion lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2675,7 +2675,11 @@ class DeclDeserializer {
if (!decl)
return;

if (IsInvalid) {
// Output an error to ensure SILGen will definitely not run when the AST
// is invalid. Skip if there's already been an error - a diagnostic that
// includes this decl may currently be in the process of being output.
// Prefer that diagnostic over this one.
if (IsInvalid && !ctx.Diags.hadAnyError()) {
decl->setInvalidBit();

DeclName name;
Expand Down
40 changes: 40 additions & 0 deletions test/ModuleInterface/allow-errors-applies-to-interface.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -module-name B -emit-module -emit-module-path %t/mods/B.swiftmodule -experimental-allow-module-with-compiler-errors -experimental-skip-all-function-bodies -module-cache-path %t/mcp %t/B.swift
// RUN: %target-swift-frontend -module-name C -emit-module -emit-module-path %t/mods/C.swiftmodule -experimental-allow-module-with-compiler-errors -experimental-skip-all-function-bodies -I%t/mods -module-cache-path %t/mcp %t/C.swift
// RUN: %target-swift-frontend -module-name C -emit-module -emit-module-path %t/mods/C.swiftmodule -experimental-allow-module-with-compiler-errors -experimental-skip-all-function-bodies -I%t/mods -module-cache-path %t/mcp %t/C.swift

//--- mods/A.swiftinterface
// swift-interface-format-version: 1.0
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name A -debug-forbid-typecheck-prefix NOTYPECHECK

import Swift
import B

public struct FromA: ProtoFromB {
public func badMethod(arg: Int) -> Int
}

@inlinable public func topLeveLFromA() -> Int {
let NOTYPECHECK_local = 1
return NOTYPECHECK_local
}

//--- B.swift
public protocol ProtoFromB {
func badMethod(arg: missing) -> Int
}

public struct StructFromB {
public field: missing
}

//--- C.swift
import A
import B

public func fromC(a: FromA) {
_ = a.badMethod(arg: 1)
_ = StructFromB()
}
24 changes: 24 additions & 0 deletions test/Serialization/AllowErrors/invalid-init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t/mods
// RUN: split-file %s %t

// RUN: %target-swift-frontend -module-name A -emit-module -emit-module-path %t/mods/A.swiftmodule -experimental-allow-module-with-compiler-errors %t/A.swift
// RUN: %target-swift-frontend -module-name B -emit-module -emit-module-path %t/mods/B.swiftmodule -experimental-allow-module-with-compiler-errors -I%t/mods %t/B.swift 2>&1 | %FileCheck %s

//--- A.swift
public struct StructFromA {
public func badMethod(arg: missing) {}
}

//--- B.swift
import A

public func test() {
// The init is internal so we expect to get a diagnostic for that. But
// deserializing `StructFromA` could potentially output a diagnostic for the
// error type on `field`. Make sure that we don't crash and that there is a
// diagnostic for the internal init.
_ = StructFromA()
// CHECK: error: 'StructFromA' initializer is inaccessible due to 'internal' protection level
}