nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.87k stars 638 forks source link

Lots of issues with `nonisolated(unsafe)` #1644

Closed finnvoor closed 6 months ago

finnvoor commented 6 months ago

Hi! I'm writing some code that uses nonisolated(unsafe) and running into a lot of issues formatting it.

Error

The following snippet errors when formatted (SwiftFormat 0.53.4, default conf)

error: Unexpected static keyword on line 1 in

public static func test1() {}

static func test2() {}

private nonisolated(unsafe) static var test3: ((
    _ true: Bool
) -> Bool)?

Unexpected Results

The following snippet outputs really incorrect formatting when formatted using the --enable organizeDeclarations flag:

class Test {
    public static func test1() {}

    private nonisolated(unsafe) static var test3: ((
        _ arg1: Bool,
        _ arg2: Int
    ) -> Bool)?

    static func test2() {}
}

results in

class Test {
    // MARK: Public

    public static func test1() {}

    private nonisolated(unsafe) static var test3: ((
        // MARK: Internal

        _ arg1: Bool,
        _ arg2: Int
    ) -> Bool)?

    static func test2() {}
}

When run on this file, everything breaks completely 😄

nicklockwood commented 6 months ago

@finnvoor thanks! Looking into it now.

private nonisolated(unsafe) static var test3: ((

FYI this one isn't a bug - it's illegal to use static outside of a type context so SwiftFormat rejects it

finnvoor commented 6 months ago

@finnvoor thanks! Looking into it now.

private nonisolated(unsafe) static var test3: ((

FYI this one isn't a bug - it's illegal to use static outside of a type context so SwiftFormat rejects it

Ah right, whoops. I definitely did run into this error with valid code though, I think probably due to the same bug as the second case.

nicklockwood commented 6 months ago

@finnvoor this should now be fixed on the develop branch.

(ps, I just realized you're the Swift-on-Playdate guy - such a cool project!)

finnvoor commented 6 months ago

thanks for the quick fix! Definitely wouldn't call myself the swift-on-playdate guy, that would be @rauhul, I'm just building on the work done by him!

nicklockwood commented 6 months ago

@finnvoor fixed in 0.53.5