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-7976] Unexpected behavior from conditional conformances #50510

Closed
DevAndArtist mannequin opened this issue Jun 12, 2018 · 10 comments
Closed

[SR-7976] Unexpected behavior from conditional conformances #50510

DevAndArtist mannequin opened this issue Jun 12, 2018 · 10 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Jun 12, 2018

Previous ID SR-7976
Radar rdar://problem/41063870
Original Reporter @DevAndArtist
Type Bug
Status Resolved
Resolution Done
Environment

Discovered with:

Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1)

Also present in: 4.2 (first WWDC Xcode 10 build)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: 50305cd36c55a0b42c3ff7ecf67477f3

relates to:

  • SR-5440 Typealias in constrained extension misinterprets the where clause

Issue Description:

The following code snippets presents two issues discovered with conditional conformances:

1. First issue is wrong type resolution.
2. Second issue is a successful conditional conformance where it should have failed.

protocol TestFoo {
  associatedtype Foo
}

protocol TestBar {
  associatedtype Bar
}

struct Generic<Foo, Bar> {}
extension Generic : TestFoo where Foo == Int {}
extension Generic : TestBar where Foo == Bar {}
print(Generic<String, Bool>.Foo.self) // prints `Int` but should be `String`
print(Generic<String, Bool>.Bar.self) // compiles and prints `String` but should not because Foo != Bar
@huonw
Copy link
Mannequin

huonw mannequin commented Jun 12, 2018

This works (as in, fails to compile with a vaguely reasonable error message) without the conformance:

struct Generic<Foo, Bar> {}
extension Generic where Foo == Int {
    typealias X = Foo
}
extension Generic where Foo == Bar {
    typealias Y = Foo
}
print(Generic<String, Bool>.X.self) 
print(Generic<String, Bool>.Y.self) 
sr7976.swift:8:7: error: 'Generic<String, Bool>.X.Type' (aka 'Int.Type') requires the types 'String' and 'Int' be equivalent
print(Generic<String, Bool>.X.self)
      ^
sr7976.swift:9:7: error: 'Generic<String, Bool>.Y.Type' (aka 'String.Type') requires the types 'String' and 'Bool' be equivalent
print(Generic<String, Bool>.Y.self)
      ^

@huonw
Copy link
Mannequin

huonw mannequin commented Jun 12, 2018

@swift-ci create

@DevAndArtist
Copy link
Mannequin Author

DevAndArtist mannequin commented Jun 12, 2018

@huonw for some reason your code does compile without an error in Swift playground and prints `Int` and `String`. Anyway your first error message seems to be wrong. The constraint for `X` to be a member of `Generic` does not require `Foo == Bar` but only `Foo == Int`.

Edit: Sorry I misunderstood the error message, it's correct.

@jepers
Copy link

jepers commented Jun 12, 2018

Could this be related to SR-5440?

@huonw
Copy link
Mannequin

huonw mannequin commented Jun 13, 2018

Ah, great catch. I was using the master branch, and it seems like the typealias case/SR-5440 was recently (mostly) fixed (in d573309 specifically), but associated types seem to not have been covered by that.

@jepers
Copy link

jepers commented Jun 13, 2018

Then maybe this is related to SR-7605 too? And maybe it's also affecting generic type parameters as in the example of my comment there?

@huonw
Copy link
Mannequin

huonw mannequin commented Jun 13, 2018

#17168 fixes this when the types are used in expressions, but there's no error if they're used in decls, e.g. let _: Generic<String, Bool>.Foo instead of the print.

It could very well be related to SR-7605, and the fix for this might fix that one too, but associated type inference is a complicated beast at the moment, so I would (unfortunately) guess it won't be quite that simple.

@jepers
Copy link

jepers commented Jun 13, 2018

OK, thanks!

@AnthonyLatsis
Copy link
Collaborator

Both of these rightly raise errors with the latest snapshot, in expression and type identifier positions.

@AnthonyLatsis
Copy link
Collaborator

Fixed by Slava alongside SR-5440

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants