Skip to content

Commit d6f27a8

Browse files
committed
[Concurrency] Always print @preconcurrency instead of (unsafe) in
swiftinterfaces.
1 parent 9997802 commit d6f27a8

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

lib/AST/Attr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,8 +1329,6 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13291329
type.print(Printer, Options);
13301330
else
13311331
attr->getTypeRepr()->print(Printer, Options);
1332-
if (attr->isArgUnsafe() && Options.IsForSwiftInterface)
1333-
Printer << "(unsafe)";
13341332
Printer.printNamePost(PrintNameContext::Attribute);
13351333
break;
13361334
}

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4652,6 +4652,8 @@ static void checkDeclWithIsolatedParameter(ValueDecl *value) {
46524652

46534653
ActorIsolation ActorIsolationRequest::evaluate(
46544654
Evaluator &evaluator, ValueDecl *value) const {
4655+
auto &ctx = value->getASTContext();
4656+
46554657
// If this declaration has actor-isolated "self", it's isolated to that
46564658
// actor.
46574659
if (evaluateOrDefault(evaluator, HasIsolatedSelfRequest{value}, false)) {
@@ -4728,6 +4730,13 @@ ActorIsolation ActorIsolationRequest::evaluate(
47284730
};
47294731

47304732
auto isolationFromAttr = getIsolationFromAttributes(value);
4733+
if (isolationFromAttr && isolationFromAttr->preconcurrency() &&
4734+
!value->getAttrs().hasAttribute<PreconcurrencyAttr>()) {
4735+
auto preconcurrency =
4736+
new (ctx) PreconcurrencyAttr(/*isImplicit*/true);
4737+
value->getAttrs().add(preconcurrency);
4738+
}
4739+
47314740
if (FuncDecl *fd = dyn_cast<FuncDecl>(value)) {
47324741
// Main.main() and Main.$main are implicitly MainActor-protected.
47334742
// Any other isolation is an error.
@@ -4807,7 +4816,6 @@ ActorIsolation ActorIsolationRequest::evaluate(
48074816

48084817
// Add an implicit attribute to capture the actor isolation that was
48094818
// inferred, so that (e.g.) it will be printed and serialized.
4810-
ASTContext &ctx = value->getASTContext();
48114819
switch (inferred) {
48124820
case ActorIsolation::Nonisolated:
48134821
case ActorIsolation::NonisolatedUnsafe:
@@ -4847,6 +4855,13 @@ ActorIsolation ActorIsolationRequest::evaluate(
48474855
if (inferred == ActorIsolation::GlobalActorUnsafe)
48484856
attr->setArgIsUnsafe(true);
48494857
value->getAttrs().add(attr);
4858+
4859+
if (inferred.preconcurrency()) {
4860+
auto preconcurrency =
4861+
new (ctx) PreconcurrencyAttr(/*isImplicit*/true);
4862+
value->getAttrs().add(preconcurrency);
4863+
}
4864+
48504865
break;
48514866
}
48524867

test/ModuleInterface/concurrency.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ public func takesMainActor(
2525
_ block: @MainActor @escaping () -> Void
2626
) { }
2727

28+
@MainActor(unsafe)
29+
public protocol UnsafeMainProtocol {
30+
func requirement()
31+
}
32+
33+
public struct InferredUnsafeMainActor: UnsafeMainProtocol {
34+
public func requirement() {}
35+
}
36+
37+
@preconcurrency @MainActor
38+
public protocol PreconcurrencyMainProtocol {
39+
func requirement()
40+
}
41+
42+
public struct InferredPreconcurrencyMainActor: PreconcurrencyMainProtocol {
43+
public func requirement() {}
44+
}
45+
2846
// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency -I %t
2947

3048
#else
@@ -43,6 +61,23 @@ func callFn() async {
4361
// CHECK: public func takesSendable(_ block: @escaping @Sendable () async throws ->
4462
// CHECK: public func takesMainActor(_ block: @escaping @{{_Concurrency.MainActor|MainActor}} () ->
4563

64+
// CHECK: @_Concurrency.MainActor @preconcurrency public protocol UnsafeMainProtocol {
65+
// CHECK-NEXT: @_Concurrency.MainActor @preconcurrency func requirement()
66+
// CHECK-NEXT: }
67+
68+
// CHECK: @_Concurrency.MainActor @preconcurrency public struct InferredUnsafeMainActor :
69+
// CHECK-NEXT: @_Concurrency.MainActor @preconcurrency public func requirement()
70+
// CHECK-NEXT: }
71+
72+
// CHECK: @preconcurrency @_Concurrency.MainActor public protocol PreconcurrencyMainProtocol {
73+
// CHECK-NEXT: @_Concurrency.MainActor @preconcurrency func requirement()
74+
// CHECK-NEXT: }
75+
76+
// CHECK: @_Concurrency.MainActor @preconcurrency public struct InferredPreconcurrencyMainActor :
77+
// CHECK-NEXT: @_Concurrency.MainActor @preconcurrency public func requirement()
78+
// CHECK-NEXT: }
79+
80+
4681
// RUN: %target-swift-emit-module-interface(%t/LibraryPreserveTypesAsWritten.swiftinterface) %s -enable-experimental-concurrency -DLIBRARY -module-name LibraryPreserveTypesAsWritten -module-interface-preserve-types-as-written
4782
// RUN: %target-swift-typecheck-module-from-interface(%t/LibraryPreserveTypesAsWritten.swiftinterface) -enable-experimental-concurrency
4883
// RUN: %FileCheck %s < %t/LibraryPreserveTypesAsWritten.swiftinterface

0 commit comments

Comments
 (0)