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-9037] Compiler can't implicitly upcast the base of a reference to a constrained protocol extension member #51540

Open
hamishknight opened this issue Oct 18, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-9037
Radar rdar://problem/45962482
Original Reporter @hamishknight
Type Bug
Status Reopened
Resolution
Environment

Swift version 4.2-dev (LLVM c4a0883115, Clang 0a99881462, Swift 533575ab62)
Target: x86_64-apple-darwin17.7.0

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

md5: b19e3c890f5c362da2976fa71ac86fa7

Issue Description:

The following doesn't compile (with an amusingly incorrect error message):

protocol P {}
extension P where Self == A {
  func foo() {}
}

class A : P {}
class B : A {}

let b = B()
b.foo() // error: 'B' is not convertible to 'A'

Putting the bad diagnostic aside, arguably the compiler should be able to implicitly upcast the base of the member reference here.

Although admittedly the above example is contrived as you would instead write the constraint as where Self : A, this issue was ran into when attempting to call contains on a subclassed OptionSet:

class A : OptionSet {
  var rawValue: Int

  required init(rawValue: RawValue) {
    self.rawValue = rawValue
  }
}

class B : A {
  static let b = B(rawValue: 1 << 2)
}

let foo = B.b
print(foo.contains(B.b)) // 'B' requires the types 'B' and 'A' be equivalent to use 'contains'

Due to the fact that contains is defined in an extension on OptionSet with the constraint where Self == Element.

@belkadan
Copy link
Contributor

cc @rudkx, @xedin

@belkadan
Copy link
Contributor

There are some tricky cases; for instance, it's correct to reject this...

protocol P {}
extension P where Self == A {
  mutating func foo() { }
}

class A : P {}
class B : A {}

var b = B()
b.foo()

...because you might not get a B back.

@xedin
Copy link
Member

xedin commented Nov 2, 2018

@swift-ci create

@slavapestov
Copy link
Member

I don't think we should allow protocol extensions where Self is constrained to a concrete type. It is non-sensical and you really want to just write 'extension A' at this point.

@belkadan
Copy link
Contributor

As the comment points out, Self might be constrained to an associated type instead.

@slavapestov
Copy link
Member

Ok, in that case it's working as intended.

@belkadan
Copy link
Contributor

No, it's not. The signature of foo doesn't involve any Self types and isn't mutating, so the compiler should be able to insert the upcast for you.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants