Skip to content

Commit 10f85db

Browse files
authored
Merge pull request #63734 from Azoy/builtin-flag
[Frontend] Add option to explicitly import Builtin
2 parents 8819581 + 023c40c commit 10f85db

File tree

8 files changed

+77
-15
lines changed

8 files changed

+77
-15
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ namespace swift {
532532
/// The model of concurrency to be used.
533533
ConcurrencyModel ActiveConcurrencyModel = ConcurrencyModel::Standard;
534534

535+
/// Allows the explicit 'import Builtin' within Swift modules.
536+
bool EnableBuiltinModule = false;
537+
535538
bool isConcurrencyModelTaskToThread() const {
536539
return ActiveConcurrencyModel == ConcurrencyModel::TaskToThread;
537540
}

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,10 @@ def parse_stdlib : Flag<["-"], "parse-stdlib">,
10441044
Flags<[FrontendOption, HelpHidden, ModuleInterfaceOption]>,
10451045
HelpText<"Parse the input file(s) as the Swift standard library">;
10461046

1047+
def enable_builtin_module : Flag<["-"], "enable-builtin-module">,
1048+
Flags<[FrontendOption, ModuleInterfaceOption]>,
1049+
HelpText<"Enables the explicit import of the Builtin module">;
1050+
10471051
def modes_Group : OptionGroup<"<mode options>">, HelpText<"MODES">;
10481052

10491053
class ModeOpt : Group<modes_Group>;

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
307307
inputArgs.AddLastArg(arguments, options::OPT_enable_experimental_cxx_interop);
308308
inputArgs.AddLastArg(arguments, options::OPT_load_plugin_library);
309309
inputArgs.AddLastArg(arguments, options::OPT_load_plugin_executable);
310+
inputArgs.AddLastArg(arguments, options::OPT_enable_builtin_module);
310311

311312
// Pass on any build config options
312313
inputArgs.AddAllArgs(arguments, options::OPT_D);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
11051105
.Default(ConcurrencyModel::Standard);
11061106
}
11071107

