Skip to content

Commit 6cf56e9

Browse files
committed
SE-0458: Disambiguate postfix expressions vs. unsafe expressions more generally
Handle call-vs-tuple and subscript-vs-collection-expr disambiguation using the same "no trivia" rule that we used to disambiguite "unsafe.x" (which we treat as a member access) from "unsafe .x" (which we treat as an unsafe expression with a leading-dot member access). Fixes rdar://146459104.
1 parent 80050bb commit 6cf56e9

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
441441
peekToken().isAny(tok::r_paren, tok::r_brace, tok::r_square,
442442
tok::equal, tok::colon, tok::comma) ||
443443
(isExprBasic && peekToken().is(tok::l_brace)) ||
444-
peekToken().is(tok::period))) {
444+
peekToken().is(tok::period) ||
445+
(peekToken().isAny(tok::l_paren, tok::l_square) &&
446+
peekToken().getRange().getStart() == Tok.getRange().getEnd()))) {
445447
Tok.setKind(tok::contextual_keyword);
446448
SourceLoc unsafeLoc = consumeToken();
447449
ParserResult<Expr> sub =

test/Unsafe/safe.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ func acceptBools(_: Bool, _: Bool) { }
183183

184184
func acceptBoolsUnsafeLabel(unsafe _: Bool, _: Bool) { }
185185

186+
func unsafe(_: Int) { }
187+
186188
func unsafeFun() {
187189
var unsafe = true
188190
unsafe = false
@@ -200,6 +202,20 @@ func unsafeFun() {
200202
if unsafe { }
201203
}
202204

205+
func moreUnsafeFunc(unsafe: [Int]) {
206+
let _: [Int] = unsafe []
207+
// expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}}
208+
209+
_ = unsafe[1]
210+
}
211+
212+
func yetMoreUnsafeFunc(unsafe: () -> Void) {
213+
unsafe()
214+
215+
_ = unsafe ()
216+
// expected-warning@-1{{no unsafe operations occur within 'unsafe' expression}}
217+
}
218+
203219
// @safe suppresses unsafe-type-related diagnostics on an entity
204220
struct MyArray<Element> {
205221
@safe func withUnsafeBufferPointer<R, E>(

0 commit comments

Comments
 (0)