Protocol composition results in EXC_BAD_ACCESS

I'm getting an `EXC_BAD_ACCESS` here:

protocol Something {}

protocol IntContainer {
    var ints: [Int] { get set }
}

func processSomething(_ state: Something) {
    if let intContainer = state as? Something & IntContainer {
        print(intContainer.ints)
    }
}

struct MySomething: Something, IntContainer {
    var ints: [Int] =
}

let mySomething = MySomething()
processSomething(mySomething) // EXC_BAD_ACCESS here

The execution stops here:

libswiftCore.dylib`_swift_release_dealloc:
    0x11be163e0 <+0>: movq (%rdi), %rax
-> 0x11be163e3 <+3>: jmpq *-0x10(%rax)
    0x11be163e6 <+6>: nopw %cs:(%rax,%rax)

Any ideas why this is?

P.S. Related StackOverflow question with no good answer:

R+

Why do you cast against an existential in first place?

This solves the given example:

protocol Something {}

protocol IntContainer {
    var ints: [Int] { get set }
}

func processSomething(_ state: Something) {

    if let intContainer = state as? IntContainer {
        print(intContainer.ints)
    }
}

struct MySomething: Something, IntContainer {
    var ints: [Int] =
}

let mySomething = MySomething()
processSomething(mySomething)

···

--
Adrian Zubarev
Sent with Airmail

Am 3. April 2017 um 16:31:02, Rudolf Adamkovič via swift-users (swift-users@swift.org) schrieb:

I'm getting an `EXC_BAD_ACCESS` here:

protocol Something {}

protocol IntContainer {
var ints: [Int] { get set }
}

func processSomething(_ state: Something) {
if let intContainer = state as? Something & IntContainer {
print(intContainer.ints)
}
}

struct MySomething: Something, IntContainer {
var ints: [Int] =
}

let mySomething = MySomething()
processSomething(mySomething) // EXC_BAD_ACCESS here

The execution stops here:

libswiftCore.dylib`_swift_release_dealloc:
0x11be163e0 <+0>: movq (%rdi), %rax
-> 0x11be163e3 <+3>: jmpq *-0x10(%rax)
0x11be163e6 <+6>: nopw %cs:(%rax,%rax)

Any ideas why this is?

P.S. Related StackOverflow question with no good answer:

R+
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

To avoid force-unwrap at the end here:

class Store {

    var state: State

    // ...

    func processScheduledActions() {

        guard var effectfulState = state as? Effectful else {
            return
        }

        while let actions = effectfulState.scheduledActions.flush() {
            for action in actions {
                delegate?.store(self, didRequest: action)
            }
        }

        state = effectfulState as! State
        
    }

}

Thank you!

R+

···

On 3 Apr 2017, at 16:48, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Why do you cast against an existential in first place?

Thanks!

Done:

R+

···

On 3 Apr 2017, at 17:53, Joe Groff <jgroff@apple.com> wrote:

On Apr 3, 2017, at 7:53 AM, Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>> wrote:

However I kinda think this is a bug.

CC Joe Groff.

Yeah, that's a bug. If you haven't yet, could you file it at bugs.swift.org <Issues · apple/swift · GitHub?

-Joe

Why not making State as a class? EffectiveState should be subclass of State.

Zhaoxin

···

On Mon, Apr 3, 2017 at 11:08 PM, Rudolf Adamkovič via swift-users < swift-users@swift.org> wrote:

On 3 Apr 2017, at 16:48, Adrian Zubarev <adrian.zubarev@devandartist.com> > wrote:

Why do you cast against an existential in first place?

To avoid force-unwrap at the end here:

class Store {

    var state: State

    // ...

    func processScheduledActions() {

        guard var effectfulState = state as? Effectful else {
            return
        }

        while let actions = effectfulState.scheduledActions.flush() {
            for action in actions {
                delegate?.store(self, didRequest: action)
            }
        }

        state = effectfulState as! State

    }

}

Thank you!

R+

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Because it’s a value and has no identity.

R+

···

On 3 Apr 2017, at 17:58, Zhao Xin <owenzx@gmail.com> wrote:

Why not making State as a class? EffectiveState should be subclass of State.

Zhaoxin

On Mon, Apr 3, 2017 at 11:08 PM, Rudolf Adamkovič via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On 3 Apr 2017, at 16:48, Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>> wrote:

Why do you cast against an existential in first place?

To avoid force-unwrap at the end here:

class Store {

    var state: State

    // ...

    func processScheduledActions() {

        guard var effectfulState = state as? Effectful else {
            return
        }

        while let actions = effectfulState.scheduledActions.flush() {
            for action in actions {
                delegate?.store(self, didRequest: action)
            }
        }

        state = effectfulState as! State
        
    }

}

Thank you!

R+

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users