Skip to content

[Sema] Perform availability checks in literals initializers #61805

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

Merged

Conversation

LucianoPAlmeida
Copy link
Contributor

Some literal initializers were not being handled in ExprAvailabilityWalker which make then being left undiagnosed in cases like:

struct Foo {}

@available(*, deprecated)
extension Foo: ExpressibleByStringLiteral {
    init(stringLiteral value: StringLiteralType) { }
}

let a: Foo = Foo(stringLiteral: "") // warning: 'init(stringLiteral:)' is deprecated
let b: Foo = "" // no warning

So this adds check for all literals that have an associated implicit initializer and collection literal as well.

Fixes #61564

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please test

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think we probably ought to apply this logic to the result type of the literal too, but I'm happy for that to be done in a follow up if you want. cc @tshortli in case he has any other comments.

Comment on lines +3261 to +3263
// Regex literals require both the Regex<Output> type to be available,
// as well as the initializer that is implicitly called.
diagnoseDeclRefAvailability(Context.getRegexDecl(), Range);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is something that ought to be applied to the result type of a literal in general. e.g for:

@available(*, unavailable)
struct S: ExpressibleByIntegerLiteral {
  init(integerLiteral value: Int) {}
}
@available(*, unavailable)
typealias IntegerLiteralType = S

let i = 0

We're implicitly forming a call to S(integerLiteral: 0), but aren't erroring that it's unavailable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A long term goal of mine is to run availability checks on the transformed AST so that we can catch these issues in a more generalized way. Not sure if that applies directly here but it seems like it might.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah these literal cases are tricky because we don't actually form the call in the AST, we emit them directly in SILGen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is something that ought to be applied to the result type of a literal in general.

I'll create an issue to track that case and look at it separately =]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,169 @@
// RUN: %target-typecheck-verify-swift -swift-version 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the thoroughness of this test, thanks!

@hamishknight hamishknight requested a review from tshortli November 2, 2022 12:07
@LucianoPAlmeida LucianoPAlmeida merged commit 173afcf into swiftlang:main Nov 2, 2022
@LucianoPAlmeida LucianoPAlmeida deleted the availability-check-literal branch November 2, 2022 17:46
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.

@available doesn’t affect literal initialization
3 participants