[Sema] fixed global actor annotations on class base types in inheritance clauses #90085
Open
egekaya1 wants to merge 10 commits into
Open
[Sema] fixed global actor annotations on class base types in inheritance clauses #90085egekaya1 wants to merge 10 commits into
egekaya1 wants to merge 10 commits into
Conversation
Add a new diagnostic for applying a global actor attribute to a class base type and update type checking to reject such annotations. Introduces diag::global_actor_on_class_inheritance_clause in DiagnosticsSema.def, adds a check in TypeResolver::resolveAttributedType to emit the diagnostic (with a fix-it removing the attribute) and produce an ErrorType, and adds a test (test/Concurrency/global_actor_class_inheritance.swift) that verifies class base annotations are rejected while protocol conformances remain allowed (per SE-0466).
The previous commit introduced a new test file without lit 'RUN' directives, causing the test runner to mark it as Unresolved. This adds the necessary %target-typecheck-verify-swift lines to validate diagnostics for both targeted and complete concurrency models.
Make the global-actor-on-class-base diagnostic more specific by including the actor and the offending non-protocol type. Updated DiagnosticsSema.def message to take two Type parameters and changed TypeCheckType.cpp to capture the resolved global actor type and forward it to diagnose(). Adjusted tests to expect the new, more descriptive error text.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Old PR #88210 disappeared so I'm reopening it here.
Global actor annotations like @mainactor are valid in inheritance clauses
when applied to protocol conformances, this is the isolated conformances
feature (SE-0466). However, they have no meaning when applied to a class
base type and were previously accepted silently:
class Derived: @mainactor Base {} // was accepted, now an error
class Derived: @mainactor P {} // valid (SE-0466), unchanged
The annotation is parsed because the same syntax is used for isolated
conformances, but the type resolver in resolveAttributedType never
validated whether the annotated inherited type was a class or a protocol.
This fix adds that validation immediately after resolveGlobalActor claims
the attribute. If the resolved type is not a constraint type (i.e. it is a
class base type, not a protocol or protocol composition), the compiler now
emits an error with a fix-it to remove the annotation.
Resolves #86693
Explanation:
This change introduces a diagnostic error to prevent the application of global actor attributes to class base types in inheritance clauses. While SE-0466 allows these attributes for protocol conformances (isolated conformances), they are semantically meaningless when applied to a class base type. This PR ensures the compiler rejects such syntax with a helpful diagnostic and a Fix-it.
Scope:
Narrow. The change is confined to the type resolution logic (resolveAttributedType) in Semantic Analysis. It only affects how the compiler handles attributes in inheritance clauses.
Issues:
Resolves #86693.
Original PRs:
#88210
Risk:
Low. This is a diagnostic-only change. It does not affect code generation or valid existing code. It simply turns a previously silent, meaningless syntax into a clear compile-time error.
Testing:
Added a new test file test/Concurrency/global_actor_class_inheritance.swift which validates the diagnostic across both -strict-concurrency=targeted and -strict-concurrency=complete modes. Verified that valid SE-0466 isolated conformances remain unaffected.
Reviewers:
Alastair Houghton (@al45tair). Review requested from @hborla, @slavapestov, and @xedin.