Skip to content

Commit fab7578

Browse files
committed
Customize "unknown infix operator" diagnostic to make it clearer
Only && and || are allowed as infix operators within an #if condition, so note that in the diagnostic (just like the compiler does).
1 parent 757d2b3 commit fab7578

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

Sources/SwiftIfConfig/IfConfigError.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum IfConfigError: Error, CustomStringConvertible {
3434
case endiannessDoesNotMatch(syntax: ExprSyntax, argument: String)
3535
case macabiIsMacCatalyst(syntax: ExprSyntax)
3636
case expectedModuleName(syntax: ExprSyntax)
37+
case badInfixOperator(syntax: ExprSyntax)
3738

3839
var description: String {
3940
switch self {
@@ -93,6 +94,9 @@ enum IfConfigError: Error, CustomStringConvertible {
9394

9495
case .expectedModuleName:
9596
return "expected module name"
97+
98+
case .badInfixOperator:
99+
return "expected '&&' or '||' expression"
96100
}
97101
}
98102

@@ -115,7 +119,8 @@ enum IfConfigError: Error, CustomStringConvertible {
115119
.likelySimulatorPlatform(syntax: let syntax),
116120
.endiannessDoesNotMatch(syntax: let syntax, argument: _),
117121
.macabiIsMacCatalyst(syntax: let syntax),
118-
.expectedModuleName(syntax: let syntax):
122+
.expectedModuleName(syntax: let syntax),
123+
.badInfixOperator(syntax: let syntax):
119124
return Syntax(syntax)
120125

121126
case .unsupportedVersionOperator(name: _, operator: let op):

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ extension IfConfigClauseSyntax {
589589
) -> (folded: ExprSyntax, diagnostics: [Diagnostic]) {
590590
var foldingDiagnostics: [Diagnostic] = []
591591
let foldedCondition = OperatorTable.logicalOperators.foldAll(condition) { error in
592+
// Replace the "unknown infix operator" diagnostic with a custom one
593+
// that mentions that only '&&' and '||' are allowed.
594+
if case .missingOperator(_, referencedFrom: let syntax) = error,
595+
let binOp = syntax.parent?.as(BinaryOperatorExprSyntax.self)
596+
{
597+
598+
foldingDiagnostics.append(
599+
IfConfigError.badInfixOperator(syntax: ExprSyntax(binOp)).asDiagnostic
600+
)
601+
return
602+
}
603+
592604
foldingDiagnostics.append(contentsOf: error.asDiagnostics(at: condition))
593605
}.cast(ExprSyntax.self)
594606
return (folded: foldedCondition, diagnostics: foldingDiagnostics)

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ public class EvaluateTests: XCTestCase {
148148
)
149149
]
150150
)
151+
152+
assertIfConfig(
153+
"A == B",
154+
.unparsed,
155+
configuration: buildConfig,
156+
diagnostics: [
157+
DiagnosticSpec(
158+
message: "expected '&&' or '||' expression",
159+
line: 1,
160+
column: 3
161+
),
162+
DiagnosticSpec(
163+
message: "invalid conditional compilation expression",
164+
line: 1,
165+
column: 1
166+
),
167+
]
168+
)
151169
}
152170

153171
func testFeatures() throws {

0 commit comments

Comments
 (0)