[SE-0458] Disambiguate "unsafe" expression and for..in effect

Port over the disambiguation code we already had in the new Swift parser
to the existing C++ parser for the "unsafe" expression and for..in effect.
This commit is contained in:
Doug Gregor
2025-02-26 13:03:55 -08:00
parent b7b5a2a19d
commit 939d4d7d91
3 changed files with 14 additions and 2 deletions

View File

@@ -436,7 +436,9 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
return sub;
}
if (Tok.isContextualKeyword("unsafe")) {
if (Tok.isContextualKeyword("unsafe") &&
!peekToken().isAtStartOfLine() &&
!peekToken().is(tok::r_paren)) {
Tok.setKind(tok::contextual_keyword);
SourceLoc unsafeLoc = consumeToken();
ParserResult<Expr> sub =

View File

@@ -2379,7 +2379,8 @@ ParserResult<Stmt> Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) {
}
}
if (Tok.isContextualKeyword("unsafe")) {
if (Tok.isContextualKeyword("unsafe") &&
!peekToken().isAny(tok::colon, tok::kw_in)) {
UnsafeLoc = consumeToken();
}

View File

@@ -98,6 +98,15 @@ func testUnsafeAsSequenceForEach() {
for unsafe _ in unsafe uas { } // okay
}
func testForInUnsafeAmbiguity(_ integers: [Int]) {
for unsafe in integers {
_ = unsafe
}
for unsafe: Int in integers {
_ = unsafe
}
}
struct UnsafeIterator: @unsafe IteratorProtocol {
@unsafe mutating func next() -> Int? { nil }
}