mirror of
https://github.com/apple/swift.git
synced 2026-09-28 10:03:29 +02:00
If the module named in `#if canImport(<Module>, _version: ...)` cannot be found, it may indicate that the developer misspelled the module name. Emit a warning to indicate that the directive evaluates to false. If it is intentional that the module not be found, the developer can avoid the warning by first checking whether the module can be imported and then check its version. Resolves rdar://128367758.
29 lines
1.0 KiB
Markdown
29 lines
1.0 KiB
Markdown
# Missing module for canImport (CanImportMissingModule)
|
|
|
|
Warnings that indicate a versioned `#if canImport(<ModuleName>, _version: <version>)` or `#if canImport(<ModuleName>, _underlyingVersion: <version>)` directive could not find the named module.
|
|
|
|
|
|
## Overview
|
|
|
|
Developers may use a versioned `#if canImport` directive to conditionally compile code based on the version of a dependency module:
|
|
|
|
```swift
|
|
#if canImport(Dependency, _version: 1.2)
|
|
// Use declarations introduced in version 1.2 of Dependency
|
|
#endif
|
|
```
|
|
|
|
A versioned `canImport` check evaluates to `false` if the named module cannot be resolved. The compiler emits a warning in case this case:
|
|
|
|
```
|
|
warning: cannot find module 'Dependency' for canImport check; the directive will evaluate to false [#CanImportMissingModule]
|
|
```
|
|
|
|
If the module is expected to be missing in some configurations, you can suppress the warning by checking whether the module can be imported at all before checking its version:
|
|
|
|
```swift
|
|
#if canImport(Dependency) && canImport(Dependency, _version: 1.2)
|
|
// ...
|
|
#endif
|
|
```
|