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-13018] Type constraints are not honored when invoking method #55463

Closed
mbrandonw opened this issue Jun 15, 2020 · 3 comments
Closed

[SR-13018] Type constraints are not honored when invoking method #55463

mbrandonw opened this issue Jun 15, 2020 · 3 comments
Assignees
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 run-time crash Bug → crash: Swift code crashed during execution

Comments

@mbrandonw
Copy link
Contributor

Previous ID SR-13018
Radar rdar://problem/64953119
Original Reporter @mbrandonw
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, RunTimeCrash
Assignee @slavapestov
Priority Medium

md5: 2c03afa2034e8d179cd73102ecf6a551

Issue Description:

Consider the following method:

struct T<A> {
  func run<C>(_ c: C) where C: Collection, C.SubSequence == A {
    print(c[...] as! A)
  }
}

This should restrict the collection passed into `run` to be such that its subsequence matches the generic A.

For example, these following compile just fine:

// These should compile ✅
T<Substring>().run("")
T<Data>().run(Data())

However, there seem to be some situations where we can pass in a collection that does not match this constraint and yet it still compiles, and hence crashes at runtime:

// These should not but do⁉️
T<Substring>().run(Data())
T<Data>().run("")
@CodaFi
Copy link
Member

CodaFi commented Jun 19, 2020

Ah, this is an impressive soundness hole. We're straight-up forming a generic signature with the wrong constraints.

Generic signature: <A, C where A == A.SubSequence, C : Collection>

@swift-ci create

@slavapestov
Copy link
Member

Here is a reduction:

protocol P {
  associatedtype A : P where A.A == A
}

func foo<X, Y>(_: X, _: Y) where X : P, Y : P, Y.A == X {}

@slavapestov
Copy link
Member

#42113

@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
This issue was closed.
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 run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

4 participants