1108+
Opts.EnableBuiltinModule = Args.hasArg(OPT_enable_builtin_module);
1109+
11081110
return HadError || UnsupportedOS || UnsupportedArch;
11091111
}
11101112

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,14 @@ static void printImports(raw_ostream &out,
293293

294294
for (auto import : allImports) {
295295
auto importedModule = import.importedModule;
296-
if (importedModule->isOnoneSupportModule() ||
297-
importedModule->isBuiltinModule()) {
296+
if (importedModule->isOnoneSupportModule()) {
297+
continue;
298+
}
299+
300+
// Unless '-enable-builtin-module' was passed, do not print 'import Builtin'
301+
// in the interface. '-parse-stdlib' still implicitly imports it however...
302+
if (importedModule->isBuiltinModule() &&
303+
!M->getASTContext().LangOpts.EnableBuiltinModule) {
298304
continue;
299305
}
300306

lib/Sema/ImportResolution.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,26 @@ void ImportResolver::addImport(const UnboundImport &I, ModuleDecl *M) {
354354
// MARK: Import module loading
355355
//===----------------------------------------------------------------------===//
356356

357-
static ModuleDecl *
358-
getModuleImpl(ImportPath::Module modulePath, ModuleDecl *loadingModule,
359-
bool canImportBuiltin) {
357+
ModuleDecl *
358+
ImportResolver::getModule(ImportPath::Module modulePath) {
359+
auto loadingModule = SF.getParentModule();
360+
360361
ASTContext &ctx = loadingModule->getASTContext();
361362

362363
assert(!modulePath.empty());
363364
auto moduleID = modulePath[0];
364365

365-
// The Builtin module cannot be explicitly imported unless we're a .sil file.
366-
if (canImportBuiltin && moduleID.Item == ctx.TheBuiltinModule->getName())
367-
return ctx.TheBuiltinModule;
366+
// The Builtin module cannot be explicitly imported unless:
367+
// 1. We're in a .sil file
368+
// 2. '-enable-builtin-module' was passed.
369+
//
370+
// FIXME: Eventually, it would be nice to separate '-parse-stdlib' from
371+
// implicitly importing Builtin, but we're not there yet.
372+
if (SF.Kind == SourceFileKind::SIL || ctx.LangOpts.EnableBuiltinModule) {
373+
if (moduleID.Item == ctx.TheBuiltinModule->getName()) {
374+
return ctx.TheBuiltinModule;
375+
}
376+
}
368377

369378
// If the imported module name is the same as the current module,
370379
// skip the Swift module loader and use the Clang module loader instead.
@@ -382,12 +391,6 @@ getModuleImpl(ImportPath::Module modulePath, ModuleDecl *loadingModule,
382391
return ctx.getModule(modulePath);
383392
}
384393

385-
ModuleDecl *
386-
ImportResolver::getModule(ImportPath::Module modulePath) {
387-
return getModuleImpl(modulePath, SF.getParentModule(),
388-
/*canImportBuiltin=*/SF.Kind == SourceFileKind::SIL);
389-
}
390-
391394
NullablePtr<ModuleDecl>
392395
UnboundImport::getTopLevelModule(ModuleDecl *M, SourceFile &SF) {
393396
if (import.module.getModulePath().size() == 1)
@@ -737,11 +740,21 @@ void UnboundImport::validateResilience(NullablePtr<ModuleDecl> topLevelModule,
737740
if (!topLevelModule || topLevelModule.get()->isNonSwiftModule())
738741
return;
739742

743+
ASTContext &ctx = SF.getASTContext();
744+
745+
// If the module we're validating is the builtin one, then just return because
746+
// this module is essentially a header only import and does not concern
747+
// itself with resiliency. This can occur when one has passed
748+
// '-enable-builtin-module' and is explicitly importing the Builtin module in
749+
// their sources.
750+
if (topLevelModule.get() == ctx.TheBuiltinModule) {
751+
return;
752+
}
753+
740754
if (!SF.getParentModule()->isResilient() ||
741755
topLevelModule.get()->isResilient())
742756
return;
743757

744-
ASTContext &ctx = SF.getASTContext();
745758
ctx.Diags.diagnose(import.module.getModulePath().front().Loc,
746759
diag::module_not_compiled_with_library_evolution,
747760
topLevelModule.get()->getName(),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: not %target-swift-frontend -typecheck -show-diagnostics-after-fatal -D BUILTIN_IMPORT %s 2>&1 | %FileCheck -check-prefix CHECK-NO-BUILTIN %s
2+
// RUN: not %target-swift-frontend -typecheck -show-diagnostics-after-fatal -enable-builtin-module %s 2>&1 | %FileCheck -check-prefix CHECK-NO-BUILTIN-IMPORT %s
3+
// RUN: %target-swift-frontend -typecheck -enable-builtin-module -D BUILTIN_IMPORT %s
4+
5+
// CHECK-NO-BUILTIN: no such module 'Builtin'
6+
7+
#if BUILTIN_IMPORT
8+
import Builtin
9+
#endif
10+
11+
// CHECK-NO-BUILTIN: cannot find type 'Builtin' in scope
12+
// CHECK-NO-BUILTIN-IMPORT: cannot find type 'Builtin' in scope
13+
14+
func something(_: Builtin.RawPointer) {
15+
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -enable-builtin-module
2+
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface)
3+
// RUN: %FileCheck %s < %t.swiftinterface
4+
5+
// CHECK: -enable-builtin-module
6+
7+
// CHECK: import Builtin
8+
// CHECK: import Swift
9+
// CHECK: import _Concurrency
10+
// CHECK: import _StringProcessing
11+
// CHECK: import _SwiftConcurrencyShims
12+
13+
// CHECK: public func something(with x: Builtin.RawPointer)
14+
15+
import Builtin
16+
17+
public func something(with x: Builtin.RawPointer) {}

0 commit comments

Comments
 (0)