Skip to content

Add diagnostic for missing 0 in RawFloatLiteralExprSyntax #1592

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
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
25 changes: 25 additions & 0 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,31 @@ extension Parser {
return RawExprSyntax(self.parseClosureExpression())
case (.period, let handle)?: // .foo
let dot = self.eat(handle)

// Special case ".<integer_literal>" like ".4". This isn't valid, but the
// developer almost certainly meant to use "0.4". Diagnose this, and
// recover as if they wrote that.
if let integerLiteral = self.consume(if: .integerLiteral) {
let text = arena.intern("0" + String(syntaxText: dot.tokenText) + String(syntaxText: integerLiteral.tokenText))
return RawExprSyntax(
RawFloatLiteralExprSyntax(
floatingDigits: RawTokenSyntax(
missing: .floatingLiteral,
text: text,
arena: self.arena
),
RawUnexpectedNodesSyntax(
elements: [
RawSyntax(dot),
RawSyntax(integerLiteral),
],
arena: self.arena
),
arena: self.arena
)
)
}

let (name, args) = self.parseDeclNameRef([.keywords, .compoundNames])
return RawExprSyntax(
RawMemberAccessExprSyntax(
Expand Down
26 changes: 26 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,32 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: FloatLiteralExprSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}
if node.floatingDigits.presence == .missing,
let (period, integerLiteral) = node.unexpectedAfterFloatingDigits?.twoTokens(firstSatisfying: { $0.tokenKind == .period }, secondSatisfying: { $0.tokenKind.isIntegerLiteral })
{
addDiagnostic(
node,
InvalidFloatLiteralMissingLeadingZero(decimalDigits: integerLiteral),
fixIts: [
FixIt(
message: InsertFixIt(tokenToBeInserted: .integerLiteral("0")),
changes: [
.makePresent(node.floatingDigits),
.makeMissing(period),
.makeMissing(integerLiteral),
]
)
],
handledNodes: [node.floatingDigits.id, period.id, integerLiteral.id]
)
}
return .visitChildren
}

public override func visit(_ node: ForInStmtSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ public struct IdentifierNotAllowedInOperatorName: ParserError {
}
}

public struct InvalidFloatLiteralMissingLeadingZero: ParserError {
public let decimalDigits: TokenSyntax

public var message: String {
return "'.\(decimalDigits.text)' is not a valid floating point literal; it must be written '0.\(decimalDigits.text)'"
}
}

public struct InvalidIdentifierError: ParserError {
public let invalidIdentifier: TokenSyntax
public let missingIdentifier: TokenSyntax
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftParserDiagnostics/SyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ extension TokenKind {
return false
}
}

var isIntegerLiteral: Bool {
switch self {
case .integerLiteral:
return true
default:
return false
}
}
}

public extension TriviaPiece {
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftSyntax/SyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public class SyntaxArena {

/// Copies a UTF8 sequence of `String` to the memory this arena manages, and
/// returns the copied string as a `SyntaxText`
func intern(_ value: String) -> SyntaxText {
@_spi(RawSyntax)
public func intern(_ value: String) -> SyntaxText {
if value.isEmpty { return SyntaxText() }
var value = value
return value.withUTF8 { utf8 in
Expand Down
10 changes: 6 additions & 4 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1658,16 +1658,18 @@ final class RecoveryTests: XCTestCase {
assertParse(
"""
func exprPostfix2() {
_ = .1️⃣42
_ = 1️⃣.42
}
""",
diagnostics: [
// TODO: Old parser expected error on line 2: '.42' is not a valid floating point literal; it must be written '0.42', Fix-It replacements: 7 - 7 = '0'
DiagnosticSpec(message: "expected name in member access", fixIts: ["insert name"])
DiagnosticSpec(
message: "'.42' is not a valid floating point literal; it must be written '0.42'",
fixIts: ["insert '0'"]
)
],
fixedSource: """
func exprPostfix2() {
_ = .<#identifier#>42
_ = 0.42
}
"""
)
Expand Down