Skip to content

Suggest to add missing equal token to variable declarations #1770

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 1 commit into from
Jun 15, 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
8 changes: 8 additions & 0 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,14 @@ extension Parser {
value: initExpr,
arena: self.arena
)
} else if self.atStartOfExpression(), !self.at(.leftBrace), !self.currentToken.flags.contains(.isAtStartOfLine) {
let missingEqual = RawTokenSyntax(missing: .equal, arena: self.arena)
let expr = self.parseExpression()
initializer = RawInitializerClauseSyntax(
equal: missingEqual,
value: expr,
arena: self.arena
)
} else {
initializer = nil
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2530,4 +2530,14 @@ final class DeclarationTests: XCTestCase {
"""
)
}

func testMissingEqualInVariableDeclaration() {
assertParse(
"let foo: [Int] 1️⃣[]",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "expected '=' in variable", fixIts: ["insert '='"])
],
fixedSource: "let foo: [Int] = []"
)
}
}
21 changes: 10 additions & 11 deletions Tests/SwiftParserTest/RegexLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1211,48 +1211,47 @@ final class RegexLiteralTests: XCTestCase {
func testPrefixOpSplitting2a() {
assertParse(
"""
let x1️⃣ .2️⃣/abc/
let x 1️⃣.2️⃣/abc/
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
message: "expected '=' in variable",
fixIts: ["insert '='"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected name in member access",
fixIts: ["insert name"]
),
],
applyFixIts: ["insert newline", "insert name"],
applyFixIts: ["insert '='", "insert name"],
fixedSource: """
let x
.<#identifier#>/abc/
let x = .<#identifier#>/abc/
"""
)
}

