Skip to content

Commit 939d4d7

Browse files
committedFeb 26, 2025
[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.

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
 

‎lib/Parse/ParseExpr.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,9 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
436436
return sub;
437437
}
438438

439-
if (Tok.isContextualKeyword("unsafe")) {
439+
if (Tok.isContextualKeyword("unsafe") &&
440+
!peekToken().isAtStartOfLine() &&
441+
!peekToken().is(tok::r_paren)) {
440442
Tok.setKind(tok::contextual_keyword);
441443
SourceLoc unsafeLoc = consumeToken();
442444
ParserResult<Expr> sub =

‎lib/Parse/ParseStmt.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,8 @@ ParserResult<Stmt> Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) {
23792379
}
23802380
}
23812381

2382-
if (Tok.isContextualKeyword("unsafe")) {
2382+
if (Tok.isContextualKeyword("unsafe") &&
2383+
!peekToken().isAny(tok::colon, tok::kw_in)) {
23832384
UnsafeLoc = consumeToken();
23842385
}
23852386

‎test/Unsafe/safe.swift

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ func testUnsafeAsSequenceForEach() {
9898
for unsafe _ in unsafe uas { } // okay
9999
}
100100

101+
func testForInUnsafeAmbiguity(_ integers: [Int]) {
102+
for unsafe in integers {
103+
_ = unsafe
104+
}
105+
for unsafe: Int in integers {
106+
_ = unsafe
107+
}
108+
}
109+
101110
struct UnsafeIterator: @unsafe IteratorProtocol {
102111
@unsafe mutating func next() -> Int? { nil }
103112
}

0 commit comments

Comments
 (0)
Please sign in to comment.