Skip to content

Commit ec53b68

Browse files
committed
[Parse] Let #if diretives be parsed in parseBraceItems()
parseBraceItems() has specific logic for pasing conditional compilation blocks. Withoutout that, decralations in the block cannot be propagated to outside. For instance: FOO: #if true func foo() {} #endif foo() // error: use of unresolved identifier 'foo'
1 parent 54efbbb commit ec53b68

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ ParserResult<Stmt> Parser::parseStmt() {
522522
switch (Tok.getKind()) {
523523
case tok::pound_line:
524524
case tok::pound_sourceLocation:
525+
case tok::pound_if:
525526
assert((LabelInfo || tryLoc.isValid()) &&
526527
"unlabeled directives should be handled earlier");
527528
// Bailout, and let parseBraceItems() parse them.
@@ -546,10 +547,6 @@ ParserResult<Stmt> Parser::parseStmt() {
546547
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
547548
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
548549
return parseStmtGuard();
549-
case tok::pound_if:
550-
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
551-
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
552-
return parseStmtIfConfig();
553550
case tok::kw_while:
554551
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
555552
return parseStmtWhile(LabelInfo);

test/Parse/ConditionalCompilation/basicParseErrors.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,14 @@ struct S {
6464

6565
#if _endian(mid) // expected-warning {{unknown endianness for build configuration '_endian'}}
6666
#endif
67+
68+
LABEL: #if true // expected-error {{expected statement}}
69+
func fn_i() {}
70+
#endif
71+
fn_i() // OK
72+
73+
try #if false // expected-error {{expected expression}}
74+
#else
75+
func fn_j() {}
76+
#endif
77+
fn_j() // OK

0 commit comments

Comments
 (0)