func testPrefixOpSplitting2b() {
assertParse(
"""
let x1️⃣ .2️⃣/abc/
let x 1️⃣.2️⃣/abc/
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
message: "expected '=' in variable",
fixIts: ["insert '='"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "expected name in member access",
fixIts: ["insert name"]
),
],
applyFixIts: ["insert ';'", "insert name"],
applyFixIts: ["insert '='", "insert name"],
fixedSource: """
let x; .<#identifier#>/abc/
let x = .<#identifier#>/abc/
"""
)
}
Expand Down
53 changes: 19 additions & 34 deletions Tests/SwiftParserTest/translated/ConsecutiveStatementsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
if i != j { i = j }
// Errors
i = j1️⃣ j = i
let r : Int2️⃣ i = j
let r : Int 2️⃣i = j
let s : Int3️⃣ let t : Int
_ = r; _ = s; _ = t
}
Expand All @@ -83,16 +83,16 @@ final class ConsecutiveStatementsTests: XCTestCase {
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
message: "expected '=' in variable",
fixIts: ["insert '='"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
),
],
applyFixIts: ["insert newline"],
applyFixIts: ["insert newline", "insert '='"],
fixedSource: """
// Within a function
func test(i: inout Int, j: inout Int) {
Expand All @@ -102,8 +102,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
// Errors
i = j
j = i
let r : Int
i = j
let r : Int = i = j
let s : Int
let t : Int
_ = r; _ = s; _ = t
Expand All @@ -122,7 +121,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
if i != j { i = j }
// Errors
i = j1️⃣ j = i
let r : Int2️⃣ i = j
let r : Int 2️⃣i = j
let s : Int3️⃣ let t : Int
_ = r; _ = s; _ = t
}
Expand All @@ -135,8 +134,8 @@ final class ConsecutiveStatementsTests: XCTestCase {
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
message: "expected '=' in variable",
fixIts: ["insert '='"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
Expand All @@ -153,7 +152,7 @@ final class ConsecutiveStatementsTests: XCTestCase {
if i != j { i = j }
// Errors
i = j; j = i
let r : Int; i = j
let r : Int i = j
let s : Int; let t : Int
_ = r; _ = s; _ = t
}
Expand Down Expand Up @@ -468,26 +467,18 @@ final class ConsecutiveStatementsTests: XCTestCase {
assertParse(
"""
// At the top level
var i, j : Int1️⃣ i = j2️⃣ j = i
var i, j : Int 1️⃣i = j2️⃣ j = i
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
),
message: "expected '=' in variable",
fixIts: ["insert '='"]
)
],
applyFixIts: ["insert newline"],
fixedSource: """
// At the top level
var i, j : Int
i = j
j = i
var i, j : Int = i = j j = i
"""
)
}
Expand All @@ -496,24 +487,18 @@ final class ConsecutiveStatementsTests: XCTestCase {
assertParse(
"""
// At the top level
var i, j : Int1️⃣ i = j2️⃣ j = i
var i, j : Int 1️⃣i = j j = i
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
),
message: "expected '=' in variable",
fixIts: ["insert '='"]
)
],
applyFixIts: ["insert ';'"],
fixedSource: """
// At the top level
var i, j : Int; i = j; j = i
var i, j : Int = i = j j = i
"""
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,15 +964,16 @@ final class ForwardSlashRegexTests: XCTestCase {
assertParse(
"""
do {
let 1️⃣/x/
let 1️⃣/x/
}
""",
diagnostics: [
DiagnosticSpec(message: "expected pattern in variable", fixIts: ["insert pattern"])
DiagnosticSpec(message: "expected pattern in variable", fixIts: ["insert pattern"]),
DiagnosticSpec(message: "expected '=' in variable", fixIts: ["insert '='"]),
],
fixedSource: """
do {
let <#pattern#>/x/
let <#pattern#> = /x/
}
"""
)
Expand Down
59 changes: 6 additions & 53 deletions Tests/SwiftParserTest/translated/RecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ final class RecoveryTests: XCTestCase {
)
}

func testRecovery7a() {
func testRecovery7() {
assertParse(
"""
func useContainer() -> () {
var a : Containerℹ️<not 1️⃣a2️⃣ type [skip 3️⃣this greater: >] >4️⃣, b : Int
var a : Containerℹ️<not 1️⃣2️⃣a type [skip 3️⃣this greater: >] >4️⃣, b : Int
b = 5 // no-warning
a.exists()
}
Expand All @@ -61,55 +61,8 @@ final class RecoveryTests: XCTestCase {
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
message: "unexpected code 'this greater: >' in subscript"
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "expected expression after operator",
fixIts: ["insert expression"]
),
DiagnosticSpec(
locationMarker: "4️⃣",
message: "unexpected code in function"
),
],
applyFixIts: ["insert '>'", "insert newline", "insert expression"],
fixedSource: """
func useContainer() -> () {
var a : Container<not>a
type [skip this greater: >] > <#expression#>, b : Int
b = 5 // no-warning
a.exists()
}
"""
)
}

func testRecovery7b() {
assertParse(
"""
func useContainer() -> () {
var a : Containerℹ️<not 1️⃣a2️⃣ type [skip 3️⃣this greater: >] >4️⃣, b : Int
b = 5 // no-warning
a.exists()
}
""",
diagnostics: [
DiagnosticSpec(
locationMarker: "1️⃣",
message: "expected '>' to end generic argument clause",
notes: [NoteSpec(message: "to match this opening '<'")],
fixIts: ["insert '>'"]
),
DiagnosticSpec(
locationMarker: "2️⃣",
message: "consecutive statements on a line must be separated by newline or ';'",
fixIts: ["insert newline", "insert ';'"]
message: "expected '=' in variable",
fixIts: ["insert '='"]
),
DiagnosticSpec(
locationMarker: "3️⃣",
Expand All @@ -125,10 +78,10 @@ final class RecoveryTests: XCTestCase {
message: "unexpected code in function"
),
],
applyFixIts: ["insert '>'", "insert ';'", "insert expression"],
applyFixIts: ["insert '>'", "insert expression"],
fixedSource: """
func useContainer() -> () {
var a : Container<not>a; type [skip this greater: >] > <#expression#>, b : Int
var a : Container<not> a type [skip this greater: >] > <#expression#>, b : Int
b = 5 // no-warning
a.exists()
}
Expand Down