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

mbrandonw opened this issue Jun 15, 2020 · 3 comments

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

mbrandonw opened this issue Jun 15, 2020 · 3 comments


Copy link

@mbrandonw mbrandonw commented Jun 15, 2020

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 ✅

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⁉️
Copy link

@CodaFi 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

Copy link

@slavapestov slavapestov commented Aug 15, 2020

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 {}

Copy link

@slavapestov slavapestov commented Apr 2, 2022

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants