@@ -912,12 +912,13 @@ extension TokenConsumer {
912912 mutating func atContextualKeywordPrefixedSyntax(
913913 exprFlavor: Parser . ExprFlavor ,
914914 acceptClosure: Bool = false ,
915- preferPostfixExpr: Bool = true
915+ preferPostfixExpr: Bool = true ,
916+ allowNextLineOperand: Bool = false
916917 ) -> Bool {
917918 let next = peek ( )
918919
919920 // The next token must be at the same line.
920- if next. isAtStartOfLine {
921+ if next. isAtStartOfLine && !allowNextLineOperand {
921922 return false
922923 }
923924
@@ -990,26 +991,28 @@ extension TokenConsumer {
990991 // - Call vs. tuple expression
991992 // - Subscript vs. collection literal
992993 //
993- let hasSpace = ( next. leadingTriviaByteLength + currentToken. trailingTriviaByteLength) != 0
994- if !hasSpace {
995- // No space, the word is an decl-ref expression
994+ if preferPostfixExpr {
996995 return false
997996 }
998- return !preferPostfixExpr
997+
998+ // If there's no space between the tokens, consider it's an expression.
999+ // Otherwise, it looks like a keyword followed by an expression.
1000+ return ( next. leadingTriviaByteLength + currentToken. trailingTriviaByteLength) != 0
9991001
10001002 case . leftBrace:
10011003 // E.g. <word> { ... }
10021004 // Trailing closure is also ambiguous:
10031005 //
10041006 // - Trailing closure vs. immediately-invoked closure
10051007 //
1006- // Checking whitespace between the word cannot help this because people
1007- // usually put a space before trailing closures. Even though that is source
1008- // breaking, we prefer parsing it as a keyword if the syntax accepts
1009- // immediately-invoked closure patterns. E.g. 'unsafe { ... }()'
10101008 if !acceptClosure {
10111009 return false
10121010 }
1011+
1012+ // Checking whitespace between the word cannot help this because people
1013+ // usually put a space before trailing closures. Even though that is source
1014+ // breaking, we prefer parsing it as a keyword if the syntax accepts
1015+ // expressions starting with a closure. E.g. 'unsafe { ... }()'
10131016 return self . withLookahead {
10141017 $0. consumeAnyToken ( )
10151018 return $0. atValidTrailingClosure ( flavor: exprFlavor)
@@ -1070,7 +1073,7 @@ extension Parser.Lookahead {
10701073 case . yield? , . discard? :
10711074 return atContextualKeywordPrefixedSyntax ( exprFlavor: . basic, preferPostfixExpr: true )
10721075 case . then? :
1073- return atContextualKeywordPrefixedSyntax ( exprFlavor: . basic, preferPostfixExpr: false )
1076+ return atContextualKeywordPrefixedSyntax ( exprFlavor: . basic, preferPostfixExpr: false , allowNextLineOperand : !preferExpr )
10741077
10751078 case nil :
10761079 return false
0 commit comments