Open kerrmarin opened 1 year ago
Slightly simpler case:
protocol A {
func value() -> Int
}
struct B: A {
func value() -> Int { 100 }
}
extension A where Self == B {
static func b() -> some A {
B()
}
}
func printValue(_ value: any A) {
print("value: \(value.value())")
}
// In another file
printValue(.b()) // crash
Might be worth saying that there is a couple of cases where it doesn't crash:
printValue(.b())
call is in the same file as the extension A
extension A where Self == B {
static var b: some A {
B()
}
}
// In another file
printValue(.b) // doesn't crash.
@xedin I know a generic parameter can be inferred via calling a static protocol extension member with leading dot syntax if said member returns Self
and Self
is concrete under the member’s generic sig (SE-0299), but this? Do we expect it to survive Sema? Or the following seemingly related example where the same-type constraint is required but not used as an inference source:
protocol P {}
struct B: P {}
struct C: P {}
extension P where Self == B {
static func c() -> C { C() }
}
func test<T: A>(_: T) {}
test(.c()) // 'T := C'?!
I'm hitting the same situation as @rraphaeldev described, trying to define a helper function on a concrete impl of a protocol to return a some MyProtocol
. Although when I tried a minimal repro, I got a crash at __swift_allocate_boxed_opaque_existential_1
when the helper was called, instead of a __swift_instantiateConcreteTypeFromMangledName
earlier (I mention this mostly so anyone searching for that symbol will also find this issue).
Might be worth saying that there is a couple of cases where it doesn't crash:
I would add to this, it also doesn't crash if you change func b() -> some A
to func b() -> B
. Which makes me think it's quite similar to #68733. Basically every time I try and use some Foo
to try and hide a type, I hit a swift bug.
I hit something similar in another project… I believe this also looks similar to https://github.com/apple/swift/issues/69057.
Describe the bug When creating a protocol with an associated type, and extensions to that protocol that create instances of concrete implementations, and then creating instances outside of the framework that defines these extensions Swift crashes at runtime.
Steps To Reproduce Steps to reproduce the behavior:
Create a protocol like:
Instantiate
some A
(or an array[any A]
)from a different framework(?) for example from a test target:Expected behavior I expect the test to pass, or for the compiler to complain about a mistake I've made
Environment (please fill out the following information)
Additional context This does not happen if, instead of
some A
in the extension you return the concrete typeC
. Example that reproduces this here: https://github.com/kerrmarin/swift-bug-29-sept