Skip to content

Commit 70390e2

Browse files
authored
Merge pull request #79710 from DougGregor/disambiguate-unsafe-better
More disambiguation for "unsafe" expressions
2 parents 1405520 + a1180e5 commit 70390e2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,10 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
437437
}
438438

439439
if (Tok.isContextualKeyword("unsafe") &&
440-
!peekToken().isAtStartOfLine() &&
441-
!peekToken().is(tok::r_paren)) {
440+
!(peekToken().isAtStartOfLine() ||
441+
peekToken().isAny(tok::r_paren, tok::r_brace, tok::r_square,
442+
tok::equal) ||
443+
peekToken().is(tok::period))) {
442444
Tok.setKind(tok::contextual_keyword);
443445
SourceLoc unsafeLoc = consumeToken();
444446
ParserResult<Expr> sub =

test/Unsafe/safe.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ func testUnsafePositionError() -> Int {
175175
return 3 + unsafe unsafeInt() // expected-error{{'unsafe' cannot appear to the right of a non-assignment operator}}
176176
}
177177

178+
enum Color {
179+
case red
180+
}
181+
182+
func unsafeFun() {
183+
var unsafe = true
184+
unsafe = false
185+
unsafe.toggle()
186+
_ = [unsafe]
187+
_ = { unsafe }
188+
189+
let color: Color
190+
// expected-warning@+1{{no unsafe operations occur within 'unsafe' expression}}
191+
color = unsafe .red
192+
_ = color
193+
}
194+
178195
// @safe suppresses unsafe-type-related diagnostics on an entity
179196
struct MyArray<Element> {
180197
@safe func withUnsafeBufferPointer<R, E>(

0 commit comments

Comments
 (0)