Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime crash: T constraint ignored for any Alias<T> when typealias Alias<T> = ProtoA & ProtoB<T> #72390

Closed
bubski opened this issue Mar 18, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software duplicate Resolution: Duplicates another issue existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values generics Feature: generic declarations and types parameterized protocols Feature → protocol: protocols with primary associated types protocol compositions Feature → types: protocol composition types run-time crash Bug → crash: Swift code crashed during execution swift 6.0 types Feature: types

Comments

@bubski
Copy link

bubski commented Mar 18, 2024

Description

No response

Reproduction

protocol ProtoA {}

protocol ProtoB<C> {
    associatedtype C
    var val: C { get }
}

typealias Alias<T> = ProtoA & ProtoB<T>

struct Foo: ProtoA, ProtoB {
    var val: String { "hello" }
}

// This should not compile.
// `Foo` matches `Alias<String>` but not `Alias<[String]>`.
var foo: any Alias<[String]> {
    Foo()
}

print(foo.val) // crash

https://godbolt.org/z/754fnc3qY

Stack dump

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib            	       0x1acf1a6c8 swift_retain + 44
1   libswiftCore.dylib            	       0x1acf1f714 swift::metadataimpl::BufferValueWitnesses<swift::metadataimpl::ValueWitnesses<swift::metadataimpl::BridgeObjectBox>, true, 8ul, 8ul, (swift::metadataimpl::FixedPacking)1>::initializeBufferWithCopyOfBuffer(swift::TargetValueBuffer<swift::InProcess>*, swift::TargetValueBuffer<swift::InProcess>*, swift::TargetMetadata<swift::InProcess> const*) + 28
2   libswiftCore.dylib            	       0x1aceb88f8 outlined init with copy of Any + 76
3   libswiftCore.dylib            	       0x1acd11d5c specialized _print<A>(_:separator:terminator:to:) + 232
4   libswiftCore.dylib            	       0x1acd10efc print(_:separator:terminator:) + 192
5   main                          	       0x10442bb20 main + 312
6   dyld                          	       0x19cb2e0e0 0x19cb28000 + 24800

Expected behavior

The code should not compile.
There should be an error emitted for type mismatch in:

// This should not compile.
// `Foo` matches `Alias<String>` but not `Alias<[String]>`.
var foo: any Alias<[String]> {
    Foo()
}

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

For what its worth, this only happens when typealias is involved.
Otherwise, there's an unrelated compilation error:

main.swift:14:23: error: non-protocol, non-class type 'ProtoB<[String]>' cannot be used within a protocol-constrained type
var foo: any ProtoA & ProtoB<[String]> {
                      ^

#63877 (comment) leads me to believe that this particular error should not actually exist,
and both Alias<[String]> and ProtoA & ProtoB<[String]> should be allowed and handled properly.

@bubski bubski added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Mar 18, 2024
@zvonicek
Copy link

zvonicek commented Jun 5, 2024

update: still crashes on Swift 5.10, now with the following message

Failed to reconstruct type for $s6output6ProtoA_AA0B1BpSaySSG1CAaCPRts_XPD
Original type:
(existential_type
  (protocol_composition_type
    (protocol_type decl=output.(file).ProtoA@<source>:1:10)
    (parameterized_protocol_type
      (base=protocol_type decl=output.(file).ProtoB@<source>:3:10)
      (bound_generic_struct_type decl=Swift.(file).Array
        (struct_type decl=Swift.(file).String)))))

@hborla hborla added generics Feature: generic declarations and types existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values and removed triage needed This issue needs more specific labels labels Jul 14, 2024
@slavapestov
Copy link
Contributor

slavapestov commented Aug 3, 2024

Duplicate of #63877

@AnthonyLatsis AnthonyLatsis added run-time crash Bug → crash: Swift code crashed during execution parameterized protocols Feature → protocol: protocols with primary associated types protocol compositions Feature → types: protocol composition types types Feature: types compiler The Swift compiler itself swift 6.0 duplicate Resolution: Duplicates another issue labels Aug 4, 2024
@slavapestov slavapestov marked this as a duplicate of #63877 Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software duplicate Resolution: Duplicates another issue existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values generics Feature: generic declarations and types parameterized protocols Feature → protocol: protocols with primary associated types protocol compositions Feature → types: protocol composition types run-time crash Bug → crash: Swift code crashed during execution swift 6.0 types Feature: types
Projects
None yet
Development

No branches or pull requests

5 participants