Skip to content

Sema: Ban unavailable overrides of available decls #65426

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

Closed
wants to merge 1 commit into from

Conversation

tshortli
Copy link
Contributor

Unavailable decls should not be allowed to override available decls since overrides can be invoked at runtime through vtables and are therefore reachable at runtime if the base declaration is available. For example:

@available(*, unavailable)
struct Unavailable {
  init() { print("Oops") }
}

class Base {
  required init() {}

  class func factory() -> Self {
    Self.init()
  }
}

class Derived: Base {
  @available(*, unavailable)
  required init() {
    super.init()
    _ = Unavailable()
  }
}

_ = Derived.factory() // Oops

Resolves rdar://108396778

Unavailable decls should not be allowed to override available decls since
overrides can be invoked at runtime through vtables and are therefore reachable
at runtime if the base declaration is available. For example:

```
@available(*, unavailable)
struct Unavailable {
  init() { print("Oops") }
}

class Base {
  required init() {}

  class func factory() -> Self {
    Self.init()
  }
}

class Derived: Base {
  @available(*, unavailable)
  required init() {
    super.init()
    _ = Unavailable()
  }
}

_ = Derived.factory() // Oops
```

Resolves rdar://108396778
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli
Copy link
Contributor Author

@swift-ci please test source compatibility

@tshortli tshortli marked this pull request as draft April 26, 2023 03:48
@tshortli
Copy link
Contributor Author

It looks like banning is going to be too much of a source break. In that case, I think we should probably make it behave the way developers would intuitively think it would behave and instead remove unavailable methods from vtables so that they don't get executed via dynamic dispatch.

@tshortli
Copy link
Contributor Author

I turned the diagnostic improvements and test coverage improvements from this change into a separate PR that doesn't restrict unavailable decls from overriding available decls: #65456

@tshortli
Copy link
Contributor Author

Closing this PR.

@tshortli tshortli closed this Apr 27, 2023
@tshortli tshortli deleted the ban-unavailable-overrides branch April 27, 2023 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant