Skip to content

Commit d12cb84

Browse files
authored
[Parse] Avoid parsing unsafe expression before binary or postfix op (#81429)
rdar://150751248
1 parent 7ff1066 commit d12cb84

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,14 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
421421

422422
if (Tok.isContextualKeyword("unsafe") &&
423423
!(peekToken().isAtStartOfLine() ||
424-
peekToken().isAny(tok::r_paren, tok::r_brace, tok::r_square,
425-
tok::equal, tok::colon, tok::comma, tok::eof) ||
424+
peekToken().isAny(tok::r_paren, tok::r_brace, tok::r_square, tok::equal,
425+
tok::colon, tok::comma, tok::eof) ||
426426
(isExprBasic && peekToken().is(tok::l_brace)) ||
427427
peekToken().is(tok::period) ||
428428
(peekToken().isAny(tok::l_paren, tok::l_square) &&
429-
peekToken().getRange().getStart() == Tok.getRange().getEnd()))) {
429+
peekToken().getRange().getStart() == Tok.getRange().getEnd()) ||
430+
peekToken().isBinaryOperatorLike() ||
431+
peekToken().isPostfixOperatorLike())) {
430432
Tok.setKind(tok::contextual_keyword);
431433
SourceLoc unsafeLoc = consumeToken();
432434
ParserResult<Expr> sub =

test/Unsafe/safe.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ func unsafeFun() {
200200
_ = color
201201

202202
if unsafe { }
203+
204+
_ = unsafe ? 1 : 0
203205
}
204206

205207
func moreUnsafeFunc(unsafe: [Int]) {
@@ -218,6 +220,13 @@ func yetMoreUnsafeFunc(unsafe: () -> Void) {
218220
// expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}}
219221
}
220222

223+
func yetMoreMoreUnsafeFunc(unsafe: Int?) {
224+
_ = unsafe!
225+
if let unsafe {
226+
_ = unsafe + 1
227+
}
228+
}
229+
221230
// @safe suppresses unsafe-type-related diagnostics on an entity
222231
struct MyArray<Element> {
223232
@safe func withUnsafeBufferPointer<R, E>(

0 commit comments

Comments
 (0)