Skip to content

Align isolated conformances with the current proposal #79983

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
merged 10 commits into from
Mar 14, 2025

Conversation

DougGregor
Copy link
Member

Make several changes to bring the isolated conformances implementation into alignment with the latest proposal:

  • Use global-actor syntax to specify isolation conformances, e.g., @MainActor P, rather than isolated P
  • Allow conformance isolation to differ from that of the conforming type
  • Infer @MainActor on conformances when SE-0466 is enabled, which can be suppressed by nonisolated
  • Test printing of conformance isolation

Instead of using the `isolated P` syntax, switch to specifying the
global actor type directly, e.g.,

   class MyClass: @mainactor MyProto { ... }

No functionality change at this point
Update diagnostic and Fix-It to suggest global actor isolation on a
conformance (e.g, `@MainActor P`) rather than `isolated`.
…e type's

With the move to explicitly specifying the global actor for an isolated
conformance, we can now have conformances whose isolation differs from
that of the type, including having actors with global-actor-isolated
conformances. Introduce this generalization to match the proposal, and
update/add tests accordingly.
It doesn't mean anything yet, but parse it and make sure it reproduces
in the module interface.
The NormalProtocolConformance APIs for checking for an explicitly-written
isolation on a conformance were easy to get to, and the real semantic
API was buried in the type checker, leading to some unprincipled
checking. Instead, create a central ProtocolConformance::getIsolation()
to get the (semantic) actor isolation, and let that be the only place
that will access the explicitly-written global actor isolation for a
conformance. Update all call sites appropriately.
…eansMainActorIsolated

When code in the current module defaults to main actor (under SE-0466),
also infer main-actor isolation for protocol conformances of main-actor
isolated types.
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#3015

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#3015

@swift-ci please build toolchain

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#3015

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#3015

@swift-ci please build toolchain Windows

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Windows

SourceLoc globalActorAtLoc;

/// The global actor type to which this conformance is isolated.
TypeExpr *globalActorType = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this ought to be a TypeLoc?

Copy link
Member Author

Choose a reason for hiding this comment

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

It could. We get a TypeExpr out of the CustomAttr itself, so we can either stick that into a TypeLoc or keep it as-is. This way it's only a single pointer in ConformanceAttributes vs. the two-pointer TypeLoc, so I didn't do it.

bool ConformanceIsolationRequest::isCached() const {
// We only want to cache for global-actor-isolated conformances. For
// everything else, which is nearly every conformance, this request quickly
// returns "nonisolated" so there is no point in caching it.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is what split caching is for

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh yes, so it is! Thanks for the reminder---I don't think I've used this feature before.

@@ -546,6 +555,9 @@ class NormalProtocolConformance : public RootProtocolConformance,
/// NominalTypeDecl that declared the conformance.
DeclContext *Context;

/// The global actor isolation for this conformance, if there is one.
TypeExpr *globalActorIsolation = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this null most of the time? You can use a single bit to record the presence of this attribute, while storing the actual value in the request evaluator cache only when present.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's NULL most of the time. We could have a put and put it an ASTContext-managed side table, since it needs to be there at the point when the NormalProtocolConformance is created.

@DougGregor DougGregor force-pushed the isolated-conformances-alignment branch from 836fcbe to a1132ca Compare March 13, 2025 23:43
@DougGregor
Copy link
Member Author

Nnnnoooooooooo!!!! I force-pushed the wrong branch. Now it is time for me to go cry

Switch over to split caching for the conformance isolation request,
which optimizes for the common case where the conformance is
nonisolated. Also put the explicit global actor TypeExpr* in an
ASTContext side table, so we don't take a pointer's worth of storage
in every conformance.

For that side table, introduce a new "ASTContext::GlobalCache" that's
there only for side tables, so we don't have to go add get/set
operations to ASTContext and recompile the world every time we want to
add a side table like this.

Thanks, Slava!
@DougGregor DougGregor force-pushed the isolated-conformances-alignment branch from a1132ca to c7fd48a Compare March 14, 2025 00:41
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor enabled auto-merge March 14, 2025 00:41
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit 4cc6411 into swiftlang:main Mar 14, 2025
3 checks passed
@DougGregor DougGregor deleted the isolated-conformances-alignment branch March 14, 2025 17:34
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