@@ -186,7 +186,7 @@ extension Parser {
186
186
var keepGoing : RawTokenSyntax ? = nil
187
187
var loopProgress = LoopProgressCondition ( )
188
188
repeat {
189
- let condition = self . parseConditionElement ( )
189
+ let condition = self . parseConditionElement ( lastBindingKind : elements . last ? . condition . as ( RawOptionalBindingConditionSyntax . self ) ? . bindingKeyword )
190
190
let unexpectedBeforeKeepGoing : RawUnexpectedNodesSyntax ?
191
191
keepGoing = self . consume ( if: . comma)
192
192
if keepGoing == nil , let andOperator = self . consumeIfContextualPunctuator ( " && " ) {
@@ -219,15 +219,15 @@ extension Parser {
219
219
/// optional-binding-condition → 'let' pattern initializer? | 'var' pattern initializer? |
220
220
/// 'inout' pattern initializer?
221
221
@_spi ( RawSyntax)
222
- public mutating func parseConditionElement( ) -> RawConditionElementSyntax . Condition {
222
+ public mutating func parseConditionElement( lastBindingKind : RawTokenSyntax ? ) -> RawConditionElementSyntax . Condition {
223
223
// Parse a leading #available/#unavailable condition if present.
224
224
if self . at ( . poundAvailableKeyword, . poundUnavailableKeyword) {
225
225
return self . parsePoundAvailableConditionElement ( )
226
226
}
227
227
228
228
// Parse the basic expression case. If we have a leading let, var, inout,
229
229
// borrow, case keyword or an assignment, then we know this is a binding.
230
- guard self . at ( . keyword( . let) , . keyword( . var) , . keyword( . case) ) || self . at ( . keyword( . inout) ) else {
230
+ guard self . at ( . keyword( . let) , . keyword( . var) , . keyword( . case) ) || self . at ( . keyword( . inout) ) || ( lastBindingKind != nil && self . peek ( ) . rawTokenKind == . equal ) else {
231
231
// If we lack it, then this is theoretically a boolean condition.
232
232
// However, we also need to handle migrating from Swift 2 syntax, in
233
233
// which a comma followed by an expression could actually be a pattern
@@ -244,20 +244,20 @@ extension Parser {
244
244
}
245
245
246
246
// We're parsing a conditional binding.
247
- precondition ( self . at ( . keyword( . let) , . keyword( . var) ) || self . at ( . keyword( . inout) , . keyword( . case) ) )
248
247
enum BindingKind {
249
248
case pattern( RawTokenSyntax , RawPatternSyntax )
250
- case optional( RawTokenSyntax , RawPatternSyntax )
249
+ case optional( RawUnexpectedNodesSyntax ? , RawTokenSyntax , RawPatternSyntax )
251
250
}
252
251
253
252
let kind : BindingKind
254
253
if let caseKeyword = self . consume ( if: . keyword( . case) ) {
255
254
let pattern = self . parseMatchingPattern ( context: . matching)
256
255
kind = . pattern( caseKeyword, pattern)
257
256
} else {
258
- let letOrVar = self . consumeAnyToken ( )
257
+ let ( unexpectedBeforeBindingKeyword, letOrVar) = self . expect ( . keyword( . let) , . keyword( . var) , default: . keyword( Keyword ( lastBindingKind!. tokenText) !) )
258
+
259
259
let pattern = self . parseMatchingPattern ( context: . bindingIntroducer)
260
- kind = . optional( letOrVar, pattern)
260
+ kind = . optional( unexpectedBeforeBindingKeyword , letOrVar, pattern)
261
261
}
262
262
263
263
// Now parse an optional type annotation.
@@ -289,9 +289,10 @@ extension Parser {
289
289
}
290
290
291
291
switch kind {
292
- case let . optional( bindingKeyword, pattern) :
292
+ case let . optional( unexpectedBeforeBindingKeyword , bindingKeyword, pattern) :
293
293
return . optionalBinding(
294
294
RawOptionalBindingConditionSyntax (
295
+ unexpectedBeforeBindingKeyword,
295
296
bindingKeyword: bindingKeyword,
296
297
pattern: pattern,
297
298
typeAnnotation: annotation,
0 commit comments