Skip to content

Commit ceab3bc

Browse files
authored
Merge pull request #6093 from rintaro/parse-labeled-pound
[Parse] Make sure pound-directives be parsed in 'parseBraceItems()'
2 parents 3328592 + ec53b68 commit ceab3bc

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/Parse/Parser.h"
1818
#include "swift/AST/Attr.h"
1919
#include "swift/AST/Decl.h"
20+
#include "swift/Basic/Fallthrough.h"
2021
#include "swift/Basic/Version.h"
2122
#include "swift/Parse/Lexer.h"
2223
#include "swift/Parse/CodeCompletionCallbacks.h"
@@ -519,6 +520,13 @@ ParserResult<Stmt> Parser::parseStmt() {
519520
(void)consumeIf(tok::kw_try, tryLoc);
520521

521522
switch (Tok.getKind()) {
523+
case tok::pound_line:
524+
case tok::pound_sourceLocation:
525+
case tok::pound_if:
526+
assert((LabelInfo || tryLoc.isValid()) &&
527+
"unlabeled directives should be handled earlier");
528+
// Bailout, and let parseBraceItems() parse them.
529+
SWIFT_FALLTHROUGH;
522530
default:
523531
diagnose(Tok, tryLoc.isValid() ? diag::expected_expr : diag::expected_stmt);
524532
return nullptr;
@@ -539,18 +547,6 @@ ParserResult<Stmt> Parser::parseStmt() {
539547
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
540548
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
541549
return parseStmtGuard();
542-
case tok::pound_if:
543-
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
544-
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
545-
return parseStmtIfConfig();
546-
case tok::pound_line:
547-
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
548-
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
549-
return parseLineDirective(true);
550-
case tok::pound_sourceLocation:
551-
if (LabelInfo) diagnose(LabelInfo.Loc, diag::invalid_label_on_stmt);
552-
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
553-
return parseLineDirective(false);
554550
case tok::kw_while:
555551
if (tryLoc.isValid()) diagnose(tryLoc, diag::try_on_stmt, Tok.getText());
556552
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

test/Parse/line-directive.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ public struct S { // expected-note{{in declaration of 'S'}}
3131

3232
#sourceLocation()
3333

34+
// expected-error@+1 {{expected expression}}
35+
try #sourceLocation(file: "try.swift", line: 100)
36+
#sourceLocation()
37+
38+
// expected-error@+3 {{expected statement}}
39+
// expected-error@+2 {{#line directive was renamed to #sourceLocation}}
40+
LABEL:
41+
#line 200 "labeled.swift"
42+
#sourceLocation()

0 commit comments

Comments
 (0)