Inout fails

Is there a reason the following code would crash with a bad access fault?

func has2inout( _ one:inout Int, _ two:inout Int )
{
    one = 5
    two = two + 1
}

var a :Int = 2
var b :Int = a

has2inout( &a, &b ) 
print( a, b )

I was trying to present an example that you can't create aliases using inout parameters, using the code:

func has2inout( _ one:inout Int, _ two:inout Int )
{
    one = 5
    two = two + 1
}

var a :Int = 2

has2inout( &a, &a )
print( a )

And Swift reported (as expected) "Inout arguments are not allowed to alias each other". It also mentioned "Overlapping accesses to 'a', but modification requires exclusive access; consider copying to a local variable" so I said to myself, "why not?" After copying a to a local variable b, it compiled but crashed when running. Any comments?
Thanks.

1 Like

This does not happen for me, using Xcode 16.2 or 16.3 beta.

Ditto, here too - using Xcode Version 16.2 (16C5032a) on a 2018 macMini.

@main
enum Temp {
    static func main () {
        func has2inout (_ one: inout Int, _ two: inout Int) {
            one = 5
            two = two + 1
        }

        var a: Int = 2
        var b: Int = a

        has2inout (&a, &b)
        print (a, b)
    }
}

Outputs

5 3
Program ended with exit code: 0

And today it ran for me. Wonder what I did wrong?

Was the segfault in the compiler itself?

Or did it compile the program successfully, and it was the program that segfaulted?

Segfault. However, I suspect my problem was this giant test file I've been using that had some other code prior to the has2inout function. There was probably some interaction with the previous code. Sadly, I've long since cleaned up that test file, so I doubt I'd be able to reproduce the problem at this time.