Skip to content

Commit 5cb9227

Browse files
authored
Merge pull request swiftlang#37005 from artemcm/OldSDKNoConcurrency
Verify that `_Concurrency` *can* be imported on implicit import
2 parents c0394f6 + f92abb8 commit 5cb9227

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed

include/swift/AST/ASTContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,13 @@ class ASTContext final {
885885
/// If there is no Clang module loader, returns a null pointer.
886886
/// The loader is owned by the AST context.
887887
ClangModuleLoader *getDWARFModuleLoader() const;
888+
889+
/// Check whether the module with a given name can be imported without
890+
/// importing it.
891+
///
892+
/// Note that even if this check succeeds, errors may still occur if the
893+
/// module is loaded in full.
894+
bool canImportModuleImpl(ImportPath::Element ModulePath) const;
888895
public:
889896
namelookup::ImportCache &getImportCache() const;
890897

@@ -914,6 +921,7 @@ class ASTContext final {
914921
/// Note that even if this check succeeds, errors may still occur if the
915922
/// module is loaded in full.
916923
bool canImportModule(ImportPath::Element ModulePath);
924+
bool canImportModule(ImportPath::Element ModulePath) const;
917925

918926
/// \returns a module with a given name that was already loaded. If the
919927
/// module was not loaded, returns nullptr.

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ ERROR(cannot_emit_ir_skipping_function_bodies,none,
136136
WARNING(emit_reference_dependencies_without_primary_file,none,
137137
"ignoring -emit-reference-dependencies (requires -primary-file)", ())
138138

139+
WARNING(warn_implicit_concurrency_import_failed,none,
140+
"unable to perform implicit import of \"_Concurrency\" module: no such module found", ())
141+
139142
ERROR(error_module_name_required,none, "-module-name is required", ())
140143
ERROR(error_bad_module_name,none,
141144
"module name \"%0\" is not a valid identifier"

include/swift/Frontend/Frontend.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ class CompilerInstance {
529529
return getMainModule()->getPrimarySourceFiles();
530530
}
531531

532+
/// Verify that if an implicit import of the `Concurrency` module if expected,
533+
/// it can actually be imported. Emit a warning, otherwise.
534+
void verifyImplicitConcurrencyImport();
535+
536+
/// Whether the Swift Concurrency support library can be imported
537+
/// i.e. if it can be found.
538+
bool canImportSwiftConcurrency() const;
539+
532540
/// Gets the SourceFile which is the primary input for this CompilerInstance.
533541
/// \returns the primary SourceFile, or nullptr if there is no primary input;
534542
/// if there are _multiple_ primary inputs, fails with an assertion.

lib/AST/ASTContext.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,7 @@ bool ASTContext::shouldPerformTypoCorrection() {
19501950
return NumTypoCorrections <= LangOpts.TypoCorrectionLimit;
19511951
}
19521952

1953-
bool ASTContext::canImportModule(ImportPath::Element ModuleName) {
1953+
bool ASTContext::canImportModuleImpl(ImportPath::Element ModuleName) const {
19541954
// If this module has already been successfully imported, it is importable.
19551955
if (getLoadedModule(ImportPath::Module::Builder(ModuleName).get()) != nullptr)
19561956
return true;
@@ -1966,10 +1966,22 @@ bool ASTContext::canImportModule(ImportPath::Element ModuleName) {
19661966
}
19671967
}
19681968

1969-
FailedModuleImportNames.insert(ModuleName.Item);
19701969
return false;
19711970
}
19721971

1972+
bool ASTContext::canImportModule(ImportPath::Element ModuleName) {
1973+
if (canImportModuleImpl(ModuleName)) {
1974+
return true;
1975+
} else {
1976+
FailedModuleImportNames.insert(ModuleName.Item);
1977+
return false;
1978+
}
1979+
}
1980+
1981+
bool ASTContext::canImportModule(ImportPath::Element ModuleName) const {
1982+
return canImportModuleImpl(ModuleName);
1983+
}
1984+
19731985
ModuleDecl *
19741986
ASTContext::getModule(ImportPath::Module ModulePath) {
19751987
assert(!ModulePath.empty());

lib/Frontend/Frontend.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,19 @@ bool CompilerInvocation::shouldImportSwiftONoneSupport() const {
800800
FrontendOptions::doesActionGenerateSIL(options.RequestedAction);
801801
}
802802

803+
void CompilerInstance::verifyImplicitConcurrencyImport() {
804+
if (Invocation.shouldImportSwiftConcurrency() &&
805+
!canImportSwiftConcurrency()) {
806+
Diagnostics.diagnose(SourceLoc(),
807+
diag::warn_implicit_concurrency_import_failed);
808+
}
809+
}
810+
811+
bool CompilerInstance::canImportSwiftConcurrency() const {
812+
return getASTContext().canImportModule(
813+
{getASTContext().getIdentifier(SWIFT_CONCURRENCY_NAME), SourceLoc()});
814+
}
815+
803816
ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
804817
auto &frontendOpts = Invocation.getFrontendOptions();
805818

@@ -824,14 +837,18 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
824837
pushImport(SWIFT_ONONE_SUPPORT);
825838
}
826839

840+
// FIXME: The canImport check is required for compatibility
841+
// with older SDKs. Longer term solution is to have the driver make
842+
// the decision on the implicit import: rdar://76996377
827843
if (Invocation.shouldImportSwiftConcurrency()) {
828844
switch (imports.StdlibKind) {
829845
case ImplicitStdlibKind::Builtin:
830846
case ImplicitStdlibKind::None:
831847
break;
832848

833849
case ImplicitStdlibKind::Stdlib:
834-
pushImport(SWIFT_CONCURRENCY_NAME);
850+
if (canImportSwiftConcurrency())
851+
pushImport(SWIFT_CONCURRENCY_NAME);
835852
break;
836853
}
837854
}
@@ -1043,6 +1060,8 @@ bool CompilerInstance::loadStdlibIfNeeded() {
10431060
return true;
10441061
}
10451062

1063+
verifyImplicitConcurrencyImport();
1064+
10461065
// If we failed to load, we should have already diagnosed.
10471066
if (M->failedToLoad()) {
10481067
assert(Diagnostics.hadAnyError() &&
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This test ensures that if implicit import of the Concurrency module is enabled,
2+
// but no such module can be located (here verified by forcing explicit modules),
3+
// a warning diagnostic is emitted.
4+
// REQUIRES: concurrency
5+
// RUN: %empty-directory(%t)
6+
// RUN: mkdir -p %t/inputs
7+
8+
// RUN: echo "[{" > %/t/inputs/map.json
9+
// RUN: echo "\"moduleName\": \"Swift\"," >> %/t/inputs/map.json
10+
// RUN: echo "\"modulePath\": \"%/stdlib_module\"," >> %/t/inputs/map.json
11+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
12+
// RUN: echo "}," >> %/t/inputs/map.json
13+
// RUN: echo "{" >> %/t/inputs/map.json
14+
// RUN: echo "\"moduleName\": \"SwiftOnoneSupport\"," >> %/t/inputs/map.json
15+
// RUN: echo "\"modulePath\": \"%/ononesupport_module\"," >> %/t/inputs/map.json
16+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
17+
// RUN: echo "}]" >> %/t/inputs/map.json
18+
19+
// RUN: %target-swift-frontend -typecheck %s -explicit-swift-module-map-file %t/inputs/map.json -disable-implicit-swift-modules -enable-experimental-concurrency 2>&1 | %FileCheck %s
20+
import Swift
21+
// CHECK: warning: unable to perform implicit import of "_Concurrency" module: no such module found

0 commit comments

Comments
 (0)