Skip to content

Commit e8bd702

Browse files
authored
Merge pull request #6855 from rintaro/parse-ifconfig-invalid-binary
[Parse] Disallow unsupported condition expression in #if directive
2 parents 7e34f50 + b2d549e commit e8bd702

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,9 @@ ERROR(unexpected_version_comparison_operator,none,
13911391
())
13921392
ERROR(unsupported_conditional_compilation_expression_type,none,
13931393
"invalid conditional compilation expression", ())
1394+
WARNING(swift3_unsupported_conditional_compilation_expression_type,none,
1395+
"ignoring invalid conditional compilation expression, "
1396+
"which will be rejected in future version of Swift", ())
13941397
ERROR(unsupported_conditional_compilation_integer,none,
13951398
"'%0' is not a valid conditional compilation expression, use '%1'",
13961399
(StringRef, StringRef))

lib/Parse/ParseStmt.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,10 +1562,27 @@ Parser::classifyConditionalCompilationExpr(Expr *condition,
15621562
break;
15631563
}
15641564
} else {
1565-
D.diagnose(SE->getLoc(),
1566-
diag::unsupported_conditional_compilation_binary_expression);
1565+
D.diagnose(
1566+
SE->getLoc(),
1567+
diag::unsupported_conditional_compilation_binary_expression);
15671568
return ConditionalCompilationExprState::error();
15681569
}
1570+
} else {
1571+
// Swift3 didn't have this branch. the operator and the RHS are
1572+
// silently ignored.
1573+
if (!Context.isSwiftVersion3()) {
1574+
D.diagnose(
1575+
elements[iOperator]->getLoc(),
1576+
diag::unsupported_conditional_compilation_expression_type);
1577+
return ConditionalCompilationExprState::error();
1578+
} else {
1579+
SourceRange ignoredRange(elements[iOperator]->getLoc(),
1580+
elements[iOperand]->getEndLoc());
1581+
D.diagnose(
1582+
elements[iOperator]->getLoc(),
1583+
diag::swift3_unsupported_conditional_compilation_expression_type)
1584+
.highlight(ignoredRange);
1585+
}
15691586
}
15701587

15711588
iOperator += 2;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-typecheck-verify-swift -D FOO -swift-version 3
2+
3+
4+
#if FOO = false
5+
// expected-warning @-1 {{ignoring invalid conditional compilation expression, which will be rejected in future version of Swift}}
6+
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
7+
#else
8+
undefinedFunc() // ignored.
9+
#endif
10+
11+
#if false
12+
13+
#elseif !FOO ? false : true
14+
// expected-warning @-1 {{ignoring invalid conditional compilation expression, which will be rejected in future version of Swift}}
15+
undefinedFunc() // ignored.
16+
#else
17+
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
18+
#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)