Skip to content

Add missing switch & for-each expression and while & if condition error #1429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2174,9 +2174,24 @@ extension Parser {
ifHandle: RecoveryConsumptionHandle
) -> RawIfExprSyntax {
let (unexpectedBeforeIfKeyword, ifKeyword) = self.eat(ifHandle)
// A scope encloses the condition and true branch for any variables bound
// by a conditional binding. The else branch does *not* see these variables.
let conditions = self.parseConditionList()

let conditions: RawConditionElementListSyntax

if self.at(.leftBrace) {
conditions = RawConditionElementListSyntax(
elements: [
RawConditionElementSyntax(
condition: .expression(RawExprSyntax(RawMissingExprSyntax(arena: self.arena))),
trailingComma: nil,
arena: self.arena
)
],
arena: self.arena
)
} else {
conditions = self.parseConditionList()
}

let body = self.parseCodeBlock(introducer: ifKeyword)

// The else branch, if any, is outside of the scope of the condition.
Expand Down Expand Up @@ -2222,7 +2237,16 @@ extension Parser {
) -> RawSwitchExprSyntax {
let (unexpectedBeforeSwitchKeyword, switchKeyword) = self.eat(switchHandle)

let subject = self.parseExpression(.basic)
// If there is no expression, like `switch { default: return false }` then left brace would parsed as
// a `RawClosureExprSyntax` in the condition, which is most likely not what the user meant.
// Create a missing condition instead and use the `{` for the start of the body.
let subject: RawExprSyntax
if self.at(.leftBrace) {
subject = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
} else {
subject = self.parseExpression(.basic)
}

let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)

let cases = self.parseSwitchCases(allowStandaloneStmtRecovery: !lbrace.isMissing)
Expand Down
27 changes: 25 additions & 2 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,22 @@ extension Parser {
@_spi(RawSyntax)
public mutating func parseWhileStatement(whileHandle: RecoveryConsumptionHandle) -> RawWhileStmtSyntax {
let (unexpectedBeforeWhileKeyword, whileKeyword) = self.eat(whileHandle)
let conditions = self.parseConditionList()
let conditions: RawConditionElementListSyntax

if self.at(.leftBrace) {
conditions = RawConditionElementListSyntax(
elements: [
RawConditionElementSyntax(
condition: .expression(RawExprSyntax(RawMissingExprSyntax(arena: self.arena))),
trailingComma: nil,
arena: self.arena
)
],
arena: self.arena
)
} else {
conditions = self.parseConditionList()
}
let body = self.parseCodeBlock(introducer: whileKeyword)
return RawWhileStmtSyntax(
unexpectedBeforeWhileKeyword,
Expand Down Expand Up @@ -616,7 +631,15 @@ extension Parser {

let (unexpectedBeforeInKeyword, inKeyword) = self.expect(.keyword(.in))

let expr = self.parseExpression(.basic)
// If there is no expression, like `switch { default: return false }` then left brace would parsed as
// a `RawClosureExprSyntax` in the condition, which is most likely not what the user meant.
// Create a missing condition instead and use the `{` for the start of the body.
let expr: RawExprSyntax
if self.at(.leftBrace) {
expr = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
} else {
expr = self.parseExpression(.basic)
}

// Parse the 'where' expression if present.
let whereClause: RawWhereClauseSyntax?
Expand Down
41 changes: 41 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,12 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
] as [Syntax?]).compactMap({ $0 }),
handledNodes: [node.inKeyword.id, node.sequenceExpr.id, unexpectedCondition.id]
)
} else { // If it's not a C-style for loop
if node.sequenceExpr.is(MissingExprSyntax.self) {
addDiagnostic(node.sequenceExpr, .expectedSequenceExpressionInForEachLoop, handledNodes: [node.sequenceExpr.id])
}
}

return .visitChildren
}

Expand Down Expand Up @@ -750,6 +755,18 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: IfExprSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

if node.conditions.count == 1, node.conditions.first?.as(ConditionElementSyntax.self)?.condition.is(MissingExprSyntax.self) == true, !node.body.leftBrace.isMissingAllTokens {
addDiagnostic(node.conditions, MissingConditionInStatement(node: node), handledNodes: [node.conditions.id])
}

return .visitChildren
}

public override func visit(_ node: InitializerClauseSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down Expand Up @@ -1043,6 +1060,18 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: SwitchExprSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

if node.expression.is(MissingExprSyntax.self) && !node.cases.isEmpty {
addDiagnostic(node.expression, MissingExpressionInStatement(node: node), handledNodes: [node.expression.id])
}

return .visitChildren
}

public override func visit(_ node: SwitchCaseSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down Expand Up @@ -1191,6 +1220,18 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: WhileStmtSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}

if node.conditions.count == 1, node.conditions.first?.as(ConditionElementSyntax.self)?.condition.is(MissingExprSyntax.self) == true, !node.body.leftBrace.isMissingAllTokens {
addDiagnostic(node.conditions, MissingConditionInStatement(node: node), handledNodes: [node.conditions.id])
}

return .visitChildren
}

//==========================================================================//
// IMPORTANT: If you are tempted to add a `visit` method here, please //
// insert it in alphabetical order above //
Expand Down
27 changes: 27 additions & 0 deletions Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ extension DiagnosticMessage where Self == StaticParserError {
public static var expectedExpressionAfterTry: Self {
.init("expected expression after 'try'")
}
public static var expectedSequenceExpressionInForEachLoop: Self {
.init("expected Sequence expression for for-each loop")
}
public static var initializerInPattern: Self {
.init("unexpected initializer in pattern; did you mean to use '='?")
}
Expand Down Expand Up @@ -330,6 +333,30 @@ public struct MissingAttributeArgument: ParserError {
}
}

public struct MissingConditionInStatement: ParserError {
let node: SyntaxProtocol

public var message: String {
if let name = node.nodeTypeNameForDiagnostics(allowBlockNames: false) {
return "missing condition in \(name)"
} else {
return "missing condition in statement"
}
}
}

public struct MissingExpressionInStatement: ParserError {
let node: SyntaxProtocol

public var message: String {
if let name = node.nodeTypeNameForDiagnostics(allowBlockNames: false) {
return "expected expression in \(name)"
} else {
return "expected expression in statement"
}
}
}

public struct NegatedAvailabilityCondition: ParserError {
public let avaialabilityCondition: AvailabilityConditionSyntax
public let negatedAvailabilityKeyword: TokenSyntax
Expand Down
Loading