Skip to content

Existential method call incorrectly devirtualized after reassignment #85689

@nocchijiang

Description

@nocchijiang

Description

As title said.

Reproduction

protocol Fooable {
    func doSomething()
}

struct Foo1: Fooable {
    func doSomething() {
        print("foo1 do something")
    }
}

struct Foo2: Fooable {
    func doSomething() {
        print("foo2 do something")
    }
}

@inline(never)
func callFunc() {
    var foo: Fooable? = Foo2()
    foo = Foo1()
    // expecting "foo1 do something"
    // actual output: "foo2 do something"
    foo?.doSomething()
}

callFunc()

Expected behavior

The program prints "foo1 do something".

Environment

Reproduciable starting with Swift 6.1 on all supported platforms

Additional information

Compiler explorer repro: https://godbolt.org/z/5aYPd8Wzh

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.regressiontriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions