Skip to content

[Parse] Disallow unsupported condition expression in #if directive #6855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,9 @@ ERROR(unexpected_version_comparison_operator,none,
())
ERROR(unsupported_conditional_compilation_expression_type,none,
"invalid conditional compilation expression", ())
WARNING(swift3_unsupported_conditional_compilation_expression_type,none,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: align whitespace on next line with (

"ignoring invalid conditional compilation expression, "
"which will be rejected in future version of Swift", ())
ERROR(unsupported_conditional_compilation_integer,none,
"'%0' is not a valid conditional compilation expression, use '%1'",
(StringRef, StringRef))
Expand Down
21 changes: 19 additions & 2 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1570,10 +1570,27 @@ Parser::classifyConditionalCompilationExpr(Expr *condition,
break;
}
} else {
D.diagnose(SE->getLoc(),
diag::unsupported_conditional_compilation_binary_expression);
D.diagnose(
SE->getLoc(),
diag::unsupported_conditional_compilation_binary_expression);
return ConditionalCompilationExprState::error();
}
} else {
// Swift3 didn't have this branch. the operator and the RHS are
// silently ignored.
if (!Context.isSwiftVersion3()) {
D.diagnose(
elements[iOperator]->getLoc(),
diag::unsupported_conditional_compilation_expression_type);
return ConditionalCompilationExprState::error();
} else {
SourceRange ignoredRange(elements[iOperator]->getLoc(),
elements[iOperand]->getEndLoc());
D.diagnose(
elements[iOperator]->getLoc(),
diag::swift3_unsupported_conditional_compilation_expression_type)
.highlight(ignoredRange);
}
}

iOperator += 2;
Expand Down
18 changes: 18 additions & 0 deletions test/Compatibility/conditional_compiliation_expr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %target-typecheck-verify-swift -D FOO -swift-version 3


#if FOO = false
// expected-warning @-1 {{ignoring invalid conditional compilation expression, which will be rejected in future version of Swift}}
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
#else
undefinedFunc() // ignored.
#endif

#if false

#elseif !FOO ? false : true
// expected-warning @-1 {{ignoring invalid conditional compilation expression, which will be rejected in future version of Swift}}
undefinedFunc() // ignored.
#else
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
#endif
17 changes: 16 additions & 1 deletion test/Parse/ConditionalCompilation/basicParseErrors.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -D FOO -D BAZ
// RUN: %target-typecheck-verify-swift -D FOO -D BAZ -swift-version 4

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

#if foo || bar || nonExistent() // expected-error {{expected only one argument to platform condition}}
#endif

#if FOO = false
// expected-error @-1 {{invalid conditional compilation expression}}
undefinedFunc() // ignored.
#else
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
#endif

#if false
#elseif FOO ? true : false
// expected-error @-1 {{invalid conditional compilation expression}}
undefinedFunc() // ignored.
#else
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}
#endif