File tree Expand file tree Collapse file tree 6 files changed +66
-11
lines changed
Tests/SwiftParserTest/translated Expand file tree Collapse file tree 6 files changed +66
-11
lines changed Original file line number Diff line number Diff line change @@ -1967,7 +1967,23 @@ extension Lexer.Cursor {
1967
1967
if self . input. baseAddress! - tokStart. input. baseAddress! == 1 {
1968
1968
switch tokStart. peek ( ) {
1969
1969
case UInt8 ( ascii: " = " ) :
1970
- return Lexer . Result ( . equal)
1970
+ if leftBound != rightBound {
1971
+ var errorPos = tokStart
1972
+
1973
+ if rightBound {
1974
+ _ = errorPos. advance ( )
1975
+ }
1976
+
1977
+ return Lexer . Result (
1978
+ . equal,
1979
+ error: LexingDiagnostic (
1980
+ . equalMustHaveConsistentWhitespaceOnBothSides,
1981
+ position: errorPos
1982
+ )
1983
+ )
1984
+ } else {
1985
+ return Lexer . Result ( . equal)
1986
+ }
1971
1987
case UInt8 ( ascii: " & " ) :
1972
1988
if leftBound == rightBound || leftBound {
1973
1989
break
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ public extension TokenError {
39
39
/// Please order the cases in this enum alphabetically by case name.
40
40
public enum StaticTokenError : String , DiagnosticMessage {
41
41
case editorPlaceholder = " editor placeholder in source file "
42
+ case equalMustHaveConsistentWhitespaceOnBothSides = " '=' must have consistent whitespace on both sides "
42
43
case expectedBinaryExponentInHexFloatLiteral = " hexadecimal floating point literal must end with an exponent "
43
44
case expectedClosingBraceInUnicodeEscape = #"expected '}' in \u{...} escape sequence"#
44
45
case expectedDigitInFloatLiteral = " expected a digit in floating point exponent "
@@ -134,6 +135,7 @@ public extension SwiftSyntax.TokenDiagnostic {
134
135
135
136
switch self . kind {
136
137
case . editorPlaceholder: return StaticTokenError . editorPlaceholder
138
+ case . equalMustHaveConsistentWhitespaceOnBothSides: return StaticTokenError . equalMustHaveConsistentWhitespaceOnBothSides
137
139
case . expectedBinaryExponentInHexFloatLiteral: return StaticTokenError . expectedBinaryExponentInHexFloatLiteral
138
140
case . expectedClosingBraceInUnicodeEscape: return StaticTokenError . expectedClosingBraceInUnicodeEscape
139
141
case . expectedDigitInFloatLiteral: return StaticTokenError . expectedDigitInFloatLiteral
Original file line number Diff line number Diff line change @@ -771,6 +771,24 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
771
771
if shouldSkip ( node) {
772
772
return . skipChildren
773
773
}
774
+
775
+ if node. equal. hasError && node. equal. presence == . present {
776
+ addDiagnostic (
777
+ node. equal,
778
+ StaticTokenError . equalMustHaveConsistentWhitespaceOnBothSides,
779
+ fixIts: [
780
+ FixIt (
781
+ message: . insertWhitespace,
782
+ changes: [
783
+ FixIt . Changes ( changes: [ . replaceLeadingTrivia( token: node. equal, newTrivia: . spaces( 1 ) ) ] ) ,
784
+ FixIt . Changes ( changes: [ . replaceTrailingTrivia( token: node. equal, newTrivia: . spaces( 1 ) ) ] ) ,
785
+ ]
786
+ )
787
+ ] ,
788
+ handledNodes: [ node. equal. id]
789
+ )
790
+ }
791
+
774
792
if node. equal. presence == . missing {
775
793
exchangeTokens (
776
794
unexpected: node. unexpectedBeforeEqual,
Original file line number Diff line number Diff line change @@ -498,6 +498,9 @@ extension FixItMessage where Self == StaticParserFixIt {
498
498
public static var insertNewline : Self {
499
499
. init( " insert newline " )
500
500
}
501
+ public static var insertWhitespace : Self {
502
+ . init( " insert whitespace " )
503
+ }
501
504
public static var joinIdentifiers : Self {
502
505
. init( " join the identifiers together " )
503
506
}
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ public struct TokenDiagnostic: Hashable {
23
23
// Please order these alphabetically
24
24
25
25
case editorPlaceholder
26
+ case equalMustHaveConsistentWhitespaceOnBothSides
26
27
case expectedBinaryExponentInHexFloatLiteral
27
28
case expectedClosingBraceInUnicodeEscape
28
29
case expectedDigitInFloatLiteral
@@ -96,6 +97,7 @@ public struct TokenDiagnostic: Hashable {
96
97
public var severity : Severity {
97
98
switch kind {
98
99
case . editorPlaceholder: return . error
100
+ case . equalMustHaveConsistentWhitespaceOnBothSides: return . error
99
101
case . expectedBinaryExponentInHexFloatLiteral: return . error
100
102
case . expectedClosingBraceInUnicodeEscape: return . error
101
103
case . expectedDigitInFloatLiteral: return . error
Original file line number Diff line number Diff line change @@ -1962,14 +1962,21 @@ final class RecoveryTests: XCTestCase {
1962
1962
"""
1963
1963
// <rdar://problem/22387625> QoI: Common errors: 'let x= 5' and 'let x =5' could use Fix-its
1964
1964
func r22387625() {
1965
- let _ = 5
1966
- let _ =5
1965
+ let _1️⃣ = 5
1966
+ let _ =2️⃣5
1967
1967
}
1968
1968
""" ,
1969
1969
diagnostics: [
1970
- // TODO: Old parser expected error on line 3: '=' must have consistent whitespace on both sides, Fix-It replacements: 8 - 8 = ' '
1971
- // TODO: Old parser expected error on line 4: '=' must have consistent whitespace on both sides, Fix-It replacements: 10 - 10 = ' '
1972
- ]
1970
+ DiagnosticSpec ( locationMarker: " 1️⃣ " , message: " '=' must have consistent whitespace on both sides " ) ,
1971
+ DiagnosticSpec ( locationMarker: " 2️⃣ " , message: " '=' must have consistent whitespace on both sides " ) ,
1972
+ ] ,
1973
+ fixedSource: """
1974
+ // <rdar://problem/22387625> QoI: Common errors: 'let x= 5' and 'let x =5' could use Fix-its
1975
+ func r22387625() {
1976
+ let _ = 5
1977
+ let _ = 5
1978
+ }
1979
+ """
1973
1980
)
1974
1981
}
1975
1982
@@ -1978,14 +1985,21 @@ final class RecoveryTests: XCTestCase {
1978
1985
"""
1979
1986
// https://github.com/apple/swift/issues/45723
1980
1987
do {
1981
- let _: Int = 5
1982
- let _: Array<Int>= []
1988
+ let _: Int1️⃣ = 5
1989
+ let _: Array<Int>2️⃣ = []
1983
1990
}
1984
1991
""" ,
1985
1992
diagnostics: [
1986
- // TODO: Old parser expected error on line 3: '=' must have consistent whitespace on both sides, Fix-It replacements: 13 - 13 = ' '
1987
- // TODO: Old parser expected error on line 4: '=' must have consistent whitespace on both sides, Fix-It replacements: 20 - 20 = ' '
1988
- ]
1993
+ DiagnosticSpec ( locationMarker: " 1️⃣ " , message: " '=' must have consistent whitespace on both sides " ) ,
1994
+ DiagnosticSpec ( locationMarker: " 2️⃣ " , message: " '=' must have consistent whitespace on both sides " ) ,
1995
+ ] ,
1996
+ fixedSource: """
1997
+ // https://github.com/apple/swift/issues/45723
1998
+ do {
1999
+ let _: Int = 5
2000
+ let _: Array<Int> = []
2001
+ }
2002
+ """
1989
2003
)
1990
2004
}
1991
2005
You can’t perform that action at this time.
0 commit comments