Skip to content

Commit 6e3d9a7

Browse files
authored
Merge pull request #39510 from hamishknight/no-label-yield
2 parents c2a9272 + e3e9ef9 commit 6e3d9a7

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,8 @@ ERROR(expected_await_not_async,none,
10301030
// Yield Statment
10311031
ERROR(expected_expr_yield,PointsToFirstBadToken,
10321032
"expected expression in 'yield' statement", ())
1033+
ERROR(unexpected_label_yield,none,
1034+
"unexpected label in 'yield' statement", ())
10331035

10341036
// Defer Statement
10351037
ERROR(expected_lbrace_after_defer,PointsToFirstBadToken,

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,8 +3132,7 @@ ParserStatus Parser::parseExprList(tok leftTok, tok rightTok,
31323132
Kind, [&] () -> ParserStatus {
31333133
Identifier FieldName;
31343134
SourceLoc FieldNameLoc;
3135-
if (Kind != SyntaxKind::YieldStmt)
3136-
parseOptionalArgumentLabel(FieldName, FieldNameLoc);
3135+
parseOptionalArgumentLabel(FieldName, FieldNameLoc);
31373136

31383137
// See if we have an operator decl ref '(<op>)'. The operator token in
31393138
// this case lexes as a binary operator because it neither leads nor

lib/Parse/ParseStmt.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,11 @@ ParserResult<Stmt> Parser::parseStmtYield(SourceLoc tryLoc) {
844844
status = parseExprList(tok::l_paren, tok::r_paren, /*isArgumentList*/ false,
845845
lpLoc, yieldElts, rpLoc, SyntaxKind::ExprList);
846846
for (auto &elt : yieldElts) {
847-
assert(elt.Label.empty());
848-
assert(elt.LabelLoc.isInvalid());
847+
// We don't accept labels in a list of yields.
848+
if (elt.LabelLoc.isValid()) {
849+
diagnose(elt.LabelLoc, diag::unexpected_label_yield)
850+
.fixItRemoveChars(elt.LabelLoc, elt.E->getStartLoc());
851+
}
849852
yields.push_back(elt.E);
850853
}
851854
} else {

test/stmt/yield.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,16 @@ struct YieldInDefer {
9292
}
9393
}
9494
}
95+
96+
// SR-15066
97+
struct InvalidYieldParsing {
98+
var property: String {
99+
_read {
100+
yield(x: "test") // expected-error {{unexpected label in 'yield' statement}} {{13-16=}}
101+
yield(x: "test", y: {0}) // expected-error {{expected 1 yield value(s)}}
102+
// expected-error@-1 {{unexpected label in 'yield' statement}} {{13-16=}}
103+
// expected-error@-2 {{unexpected label in 'yield' statement}} {{24-29=}}
104+
yield(_: "test") // expected-error {{unexpected label in 'yield' statement}} {{13-16=}}
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)