Skip to content

Commit 43ce0d6

Browse files
committed
Provide TokenSyntax nodes for the import path in BuildConfiguration.canImport()
The compiler produces diagnostics for `canImport` checks, which requires more source location information than was previously provided via `BuildConfiguration.canImport`. Add in TokenSyntax nodes for each of the elements in the import path so that the compiler has the syntax node information it needs.
1 parent 5a19857 commit 43ce0d6

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import SwiftSyntax
1213

1314
/// Describes the ordering of a sequence of bytes that make up a word of
1415
/// storage for a particular architecture.
@@ -114,15 +115,15 @@ public protocol BuildConfiguration {
114115
/// information, which will translate into the `version` argument.
115116
///
116117
/// - Parameters:
117-
/// - importPath: A nonempty sequence of identifiers describing the
118-
/// imported module, which was written in source as a dotted sequence,
119-
/// e.g., `UIKit.UIViewController` will be passed in as the import path
120-
/// array `["UIKit", "UIViewController"]`.
118+
/// - importPath: A nonempty sequence of (token, identifier) pairs
119+
/// describing the imported module, which was written in source as a
120+
/// dotted sequence, e.g., `UIKit.UIViewController` will be passed in as
121+
/// the import path array `[(token, "UIKit"), (token, "UIViewController")]`.
121122
/// - version: The version restriction on the imported module. For the
122123
/// normal `canImport(<import-path>)` syntax, this will always be
123124
/// `CanImportVersion.unversioned`.
124125
/// - Returns: Whether the module can be imported.
125-
func canImport(importPath: [String], version: CanImportVersion) throws -> Bool
126+
func canImport(importPath: [(TokenSyntax, String)], version: CanImportVersion) throws -> Bool
126127

127128
/// Determine whether the given name is the active target OS (e.g., Linux, iOS).
128129
///

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func evaluateIfConfig(
443443
}
444444

445445
// Extract the import path.
446-
let importPath: [String]
446+
let importPath: [(TokenSyntax, String)]
447447
do {
448448
importPath = try extractImportPath(firstArg.expression)
449449
} catch {
@@ -502,7 +502,7 @@ func evaluateIfConfig(
502502
return checkConfiguration(at: call) {
503503
(
504504
active: try configuration.canImport(
505-
importPath: importPath.map { String($0) },
505+
importPath: importPath,
506506
version: version
507507
),
508508
syntaxErrorsAllowed: fn.syntaxErrorsAllowed
@@ -540,22 +540,22 @@ extension SyntaxProtocol {
540540
}
541541

542542
/// Given an expression with the expected form A.B.C, extract the import path
543-
/// ["A", "B", "C"] from it. Throws an error if the expression doesn't match
544-
/// this form.
545-
private func extractImportPath(_ expression: some ExprSyntaxProtocol) throws -> [String] {
543+
/// ["A", "B", "C"] from it with the token syntax nodes for each name.
544+
/// Throws an error if the expression doesn't match this form.
545+
private func extractImportPath(_ expression: some ExprSyntaxProtocol) throws -> [(TokenSyntax, String)] {
546546
// Member access.
547547
if let memberAccess = expression.as(MemberAccessExprSyntax.self),
548548
let base = memberAccess.base,
549549
let memberName = memberAccess.declName.simpleIdentifier?.name
550550
{
551-
return try extractImportPath(base) + [memberName]
551+
return try extractImportPath(base) + [(memberAccess.declName.baseName, memberName)]
552552
}
553553

554554
// Declaration reference.
555555
if let declRef = expression.as(DeclReferenceExprSyntax.self),
556556
let name = declRef.simpleIdentifier?.name
557557
{
558-
return [name]
558+
return [(declRef.baseName, name)]
559559
}
560560

561561
throw IfConfigDiagnostic.expectedModuleName(syntax: ExprSyntax(expression))
@@ -794,7 +794,7 @@ private struct CanImportSuppressingBuildConfiguration<Other: BuildConfiguration>
794794
return try other.hasAttribute(name: name)
795795
}
796796

797-
func canImport(importPath: [String], version: CanImportVersion) throws -> Bool {
797+
func canImport(importPath: [(TokenSyntax, String)], version: CanImportVersion) throws -> Bool {
798798
return false
799799
}
800800

0 commit comments

Comments
 (0)