Skip to content

[Sema] fixed global actor annotations on class base types in inheritance clauses #90085

Open
egekaya1 wants to merge 10 commits into
swiftlang:mainfrom
egekaya1:main
Open

[Sema] fixed global actor annotations on class base types in inheritance clauses #90085
egekaya1 wants to merge 10 commits into
swiftlang:mainfrom
egekaya1:main

Conversation

@egekaya1

Copy link
Copy Markdown

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.

egekaya1 and others added 9 commits March 31, 2026 17:22
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.
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.

global actor annotations can be applied to inheritance clauses

1 participant