Skip to content

Commit 24ead55

Browse files
committed
Slight diagnostics cleanup for incorrect infix and prefix operators
1 parent 1a3f156 commit 24ead55

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Sources/SwiftIfConfig/IfConfigError.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum IfConfigError: Error, CustomStringConvertible {
3535
case macabiIsMacCatalyst(syntax: ExprSyntax)
3636
case expectedModuleName(syntax: ExprSyntax)
3737
case badInfixOperator(syntax: ExprSyntax)
38+
case badPrefixOperator(syntax: ExprSyntax)
3839

3940
var description: String {
4041
switch self {
@@ -97,6 +98,9 @@ enum IfConfigError: Error, CustomStringConvertible {
9798

9899
case .badInfixOperator:
99100
return "expected '&&' or '||' expression"
101+
102+
case .badPrefixOperator:
103+
return "expected unary '!' expression"
100104
}
101105
}
102106

@@ -120,7 +124,8 @@ enum IfConfigError: Error, CustomStringConvertible {
120124
.endiannessDoesNotMatch(syntax: let syntax, argument: _),
121125
.macabiIsMacCatalyst(syntax: let syntax),
122126
.expectedModuleName(syntax: let syntax),
123-
.badInfixOperator(syntax: let syntax):
127+
.badInfixOperator(syntax: let syntax),
128+
.badPrefixOperator(syntax: let syntax):
124129
return Syntax(syntax)
125130

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

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ func evaluateIfConfig(
107107
}
108108

109109
// Logical '!'.
110-
if let prefixOp = condition.as(PrefixOperatorExprSyntax.self),
111-
prefixOp.operator.text == "!"
112-
{
110+
if let prefixOp = condition.as(PrefixOperatorExprSyntax.self) {
111+
// If this isn't '!', complain.
112+
guard prefixOp.operator.text == "!" else {
113+
return recordError(.badPrefixOperator(syntax: condition))
114+
}
115+
113116
let (innerActive, innerSyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig(
114117
condition: prefixOp.expression,
115118
configuration: configuration,
@@ -121,9 +124,14 @@ func evaluateIfConfig(
121124

122125
// Logical '&&' and '||'.
123126
if let binOp = condition.as(InfixOperatorExprSyntax.self),
124-
let op = binOp.operator.as(BinaryOperatorExprSyntax.self),
125-
(op.operator.text == "&&" || op.operator.text == "||")
127+
let op = binOp.operator.as(BinaryOperatorExprSyntax.self)
126128
{
129+
// If this is neither && nor ||, it was already diagnosed as part of
130+
// operator folding. Just return this as inactive.
131+
guard op.operator.text == "&&" || op.operator.text == "||" else {
132+
return (active: false, syntaxErrorsAllowed: true, diagnostics: extraDiagnostics)
133+
}
134+
127135
// Check whether this was likely to be a check for targetEnvironment(simulator).
128136
if outermostCondition,
129137
let targetEnvironmentDiag = diagnoseLikelySimulatorEnvironmentTest(binOp)

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,19 @@ public class EvaluateTests: XCTestCase {
158158
message: "expected '&&' or '||' expression",
159159
line: 1,
160160
column: 3
161-
),
161+
)
162+
]
163+
)
164+
165+
assertIfConfig(
166+
"^DEBUG",
167+
.unparsed,
168+
diagnostics: [
162169
DiagnosticSpec(
163-
message: "invalid conditional compilation expression",
170+
message: "expected unary '!' expression",
164171
line: 1,
165172
column: 1
166-
),
173+
)
167174
]
168175
)
169176
}

0 commit comments

Comments
 (0)