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

[SR-13434] subscript(dynamicMember:) method on generic type with constraints allows key paths with invalid types #55875

Open
Gotyanov opened this issue Aug 23, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@Gotyanov
Copy link

Previous ID SR-13434
Radar rdar://problem/67746797
Original Reporter @Gotyanov
Type Bug
Environment

XCode 12

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee None
Priority Medium

md5: 3c0b1f24cebd97971d4eb7d0b56236ae

Issue Description:

protocol ContainerType {
    associatedtype Parent

    var parent: Parent { get }
}

@dynamicMemberLookup
struct Context<Container: ContainerType> {
    let container: Container

    subscript<T>(dynamicMember keyPath: KeyPath<Context<Container.Parent>, T>) -> T {
        Context<Container.Parent>(container: container.parent)[keyPath: keyPath]
    }
}

struct NotContainer {
    let value = 42
}

struct ContainerImpl: ContainerType {
    let parent = NotContainer()
}

// uncomment next line to get a crash with "Illegal instuction: 4" message
// print(Context(container: ContainerImpl()).value)

Type Context<Container.Parent> is invalid because Context has a constraint on Container – this type must conform to ContainerType protocol, but Container.Parent does not conform to it.
Unfortunately this code compiles, unless your uncomment last line.

Expected result – Context<Container.Parent> usage should cause an error "Type 'Container.Parent' does not conform to protocol 'ContainerType'"

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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 in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

3 participants