Skip to content

Commit 30b207e

Browse files
authored
Merge pull request #85418 from xedin/rdar-164267736
[AST/Serialization] A few fixes for `nonisolated(nonsending)` handling
2 parents e360e8c + 4780581 commit 30b207e

File tree

8 files changed

+72
-39
lines changed

8 files changed

+72
-39
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7031,9 +7031,6 @@ class ParamDecl : public VarDecl {
70317031

70327032
/// Whether or not this parameter is 'sending'.
70337033
IsSending = 1 << 4,
7034-
7035-
/// Whether or not this parameter is isolated to a caller.
7036-
IsCallerIsolated = 1 << 5,
70377034
};
70387035

70397036
/// The type repr and 3 bits used for flags.
@@ -7334,18 +7331,6 @@ class ParamDecl : public VarDecl {
73347331
removeFlag(Flag::IsSending);
73357332
}
73367333

7337-
/// Whether or not this parameter is marked with 'nonisolated(nonsending)'.
7338-
bool isCallerIsolated() const {
7339-
return getOptions().contains(Flag::IsCallerIsolated);
7340-
}
7341-
7342-
void setCallerIsolated(bool value = true) {
7343-
if (value)
7344-
addFlag(Flag::IsCallerIsolated);
7345-
else
7346-
removeFlag(Flag::IsCallerIsolated);
7347-
}
7348-
73497334
/// Whether or not this parameter is marked with '@_addressable'.
73507335
bool isAddressable() const {
73517336
return getOptions().contains(Flag::IsAddressable);

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ struct ShouldPrintChecker {
127127

128128
/// Type-printing options which should only be applied to the outermost
129129
/// type.
130-
enum class NonRecursivePrintOption: uint32_t {
130+
enum class NonRecursivePrintOption : uint32_t {
131131
/// Print `Optional<T>` as `T!`.
132132
ImplicitlyUnwrappedOptional = 1 << 0,
133+
/// Avoid printing `nonisolated(nonsending)` modifier.
134+
SkipNonisolatedNonsending = 1 << 1,
133135
};
134136
using NonRecursivePrintOptions = OptionSet<NonRecursivePrintOption>;
135137

lib/AST/ASTPrinter.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ static NonRecursivePrintOptions getNonRecursiveOptions(const ValueDecl *D) {
229229
NonRecursivePrintOptions options;
230230
if (D->isImplicitlyUnwrappedOptional())
231231
options |= NonRecursivePrintOption::ImplicitlyUnwrappedOptional;
232+
if (isa<ParamDecl>(D))
233+
options |= NonRecursivePrintOption::SkipNonisolatedNonsending;
232234
return options;
233235
}
234236

@@ -3895,6 +3897,12 @@ static bool isEscaping(Type type) {
38953897
return false;
38963898
}
38973899

3900+
static bool isNonisolatedCaller(Type type) {
3901+
if (auto *funcTy = type->getAs<AnyFunctionType>())
3902+
return funcTy->getIsolation().isNonIsolatedCaller();
3903+
return false;
3904+
}
3905+
38983906
static void printParameterFlags(ASTPrinter &printer,
38993907
const PrintOptions &options,
39003908
const ParamDecl *param,
@@ -4077,17 +4085,6 @@ void PrintAST::printOneParameter(const ParamDecl *param,
40774085

40784086
auto interfaceTy = param->getInterfaceType();
40794087

4080-
// If type of this parameter is isolated to a caller, let's
4081-
// strip the isolation from the type to avoid printing it as
4082-
// part of the function type because that would break ordering
4083-
// between specifiers and attributes.
4084-
if (param->isCallerIsolated()) {
4085-
if (auto *funcTy = dyn_cast<AnyFunctionType>(interfaceTy.getPointer())) {
4086-
interfaceTy =
4087-
funcTy->withIsolation(FunctionTypeIsolation::forNonIsolated());
4088-
}
4089-
}
4090-
40914088
TypeLoc TheTypeLoc;
40924089
if (auto *repr = param->getTypeRepr()) {
40934090
TheTypeLoc = TypeLoc(repr, interfaceTy);
@@ -4108,7 +4105,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
41084105
// be written explicitly in this position.
41094106
printParameterFlags(Printer, Options, param, paramFlags,
41104107
isEscaping(type) && !isEnumElement,
4111-
param->isCallerIsolated());
4108+
isNonisolatedCaller(interfaceTy));
41124109
}
41134110

41144111
printTypeLoc(TheTypeLoc, getNonRecursiveOptions(param));
@@ -6629,7 +6626,8 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
66296626
visit(staticSelfT);
66306627
}
66316628

6632-
void printFunctionExtInfo(AnyFunctionType *fnType) {
6629+
void printFunctionExtInfo(AnyFunctionType *fnType,
6630+
NonRecursivePrintOptions nrOptions) {
66336631
if (!fnType->hasExtInfo()) {
66346632
Printer << "@_NO_EXTINFO ";
66356633
return;
@@ -6686,6 +6684,9 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
66866684
break;
66876685

66886686
case FunctionTypeIsolation::Kind::NonIsolatedNonsending:
6687+
if (nrOptions & NonRecursivePrintOption::SkipNonisolatedNonsending)
6688+
break;
6689+
66896690
Printer << "nonisolated(nonsending) ";
66906691
break;
66916692
}
@@ -6938,7 +6939,7 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
69386939
Printer.printStructurePost(PrintStructureKind::FunctionType);
69396940
};
69406941

6941-
printFunctionExtInfo(T);
6942+
printFunctionExtInfo(T, nrOptions);
69426943

69436944
// If we're stripping argument labels from types, do it when printing.
69446945
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/ false,
@@ -6996,7 +6997,7 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
69966997
Printer.printStructurePost(PrintStructureKind::FunctionType);
69976998
};
69986999

6999-
printFunctionExtInfo(T);
7000+
printFunctionExtInfo(T, nrOptions);
70007001
printGenericSignature(T->getGenericSignature(),
70017002
PrintAST::PrintParams |
70027003
PrintAST::defaultGenericRequirementFlags(Options));

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9204,8 +9204,8 @@ void ParamDecl::setTypeRepr(TypeRepr *repr) {
92049204

92059205
if (auto *callerIsolated =
92069206
dyn_cast<CallerIsolatedTypeRepr>(unwrappedType)) {
9207-
setCallerIsolated(true);
92089207
unwrappedType = callerIsolated->getBase();
9208+
continue;
92099209
}
92109210

92119211
break;

lib/Serialization/Deserialization.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,7 +4222,6 @@ class DeclDeserializer {
42224222
bool isIsolated;
42234223
bool isCompileTimeLiteral, isConstValue;
42244224
bool isSending;
4225-
bool isCallerIsolated;
42264225
bool isAddressable;
42274226
uint8_t rawDefaultArg;
42284227
TypeID defaultExprType;
@@ -4236,7 +4235,6 @@ class DeclDeserializer {
42364235
isCompileTimeLiteral,
42374236
isConstValue,
42384237
isSending,
4239-
isCallerIsolated,
42404238
isAddressable,
42414239
rawDefaultArg,
42424240
defaultExprType,
@@ -4283,7 +4281,6 @@ class DeclDeserializer {
42834281
param->setCompileTimeLiteral(isCompileTimeLiteral);
42844282
param->setConstValue(isConstValue);
42854283
param->setSending(isSending);
4286-
param->setCallerIsolated(isCallerIsolated);
42874284
param->setAddressable(isAddressable);
42884285

42894286
// Decode the default argument kind.

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 973; // @export attribute
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 974; // remove 'isCallerIsolated' bit from ParamDecl
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -1763,7 +1763,6 @@ namespace decls_block {
17631763
BCFixed<1>, // isCompileTimeLiteral?
17641764
BCFixed<1>, // isConst?
17651765
BCFixed<1>, // isSending?
1766-
BCFixed<1>, // isCallerIsolated?
17671766
BCFixed<1>, // isAddressable?
17681767
DefaultArgumentField, // default argument kind
17691768
TypeIDField, // default argument type

lib/Serialization/Serialization.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4808,7 +4808,6 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
48084808
param->isCompileTimeLiteral(),
48094809
param->isConstVal(),
48104810
param->isSending(),
4811-
param->isCallerIsolated(),
48124811
param->isAddressable(),
48134812
getRawStableDefaultArgumentKind(argKind),
48144813
S.addTypeRef(defaultExprType),

test/Concurrency/attr_execution/nonisolated_cross_module_with_flag_enabled.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// RUN: -emit-module-path %t/A.swiftmodule \
1111
// RUN: -emit-module-interface-path %t/A.swiftinterface
1212

13+
// RUN: %FileCheck %t/src/A.swift --input-file %t/A.swiftinterface
14+
1315
// RUN: %target-swift-typecheck-module-from-interface(%t/A.swiftinterface) -module-name A
1416

1517
// Build the client using module
@@ -22,9 +24,27 @@
2224

2325
//--- A.swift
2426
@MainActor
25-
2627
public final class Test {
28+
// CHECK: nonisolated(nonsending) final public func test() async
2729
public nonisolated func test() async {}
30+
31+
// CHECK: @_Concurrency.MainActor final public func compute(callback: nonisolated(nonsending) @escaping @Sendable () async -> Swift.Void)
32+
public func compute(callback: @escaping @Sendable () async -> Void) {}
33+
}
34+
35+
public struct InferenceTest {
36+
// CHECK: nonisolated(nonsending) public func infersAttr() async
37+
public func infersAttr() async {}
38+
39+
// CHECK: public func testNested(callback: @escaping (nonisolated(nonsending) @Sendable () async -> Swift.Void) -> Swift.Void)
40+
public func testNested(callback: @escaping (@Sendable () async -> Void) -> Void) {}
41+
// CHECK: public func testNested(dict: [Swift.String : (nonisolated(nonsending) () async -> Swift.Void)?])
42+
public func testNested(dict: [String: (() async -> Void)?]) {}
43+
44+
// CHECK: nonisolated(nonsending) public func testAutoclosure(value1 fn: nonisolated(nonsending) @autoclosure () async -> Swift.Int) async
45+
public func testAutoclosure(value1 fn: @autoclosure () async -> Int) async { await fn() }
46+
// CHECK: nonisolated(nonsending) public func testAutoclosure(value2 fn: nonisolated(nonsending) @autoclosure () async -> Swift.Int) async
47+
public func testAutoclosure(value2 fn: nonisolated(nonsending) @autoclosure () async -> Int) async { await fn() }
2848
}
2949

3050
//--- Client.swift
@@ -42,3 +62,33 @@ import A
4262
func test(t: Test) async {
4363
await t.test() // Ok
4464
}
65+
66+
// CHECK-LABEL: sil hidden @$s6Client16testWithCallback1ty1A4TestC_tYaF : $@convention(thin) @async (@guaranteed Test) -> ()
67+
// CHECK: function_ref @$s1A4TestC7compute8callbackyyyYaYbYCc_tF : $@convention(method) (@guaranteed @Sendable @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor) -> (), @guaranteed Test) -> ()
68+
// CHECK: } // end sil function '$s6Client16testWithCallback1ty1A4TestC_tYaF'
69+
@MainActor
70+
func testWithCallback(t: Test) async {
71+
t.compute(callback: {})
72+
}
73+
74+
// CHECK-LABEL: sil hidden @$s6Client13testInference1ty1A0C4TestV_tYaF : $@convention(thin) @async (@in_guaranteed InferenceTest) -> ()
75+
// CHECK: function_ref @$s1A13InferenceTestV10infersAttryyYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @in_guaranteed InferenceTest) -> ()
76+
// CHECK: function_ref @$s1A13InferenceTestV10testNested8callbackyyyyYaYbYCXEc_tF : $@convention(method) (@guaranteed @callee_guaranteed (@guaranteed @noescape @Sendable @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor) -> ()) -> (), @in_guaranteed InferenceTest) -> ()
77+
// CHECK: function_ref @$s1A13InferenceTestV10testNested4dictySDySSyyYaYCcSgG_tF : $@convention(method) (@guaranteed Dictionary<String, Optional<nonisolated(nonsending) () async -> ()>>, @in_guaranteed InferenceTest) -> ()
78+
// CHECK: } // end sil function '$s6Client13testInference1ty1A0C4TestV_tYaF'
79+
@MainActor
80+
func testInference(t: InferenceTest) async {
81+
await t.infersAttr()
82+
83+
t.testNested { _ in }
84+
t.testNested(dict: [:])
85+
}
86+
87+
// CHECK-LABEL: sil hidden @$s6Client15testAutoclosure1ty1A13InferenceTestV_tYaF : $@convention(thin) @async (@in_guaranteed InferenceTest) -> ()
88+
// CHECK: function_ref @$s1A13InferenceTestV15testAutoclosure6value1ySiyYaYCXK_tYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @guaranteed @noescape @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor) -> Int, @in_guaranteed InferenceTest) -> ()
89+
// CHECK: function_ref @$s1A13InferenceTestV15testAutoclosure6value2ySiyYaYCXK_tYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor, @guaranteed @noescape @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Builtin.ImplicitActor) -> Int, @in_guaranteed InferenceTest) -> ()
90+
// CHECK: } // end sil function '$s6Client15testAutoclosure1ty1A13InferenceTestV_tYaF'
91+
func testAutoclosure(t: InferenceTest) async {
92+
await t.testAutoclosure(value1: 42)
93+
await t.testAutoclosure(value2: 42)
94+
}

0 commit comments

Comments
 (0)