File tree Expand file tree Collapse file tree 3 files changed +33
-10
lines changed Expand file tree Collapse file tree 3 files changed +33
-10
lines changed Original file line number Diff line number Diff line change @@ -120,20 +120,33 @@ extension Parser {
120
120
/// =======
121
121
///
122
122
/// typed-pattern → pattern ':' attributes? inout? type
123
- mutating func parseTypedPattern( ) -> ( RawPatternSyntax , RawTypeAnnotationSyntax ? ) {
123
+ mutating func parseTypedPattern( allowRecoveryFromMissingColon : Bool = true ) -> ( RawPatternSyntax , RawTypeAnnotationSyntax ? ) {
124
124
let pattern = self . parsePattern ( )
125
125
126
126
// Now parse an optional type annotation.
127
- guard let colon = self . consume ( if: . colon) else {
128
- return ( pattern, nil )
127
+ let colon = self . consume ( if: . colon)
128
+ var lookahead = self . lookahead ( )
129
+ var type : RawTypeAnnotationSyntax ?
130
+ if let colon = colon {
131
+ let result = self . parseResultType ( )
132
+ type = RawTypeAnnotationSyntax (
133
+ colon: colon,
134
+ type: result,
135
+ arena: self . arena
136
+ )
137
+ } else if allowRecoveryFromMissingColon
138
+ && !self . currentToken. isAtStartOfLine
139
+ && lookahead. canParseType ( ) {
140
+ // Recovery if the user forgot to add ':'
141
+ let result = self . parseResultType ( )
142
+ type = RawTypeAnnotationSyntax (
143
+ colon: self . missingToken ( . colon, text: nil ) ,
144
+ type: result,
145
+ arena: self . arena
146
+ )
129
147
}
130
148
131
- let result = self . parseResultType ( )
132
- let type = RawTypeAnnotationSyntax (
133
- colon: colon,
134
- type: result,
135
- arena: self . arena
136
- )
149
+
137
150
return ( pattern, type)
138
151
}
139
152
Original file line number Diff line number Diff line change @@ -580,7 +580,7 @@ extension Parser {
580
580
type = nil
581
581
}
582
582
} else {
583
- ( pattern, type) = self . parseTypedPattern ( )
583
+ ( pattern, type) = self . parseTypedPattern ( allowRecoveryFromMissingColon : false )
584
584
}
585
585
586
586
let ( unexpectedBeforeInKeyword, inKeyword) = self . expect ( . inKeyword)
Original file line number Diff line number Diff line change 3
3
import XCTest
4
4
5
5
final class TypeTests : XCTestCase {
6
+
7
+ func testMissingColonInType( ) {
8
+ AssertParse (
9
+ """
10
+ var foo 1️⃣Bar = 1
11
+ """ , diagnostics: [
12
+ DiagnosticSpec ( message: " expected ':' in type annotation " )
13
+ ] )
14
+ }
15
+
6
16
func testClosureParsing( ) throws {
7
17
AssertParse (
8
18
" (a, b) -> c " ,
You can’t perform that action at this time.
0 commit comments