Skip to content

Commit dacd986

Browse files
committed
AST: Introduce a warning group for a versioned #if canImport diagnostic.
This allows developers to control whether these diagnostics are considered errors when `-warnings-as-errors` is specified. Resolves rdar://157694667.
1 parent 09edc47 commit dacd986

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ GROUP(ImplementationOnlyDeprecated, "implementation-only-deprecated")
5353
GROUP(IsolatedConformances, "isolated-conformances")
5454
GROUP(MemberImportVisibility, "member-import-visibility")
5555
GROUP(MissingModuleOnKnownPaths, "missing-module-on-known-paths")
56+
GROUP(ModuleVersionMissing, "module-version-missing")
5657
GROUP(MultipleInheritance, "multiple-inheritance")
5758
GROUP(MutableGlobalVariable, "mutable-global-variable")
5859
GROUP(NominalTypes, "nominal-types")

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ REMARK(explicit_interface_build_skipped,none,
13091309
"Skipped rebuilding module at %0 - up-to-date",
13101310
(StringRef))
13111311

1312-
WARNING(cannot_find_project_version,none,
1312+
GROUPED_WARNING(cannot_find_module_version,ModuleVersionMissing,none,
13131313
"cannot find user version number for%select{| Clang}1 module '%0';"
13141314
" version number ignored", (StringRef, bool))
13151315

lib/AST/ASTContext.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,7 @@ void ASTContext::addSucceededCanImportModule(
26172617

26182618
bool ASTContext::canImportModuleImpl(
26192619
ImportPath::Module ModuleName, SourceLoc loc, llvm::VersionTuple version,
2620-
bool underlyingVersion, bool isSourceCanImport,
2620+
bool isUnderlyingVersion, bool isSourceCanImport,
26212621
llvm::VersionTuple &foundVersion,
26222622
llvm::VersionTuple &foundUnderlyingClangVersion) const {
26232623
SmallString<64> FullModuleName;
@@ -2629,16 +2629,16 @@ bool ASTContext::canImportModuleImpl(
26292629
return false;
26302630

26312631
auto missingVersion = [this, &loc, &ModuleName,
2632-
&underlyingVersion]() -> bool {
2632+
&isUnderlyingVersion]() -> bool {
26332633
// The module version could not be parsed from the preferred source for
2634-
// this query. Diagnose and return `true` to indicate that the unversioned module
2635-
// will satisfy the query.
2634+
// this query. Diagnose and return `true` to indicate that the unversioned
2635+
// module will satisfy the query.
26362636
auto mID = ModuleName[0];
26372637
auto diagLoc = mID.Loc;
26382638
if (mID.Loc.isInvalid())
26392639
diagLoc = loc;
2640-
Diags.diagnose(diagLoc, diag::cannot_find_project_version, mID.Item.str(),
2641-
underlyingVersion);
2640+
Diags.diagnose(diagLoc, diag::cannot_find_module_version, mID.Item.str(),
2641+
isUnderlyingVersion);
26422642
return true;
26432643
};
26442644

@@ -2650,9 +2650,9 @@ bool ASTContext::canImportModuleImpl(
26502650
if (version.empty())
26512651
return true;
26522652

2653-
const auto &foundComparisonVersion = underlyingVersion
2654-
? Found->second.UnderlyingVersion
2655-
: Found->second.Version;
2653+
const auto &foundComparisonVersion = isUnderlyingVersion
2654+
? Found->second.UnderlyingVersion
2655+
: Found->second.Version;
26562656
if (!foundComparisonVersion.empty())
26572657
return version <= foundComparisonVersion;
26582658
else
@@ -2699,10 +2699,10 @@ bool ASTContext::canImportModuleImpl(
26992699
bestUnderlyingVersionInfo = versionInfo;
27002700
}
27012701

2702-
if (!underlyingVersion && !bestVersionInfo.isValid())
2702+
if (!isUnderlyingVersion && !bestVersionInfo.isValid())
27032703
return false;
27042704

2705-
if (underlyingVersion && !bestUnderlyingVersionInfo.isValid())
2705+
if (isUnderlyingVersion && !bestUnderlyingVersionInfo.isValid())
27062706
return false;
27072707

27082708
foundVersion = bestVersionInfo.getVersion();
@@ -2751,7 +2751,8 @@ bool ASTContext::canImportModuleImpl(
27512751
if (!lookupVersionedModule(versionInfo, underlyingVersionInfo))
27522752
return false;
27532753

2754-
const auto &queryVersion = underlyingVersion ? underlyingVersionInfo : versionInfo;
2754+
const auto &queryVersion =
2755+
isUnderlyingVersion ? underlyingVersionInfo : versionInfo;
27552756
if (queryVersion.getVersion().empty())
27562757
return missingVersion();
27572758

userdocs/diagnostics/diagnostic-groups.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Or upgrade all warnings except deprecated declaration to errors:
3333
- <doc:preconcurrency-import>
3434
- <doc:clang-declaration-import>
3535
- <doc:missing-module-on-known-paths>
36+
- <doc:module-version-missing>
3637
- <doc:strict-language-features>
3738
- <doc:strict-memory-safety>
3839
- <doc:unknown-warning-group>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Missing module version (ModuleVersionMissing)
2+
3+
Warnings that indicate the compiler cannot resolve an `#if canImport(<ModuleName>, _version: <version>)` directive because the module found was not built with a `-user-module-version` flag.
4+
5+
6+
## Overview
7+
8+
Developers may conditionalize which code is active in a source file based on the module version number of an imported dependency module, like this:
9+
10+
```swift
11+
import Dependency
12+
13+
#if canImport(Dependency, _version: 1.2)
14+
// Use declarations introduced in version 1.2 of Dependency
15+
#else
16+
// ...
17+
#endif
18+
```
19+
20+
A dependency's module version must be established by passing the `-user-module-version` flag when compiling the sources of the dependency the module. If no `-user-module-version` flag was specified when the dependency module was built, then the compiler will warn that it cannot resolve the `#if canImport` directive:
21+
22+
```
23+
warning: cannot find user version number for module 'Dependency'; version number ignored [#ModuleVersionMissing]
24+
```
25+
26+
If this diagnostic is emitted, then the `#if canImport` directive implicitly evaluates true.

0 commit comments

Comments
 (0)