Skip to content

Commit be7e875

Browse files
committed
[SE-0458] Improved disambiguation for unsafe expressions
Disambiguate `unsafe` in a few more common contexts: * Before a comma in a list of whatever form * Before a left brace somewhere that we cannot have a closure Fixes a few more source compatibility regressions found in the wild, rdar://146125433.
1 parent 6da529f commit be7e875

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/Parse/ParseExpr.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
439439
if (Tok.isContextualKeyword("unsafe") &&
440440
!(peekToken().isAtStartOfLine() ||
441441
peekToken().isAny(tok::r_paren, tok::r_brace, tok::r_square,
442-
tok::equal) ||
442+
tok::equal, tok::colon, tok::comma) ||
443+
(isExprBasic && peekToken().is(tok::l_brace)) ||
443444
peekToken().is(tok::period))) {
444445
Tok.setKind(tok::contextual_keyword);
445446
SourceLoc unsafeLoc = consumeToken();

test/Unsafe/safe.swift

+8
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,25 @@ enum Color {
179179
case red
180180
}
181181

182+
func acceptBools(_: Bool, _: Bool) { }
183+
184+
func acceptBoolsUnsafeLabel(unsafe _: Bool, _: Bool) { }
185+
182186
func unsafeFun() {
183187
var unsafe = true
184188
unsafe = false
185189
unsafe.toggle()
186190
_ = [unsafe]
187191
_ = { unsafe }
192+
acceptBools(unsafe, unsafe)
193+
acceptBoolsUnsafeLabel(unsafe: unsafe, unsafe)
188194

189195
let color: Color
190196
// expected-warning@+1{{no unsafe operations occur within 'unsafe' expression}}
191197
color = unsafe .red
192198
_ = color
199+
200+
if unsafe { }
193201
}
194202

195203
// @safe suppresses unsafe-type-related diagnostics on an entity

0 commit comments

Comments
 (0)