File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -455,8 +455,13 @@ extension Parser {
455
455
// Start of a closure in a context where it should be interpreted as
456
456
// being part of a statement.
457
457
|| ( flavor == . stmtCondition && self . peek ( isAt: . leftBrace) )
458
- // `unsafe.something` with no trivia
459
- || ( self . peek ( isAt: . period) && self . peek ( ) . leadingTriviaByteLength == 0
458
+ // Avoid treating as an "unsafe" expression when there is no trivia
459
+ // following the "unsafe" and the following token could either be a
460
+ // postfix expression or a subexpression:
461
+ // - Member access vs. leading .
462
+ // - Call vs. tuple expression.
463
+ // - Subscript vs. array or dictionary expression
464
+ || ( self . peek ( isAt: . period, . leftParen, . leftSquare) && self . peek ( ) . leadingTriviaByteLength == 0
460
465
&& self . currentToken. trailingTriviaByteLength == 0 )
461
466
{
462
467
break EXPR_PREFIX
Original file line number Diff line number Diff line change @@ -2295,6 +2295,46 @@ final class StatementExpressionTests: ParserTestCase {
2295
2295
""" ,
2296
2296
substructure: DeclReferenceExprSyntax ( baseName: . identifier( " unsafe " ) )
2297
2297
)
2298
+
2299
+ assertParse (
2300
+ """
2301
+ func f() {
2302
+ unsafe()
2303
+ }
2304
+ """ ,
2305
+ substructure: DeclReferenceExprSyntax ( baseName: . identifier( " unsafe " ) )
2306
+ )
2307
+
2308
+ assertParse (
2309
+ """
2310
+ func f() {
2311
+ unsafe ()
2312
+ }
2313
+ """ ,
2314
+ substructure: UnsafeExprSyntax (
2315
+ expression: TupleExprSyntax ( elements: LabeledExprListSyntax ( ) )
2316
+ )
2317
+ )
2318
+
2319
+ assertParse (
2320
+ """
2321
+ func f() {
2322
+ unsafe[]
2323
+ }
2324
+ """ ,
2325
+ substructure: DeclReferenceExprSyntax ( baseName: . identifier( " unsafe " ) )
2326
+ )
2327
+
2328
+ assertParse (
2329
+ """
2330
+ func f() {
2331
+ unsafe []
2332
+ }
2333
+ """ ,
2334
+ substructure: UnsafeExprSyntax (
2335
+ expression: ArrayExprSyntax ( expressions: [ ] )
2336
+ )
2337
+ )
2298
2338
}
2299
2339
2300
2340
func testUnterminatedInterpolationAtEndOfMultilineStringLiteral( ) {
You can’t perform that action at this time.
0 commit comments