Skip to content

Sema: Ban global actor on getters in a future lang mode, and more #80399

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Mar 31, 2025

  1. Global actor attributes were hitherto allowed on getters and (until Swift 6) other accessors, but intendedly ignored in favor of the enclosing var or subscript declaration's isolation when accessing the storage declaration. This change deprecates the getter case as well, to become an error in the next language mode.

    Also do a better job at suggesting to move the attribute to the storage:

    • Tighten up the condition for when the attr is legal on the storage.
    • Don't suggest if the storage already specifies some kind of explicit isolation.
    • Highlight the attr.
    • Also remove the attr from the accessor.
  2. Accesses to storage declarations are checked relative to the storage's isolation, not the accessor's, whereas accessors are checked relative to their own isolation. This inconsistency exposes a data race safety hole because global actor attributes are allowed on accessors in Swift 5 and even in Swift 6 on getters.

    This fixes the bug by inferring an accessor's actor isolation from its from its storage declaration in Swift 6 and onward.

    Expected source compatibility impact is negligible. First, an accessor with a global actor attribute is not a common pattern. Second, this change will only affect clients that depend on resilient dynamic libraries that declare such an accessor as inlinable.

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

@AnthonyLatsis AnthonyLatsis force-pushed the danaus-plexippus-5 branch 2 times, most recently from bd74bc8 to d587460 Compare April 15, 2025 21:41
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

1 similar comment
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

} else if (auto *accessor = dyn_cast<AccessorDecl>(decl)) {
auto &ctx = accessor->getASTContext();
const auto attrRange = globalActorAttr->getRangeWithAt();
const unsigned langModeForError = accessor->isGetter() ? 7 : 6;
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI we should replace 7 with Version::getFutureMajorLanguageVersion here once #80853 lands (doesn't have to block this PR though)

Global actor attributes were hitherto allowed on getters and (until
Swift 6) other accessors, but intendedly ignored in favor of the
enclosing `var` or `subscript` declaration's isolation when accessing
the storage declaration. This change deprecates the getter case as well,
to become an error in the next language mode.

Also do a better job at suggesting to move the attribute to the storage:
* Tighten up the condition for when the attr is legal on the storage.
* Don't suggest if the storage already specifies some kind of explicit
  isolation.
* Highlight the attr.
* Also remove the attr from the accessor.
Accesses to storage declarations are checked relative to the storage's
isolation, not the accessor's, whereas accessors are checked relative to
their own isolation. This inconsistency exposes a data race safety hole
because global actor attributes are allowed on accessors in Swift 5 and
even in Swift 6 on getters.

This fixes the bug by inferring an accessor's actor isolation from its
from its storage declaration in Swift 6 and onward.

Expected source compatibility impact is negligible. First, an accessor
with a global actor attribute is not a common pattern. Second, this
change will only affect clients that depend on resilient dynamic
libraries that declare such an accessor as inlinable.
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

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.

2 participants