Skip to content

Commit e0533a8

Browse files
committed
improve diagnostic for unnamed closure parameters
- instead of treating as unrecognized, try to parse the remaining tokens as a type even if the preceding colon is missing
1 parent 050f057 commit e0533a8

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

Sources/SwiftParser/Parameters.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,20 @@ extension Parser {
160160
let misplacedSpecifiers = parseMisplacedSpecifiers()
161161

162162
let names = self.parseParameterNames()
163-
let colon = self.consume(if: .colon)
164-
let type: RawTypeSyntax?
165-
if colon != nil {
166-
type = self.parseType(misplacedSpecifiers: misplacedSpecifiers)
167-
} else {
163+
var colon = self.consume(if: .colon)
164+
// try to parse the type regardless of the presence of the preceding colon
165+
// because the parameter may be unnamed
166+
// e.g. { [X] in } or { (:[X]) in }
167+
var type: RawTypeSyntax? = self.parseType(misplacedSpecifiers: misplacedSpecifiers)
168+
if type?.raw.kind != .missingType {
169+
if colon == nil {
170+
// mark the preceding colon as missing if the parameter is unnamed
171+
// e.g. { [X] in }
172+
colon = missingToken(.colon)
173+
}
174+
} else if colon == nil {
175+
// don't mark the type as missing if the preceding colon is absent
176+
// e.g. { _ in }
168177
type = nil
169178
}
170179

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,4 +3355,19 @@ final class RecoveryTests: ParserTestCase {
33553355
]
33563356
)
33573357
}
3358+
3359+
func testRecovery185() {
3360+
// <rdar://1181099123>
3361+
assertParse(
3362+
"""
3363+
test { (1️⃣[X]) in }
3364+
""",
3365+
diagnostics: [
3366+
DiagnosticSpec(message: "expected identifier and ':' in parameter", fixIts: ["insert identifier and ':'"]),
3367+
],
3368+
fixedSource: """
3369+
test { (<#identifier#>: [X]) in }
3370+
"""
3371+
)
3372+
}
33583373
}

0 commit comments

Comments
 (0)