Skip to content
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

Bug: Method Call with Generic Invariance Swaps Parameter List with Right Hand Side of Comparison #1957

Open
orchetect opened this issue Jan 4, 2025 · 0 comments

Comments

@orchetect
Copy link

orchetect commented Jan 4, 2025

Issue

This is an odd edge-case bug where when a method call has inline generics in a comparison expression, the parameter list of the method call is swapped with the right-hand side content of the expression when the right-hand side is a variable and not a literal.

Conditions

The issue can be reproduced with:

  • SwiftFormat 0.55.3
  • factory default SwiftFormat configuration
  • specifying any Swift version - I tried all versions from Swift 5.0 through 6.0 and they all produce the same incorrect output
  • issue still happens when parameter list is non-empty

Example

Original code:

func method() -> Int { 123 }
func method() -> String { "string" }

let intVariable: Int = 123

enum IntEnum: Int {
    case foo = 123
}

let stringVariable: String = "string"

enum StringEnum: Int {
    case foo = "string"
}

func test() {
    print(method<Int>() == 123)
    print(method<Int>() == intVariable)
    print(method<Int>() == IntEnum.foo.rawValue)
    
    print(method<String>() == "string")
    print(method<String>() == stringVariable)
    print(method<String>() == StringEnum.foo.rawValue)
}

SwiftFormat output (diff):

-    print(method<Int>() == intVariable)
+    print(method<Int>intVariable == ())
-    print(method<Int>() == IntEnum.foo.rawValue)
+    print(method<Int>IntEnum.foo.rawValue == ())

-    print(method<String>() == stringVariable)
+    print(method<String>stringVariable == ())
-    print(method<String>() == StringEnum.foo.rawValue)
+    print(method<String>StringEnum.foo.rawValue == ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant