Skip to content

Commit 3f5c9ea

Browse files
committed
Only diagnostic likely targetEnvironment(simulator) conditions at the top level
This matches the compiler's behavior.
1 parent fab7578 commit 3f5c9ea

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import SwiftSyntax
3030
/// diagnose syntax errors in blocks where the check fails.
3131
func evaluateIfConfig(
3232
condition: ExprSyntax,
33-
configuration: some BuildConfiguration
33+
configuration: some BuildConfiguration,
34+
outermostCondition: Bool = true
3435
) -> (active: Bool, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
3536
var extraDiagnostics: [Diagnostic] = []
3637

@@ -111,7 +112,8 @@ func evaluateIfConfig(
111112
{
112113
let (innerActive, innerSyntaxErrorsAllowed, innerDiagnostics) = evaluateIfConfig(
113114
condition: prefixOp.expression,
114-
configuration: configuration
115+
configuration: configuration,
116+
outermostCondition: outermostCondition
115117
)
116118

117119
return (active: !innerActive, syntaxErrorsAllowed: innerSyntaxErrorsAllowed, diagnostics: innerDiagnostics)
@@ -123,14 +125,17 @@ func evaluateIfConfig(
123125
(op.operator.text == "&&" || op.operator.text == "||")
124126
{
125127
// Check whether this was likely to be a check for targetEnvironment(simulator).
126-
if let targetEnvironmentDiag = diagnoseLikelySimulatorEnvironmentTest(binOp) {
128+
if outermostCondition,
129+
let targetEnvironmentDiag = diagnoseLikelySimulatorEnvironmentTest(binOp)
130+
{
127131
extraDiagnostics.append(targetEnvironmentDiag)
128132
}
129133

130134
// Evaluate the left-hand side.
131135
let (lhsActive, lhssyntaxErrorsAllowed, lhsDiagnostics) = evaluateIfConfig(
132136
condition: binOp.leftOperand,
133-
configuration: configuration
137+
configuration: configuration,
138+
outermostCondition: false
134139
)
135140

136141
// Short-circuit evaluation if we know the answer and the left-hand side
@@ -157,7 +162,8 @@ func evaluateIfConfig(
157162
// Evaluate the right-hand side.
158163
let (rhsActive, rhssyntaxErrorsAllowed, rhsDiagnostics) = evaluateIfConfig(
159164
condition: binOp.rightOperand,
160-
configuration: configuration
165+
configuration: configuration,
166+
outermostCondition: false
161167
)
162168

163169
switch op.operator.text {
@@ -186,7 +192,8 @@ func evaluateIfConfig(
186192
{
187193
return evaluateIfConfig(
188194
condition: element.expression,
189-
configuration: configuration
195+
configuration: configuration,
196+
outermostCondition: outermostCondition
190197
)
191198
}
192199

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public class EvaluateTests: XCTestCase {
391391

392392
func testLikelySimulatorEnvironment() throws {
393393
assertIfConfig(
394-
"((os(iOS) || os(tvOS)) && (arch(i386) || arch(x86_64))) && DEBUG",
394+
"((os(iOS) || os(tvOS)) && (arch(i386) || arch(x86_64)))",
395395
.inactive,
396396
diagnostics: [
397397
DiagnosticSpec(
@@ -408,7 +408,12 @@ public class EvaluateTests: XCTestCase {
408408
)
409409

410410
assertIfConfig(
411-
"((os(iOS) || os(tvOS)) && (arch(arm64) || arch(x86_64))) && DEBUG",
411+
"((os(iOS) || os(tvOS)) && (arch(arm64) || arch(x86_64)))",
412+
.inactive
413+
)
414+
415+
assertIfConfig(
416+
"((os(iOS) || os(tvOS)) && (arch(i386) || arch(x86_64))) && DEBUG",
412417
.inactive
413418
)
414419
}

0 commit comments

Comments
 (0)