Skip to content

Commit ce334af

Browse files
authored
Merge pull request #39444 from hamishknight/import-regulations
[Parse] Fix empty platform condition crash
2 parents fa04717 + 628dcd6 commit ce334af

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,9 @@ ERROR(unsupported_platform_condition_expression,none,
17471747
"unexpected platform condition "
17481748
"(expected 'os', 'arch', or 'swift')",
17491749
())
1750+
ERROR(platform_condition_expected_argument,none,
1751+
"expected argument to platform condition",
1752+
())
17501753
ERROR(platform_condition_expected_one_argument,none,
17511754
"expected only one argument to platform condition",
17521755
())

lib/Parse/ParseIfConfig.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ static llvm::VersionTuple getCanImportVersion(ArgumentList *args,
116116

117117
static Expr *getSingleSubExp(ArgumentList *args, StringRef kindName,
118118
DiagnosticEngine *D) {
119+
if (args->empty())
120+
return nullptr;
121+
119122
if (auto *unary = args->getUnlabeledUnaryExpr())
120123
return unary;
121124

@@ -260,7 +263,11 @@ class ValidateIfConfigCondition :
260263

261264
Expr *Arg = getSingleSubExp(E->getArgs(), *KindName, &D);
262265
if (!Arg) {
263-
D.diagnose(E->getLoc(), diag::platform_condition_expected_one_argument);
266+
if (E->getArgs()->empty()) {
267+
D.diagnose(E->getLoc(), diag::platform_condition_expected_argument);
268+
} else {
269+
D.diagnose(E->getLoc(), diag::platform_condition_expected_one_argument);
270+
}
264271
return nullptr;
265272
}
266273
// '_compiler_version' '(' string-literal ')'

test/Parse/ConditionalCompilation/basicParseErrors.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func fn_j() {}
9898
#endif
9999
fn_j() // OK
100100

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

104104
#if FOO = false
@@ -168,3 +168,8 @@ undefinedFunc() // expected-error {{cannot find 'undefinedFunc' in scope}}
168168
#else
169169
if true {}
170170
#endif // OK
171+
172+
// rdar://83017601 Make sure we don't crash
173+
#if canImport()
174+
// expected-error@-1 {{expected argument to platform condition}}
175+
#endif

0 commit comments

Comments
 (0)