'static consuming func ...' throws 'Static functions must not be declared mutating' compiler error?

Hey there,

I wrote some Swift code as I got the following compiler error:

'Static functions must not be declared mutating'

I was confused, because I try to avoid mutations if I can.
So I simplified the method and get the same result with the following function:

    public static consuming func createTuple() -> (Int, Int) {
        let two = 2
        let five = 5
        return (two, five)
    }

Is this intended behaviour, a compiler bug and / or a bad error message?
I use Xcode 16.3 with Swift 5.10.

It seems to be the combination of static and consuming in the function head.

I'm pretty sure this is just the error message mixing the words "consuming" and "mutating".

In a consuming function, "consuming" applies to the hidden self argument. A static function has no self, so can't be consuming. The same is true with static mutating, and the check in the compiler is apparently spitting the same error message in both cases.

4 Likes

One nitpick: static functions do have a self, which refers to the metatype:

struct S {
    static func f() {
        print(self)
    }
}
S.f()  // "S"

But the rest of the point stands: metatypes are static data, so they can't be consumed. The error message could probably be improved here.

5 Likes