diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index a87247d5fc333..6acbde2a188c4 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -439,9 +439,11 @@ ParserResult Parser::parseExprSequenceElement(Diag<> message, if (Tok.isContextualKeyword("unsafe") && !(peekToken().isAtStartOfLine() || peekToken().isAny(tok::r_paren, tok::r_brace, tok::r_square, - tok::equal, tok::colon, tok::comma) || + tok::equal, tok::colon, tok::comma, tok::eof) || (isExprBasic && peekToken().is(tok::l_brace)) || - peekToken().is(tok::period))) { + peekToken().is(tok::period) || + (peekToken().isAny(tok::l_paren, tok::l_square) && + peekToken().getRange().getStart() == Tok.getRange().getEnd()))) { Tok.setKind(tok::contextual_keyword); SourceLoc unsafeLoc = consumeToken(); ParserResult sub = diff --git a/test/Unsafe/safe.swift b/test/Unsafe/safe.swift index 299e8e199dd37..90271a9bca1b6 100644 --- a/test/Unsafe/safe.swift +++ b/test/Unsafe/safe.swift @@ -183,6 +183,8 @@ func acceptBools(_: Bool, _: Bool) { } func acceptBoolsUnsafeLabel(unsafe _: Bool, _: Bool) { } +func unsafe(_: Int) { } + func unsafeFun() { var unsafe = true unsafe = false @@ -200,6 +202,22 @@ func unsafeFun() { if unsafe { } } +func moreUnsafeFunc(unsafe: [Int]) { + let _: [Int] = unsafe [] + // expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}} + + _ = unsafe[1] + + _ = "\(unsafe)" +} + +func yetMoreUnsafeFunc(unsafe: () -> Void) { + unsafe() + + _ = unsafe () + // expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}} +} + // @safe suppresses unsafe-type-related diagnostics on an entity struct MyArray { @safe func withUnsafeBufferPointer(