Skip to content

Sema: Refine availability for unexposed initializer expressions of public properties #58870

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

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented May 12, 2022

Teach the compiler to refine VarDecl initializer expressions using the deployment target when the init would not be exposed to module clients. Without this, the initializers of public properties in API modules could be misdiagnosed as potentially unavailable to clients of the module, even though the expression will only ever execute on the deployment target or higher.

While developing this fix I discovered a few other bugs that will need to be addressed separately:

  • The availability of the inferred type of a property is not diagnosed. Previously this mattered less since the initializer expression and the var declaration itself would both be diagnosed with the same availability.
  • The availability of the property wrapper type of a VarDecl appears to not be diagnosed at all.
  • Members of frozen structs with property wrappers result in broken swiftinterfaces because the synthesized storage is printed explicitly, confusing the diagnostics.

Resolves rdar://92713589

@tshortli tshortli force-pushed the pattern-binding-decl-type-refinement-contexts branch 3 times, most recently from a5004b7 to cee861f Compare May 13, 2022 18:45
@tshortli tshortli changed the title Sema: Build TypeRefinementContexts for initializer expressions of public properties Sema: Refine availability for unexposed initializer expressions of public properties May 13, 2022
@tshortli tshortli force-pushed the pattern-binding-decl-type-refinement-contexts branch from cee861f to 874168c Compare May 13, 2022 18:49
@tshortli tshortli marked this pull request as ready for review May 13, 2022 18:49
@tshortli tshortli requested review from beccadax and xymus May 13, 2022 18:50
@tshortli tshortli force-pushed the pattern-binding-decl-type-refinement-contexts branch 2 times, most recently from 80b9343 to 7b0f74b Compare May 17, 2022 00:17
@tshortli

This comment was marked as outdated.

@tshortli tshortli marked this pull request as draft May 17, 2022 16:00
@tshortli tshortli force-pushed the pattern-binding-decl-type-refinement-contexts branch 2 times, most recently from 64d0b3b to 8c9fae4 Compare May 17, 2022 23:56
…using the deployment target when the init would not be exposed to module clients. Without this, the initializers of public properties in API modules could be misdiagnosed as potentially unavailable to clients of the module, even though the expression will only ever execute on the deployment target or higher.

While developing this fix I discovered a few other bugs that will need to be addressed separately:
- The availability of the inferred type of a property is not diagnosed. Previously this mattered less since the initializer expression and the var declaration itself would both be diagnosed with the same availability.
- The availability of the property wrapper type of a `VarDecl` appears to not be diagnosed at all.
- Members of frozen structs with property wrappers result in broken swiftinterfaces because the synthesized storage is printed explicitly, confusing the diagnostics.

Resolves rdar://92713589
@tshortli tshortli force-pushed the pattern-binding-decl-type-refinement-contexts branch from 8c9fae4 to f720eea Compare May 18, 2022 00:14
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli marked this pull request as ready for review May 18, 2022 00:14
@tshortli tshortli merged commit bc43ced into swiftlang:main May 18, 2022
@tshortli tshortli deleted the pattern-binding-decl-type-refinement-contexts branch May 18, 2022 05:38
tshortli added a commit that referenced this pull request Jun 1, 2022
… min` (#59176)

Cherry-pick and squash of the following PRs to release/5.7:

#42585
#58417
#58654
#58680
#58707
#58870
#58963
#59040
#59065

* Sema: Fix `isExported()` for extension decls in order to correct availability diagnostics when `-target-min-inlining-version min` is specified.

Resolves rdar://91382040

* Sema: Avoid diagnosing potential unavailability of type components (extension nominal type, superclass, etc.) on declarations that are explicitly unavailable.

Resolves rdar://92179327

* Sema: Downgrade diagnostics about potential unavailability of the extended type in an extension declaration when the following conditions are met:

1. The extension is missing explicit availability.
2. The required availability is before the deployment target.

Resolves rdar://92621567

* NFC: Expand `-target-min-inlining-versiong` tests to cover a number of corner cases:

- unavailable declarations and unavailable containers
- SPI declarations and spi containers
- property initializer expressions
- property wrappers

* Sema: When computing potential unavailability of a decl, first check whether the decl is explicitly unavailable and the context is also unavailable. If those conditions are met, treat the decl as if it were always available since unavailable code is allowed to reference unavailable decls.

Resolves rdar://92551870

* Sema: Use the deployment target when checking availability for SPI and unavailable API declarations with `-target-min-inlining-version min` specified. There's not much benefit to more accurate enforcement of availability in these decls since API clients can't use them and there's a lot of existing code that would be needlessly diagnosed without these exceptions.

Resolves rdar://92716633

* Sema: Teach the compiler to refine `VarDecl` initializer expressions using the deployment target when the init would not be exposed to module clients. Without this, the initializers of public properties in API modules could be misdiagnosed as potentially unavailable to clients of the module, even though the expression will only ever execute on the deployment target or higher.

Resolves rdar://92713589

* Tests: Update `attr_inlinable_available.swift` to require macOS and remove the OS versions for other platforms from availability attributes.

* Tests: Add test cases for potential unavailability in class inheritance when  is specified.

* Sema: Reword diagnostics about potentially unavailable decls to improve clarity for authors of API libraries. When decls are diagnosed as potentially unavailable on an OS earlier than the deployment target, the message will now indicate that the issue would be faced by clients of the module.

Resolves rdar://93466875

* Sema: Use the availability of the extended nominal as a floor for the availability of extensions. The primary motivation for this change is to reduce unnecessary availability diagnostics for API library authors. Many API libraries contain existing extension decls that lack declared availability where the extension introduces additional members to the extended type in the same release that the extended type was declared. Others contain extensions where the extension itself does not have declared availability but each of the members do. In both cases, the code is safe as written so the extra diagnostics would be a nuisance.

Resolves rdar://93630782

* Sema: Downgrade diagnostics about inheritance from a less available type when `-target-min-inlining-version min` is specified.

As a concession to source compatibility for API libraries, downgrade diagnostics about inheritance from a less available type when the following conditions are met:
1. The inherited type is only potentially unavailable before the deployment target.
2. The inheriting type is `@usableFromInline`.

Resolves rdar://92747826
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