Skip to content

Commit b950f06

Browse files
committed
[Parse] Disallow unsupported condition expression in #if directive
In non-Swift3 mode. Previously: #if FOO = false #elseif FOO ? false : true #endif were silently accepted. i.e. '= false' and '? false : true' were silently ignored.
1 parent c85cbe5 commit b950f06

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,10 +1570,22 @@ Parser::classifyConditionalCompilationExpr(Expr *condition,
15701570
break;
15711571
}
15721572
} else {
1573-
D.diagnose(SE->getLoc(),
1574-
diag::unsupported_conditional_compilation_binary_expression);
1573+
D.diagnose(
1574+
SE->getLoc(),
1575+
diag::unsupported_conditional_compilation_binary_expression);
15751576
return ConditionalCompilationExprState::error();
15761577
}
1578+
} else {
1579+
// Swift3 didn't have this branch. the operator and the RHS are
1580+
// silently ignored.
1581+
if (!Context.isSwiftVersion3()) {
1582+
D.diagnose(
1583+
elements[iOperator]->getLoc(),
1584+
diag::unsupported_conditional_compilation_expression_type);
1585+
return ConditionalCompilationExprState::error();
1586+
} else {
1587+
// TODO: Emit a warning.
1588+
}
15771589
}
15781590

15791591
iOperator += 2;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-typecheck-verify-swift -D FOO -swift-version 3
2+
3+
4+
#if FOO = false
5+
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
6+
#else
7+
undefinedFunc() // ignored.
8+
#endif
9+
10+
#if false
11+
12+
#elseif !FOO ? false : true
13+
undefinedFunc() // ignored.
14+
#else
15+
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
16+
#endif

test/Parse/ConditionalCompilation/basicParseErrors.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -D FOO -D BAZ
1+
// RUN: %target-typecheck-verify-swift -D FOO -D BAZ -swift-version 4
22

33
#if FOO == BAZ // expected-error{{expected '&&' or '||' expression}}
44
var x = 0
@@ -88,3 +88,18 @@ fn_j() // OK
8888

8989
#if foo || bar || nonExistent() // expected-error {{expected only one argument to platform condition}}
9090
#endif
91+
92+
#if FOO = false
93+
// expected-error @-1 {{invalid conditional compilation expression}}
94+
undefinedFunc() // ignored.
95+
#else
96+
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
97+
#endif
98+
99+
#if false
100+
#elseif FOO ? true : false
101+
// expected-error @-1 {{invalid conditional compilation expression}}
102+
undefinedFunc() // ignored.
103+
#else
104+
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
105+
#endif

0 commit comments

Comments
 (0)