Skip to content

Commit f6dba90

Browse files
authored
Merge pull request #70412 from xedin/rdar-113212472
[Concurrency] Teach `Type::stripConcurrency` about tuples, sugared types, and bound generic types
2 parents 69d7551 + bbd182b commit f6dba90

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/AST/Type.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,61 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
11101110
return Type(this);
11111111
}
11121112

1113+
if (auto *BGT = getAs<BoundGenericType>()) {
1114+
if (!recurse)
1115+
return Type(this);
1116+
1117+
bool anyChanged = false;
1118+
SmallVector<Type, 2> genericArgs;
1119+
llvm::transform(BGT->getGenericArgs(), std::back_inserter(genericArgs),
1120+
[&](Type argTy) {
1121+
auto newArgTy =
1122+
argTy->stripConcurrency(recurse, dropGlobalActor);
1123+
anyChanged |= !newArgTy->isEqual(argTy);
1124+
return newArgTy;
1125+
});
1126+
1127+
return anyChanged ? BoundGenericType::get(BGT->getDecl(), BGT->getParent(),
1128+
genericArgs)
1129+
: Type(this);
1130+
}
1131+
1132+
if (auto *tuple = getAs<TupleType>()) {
1133+
if (!recurse)
1134+
return Type(this);
1135+
1136+
bool anyChanged = false;
1137+
SmallVector<TupleTypeElt, 2> elts;
1138+
llvm::transform(
1139+
tuple->getElements(), std::back_inserter(elts), [&](const auto &elt) {
1140+
auto eltTy = elt.getType();
1141+
auto strippedTy = eltTy->stripConcurrency(recurse, dropGlobalActor);
1142+
anyChanged |= !strippedTy->isEqual(eltTy);
1143+
return elt.getWithType(strippedTy);
1144+
});
1145+
1146+
return anyChanged ? TupleType::get(elts, getASTContext()) : Type(this);
1147+
}
1148+
1149+
if (auto *arrayTy = dyn_cast<ArraySliceType>(this)) {
1150+
auto newBaseTy =
1151+
arrayTy->getBaseType()->stripConcurrency(recurse, dropGlobalActor);
1152+
return newBaseTy->isEqual(arrayTy->getBaseType())
1153+
? Type(this)
1154+
: ArraySliceType::get(newBaseTy);
1155+
}
1156+
1157+
if (auto *dictTy = dyn_cast<DictionaryType>(this)) {
1158+
auto keyTy = dictTy->getKeyType();
1159+
auto strippedKeyTy = keyTy->stripConcurrency(recurse, dropGlobalActor);
1160+
auto valueTy = dictTy->getValueType();
1161+
auto strippedValueTy = valueTy->stripConcurrency(recurse, dropGlobalActor);
1162+
1163+
return keyTy->isEqual(strippedKeyTy) && valueTy->isEqual(strippedValueTy)
1164+
? Type(this)
1165+
: DictionaryType::get(strippedKeyTy, strippedValueTy);
1166+
}
1167+
11131168
return Type(this);
11141169
}
11151170

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %target-swift-emit-silgen %s -verify -swift-version 5 | %FileCheck %s
2+
3+
// REQUIRES: concurrency
4+
5+
// Test erasure in generic arguments
6+
do {
7+
struct S<T> {
8+
}
9+
10+
@preconcurrency func test(_: S<any Sendable>) {}
11+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure4testL_yyAA1SL_VyypGF
12+
@preconcurrency func test(_: S<Array<@Sendable () -> Void>>) {}
13+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure4testL0_yyAA1SL_VySayyycGGF
14+
@preconcurrency func test(_: Array<(any Sendable, @Sendable () -> Void)>) {}
15+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure4testL1_yySayyp_yyctGF
16+
}
17+
18+
// Test erase in sugared types
19+
do {
20+
@preconcurrency func testArray1(_: [any Sendable]) {}
21+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure10testArray1L2_yySayypGF
22+
@preconcurrency func testArray2(_: [@Sendable () -> Void]) {}
23+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure10testArray2L2_yySayyycGF
24+
25+
@preconcurrency func testDictionary(_: [Int: (any Sendable, (String, @Sendable (any Sendable) -> Void))]) {}
26+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure14testDictionaryL2_yySDySiyp_SS_yypcttGF
27+
28+
@preconcurrency func testDesugaredDict(_: Dictionary<Int, (any Sendable, (String, @Sendable (any Sendable) -> Void))>) {}
29+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure17testDesugaredDictL2_yySDySiyp_SS_yypcttGF
30+
31+
@preconcurrency func testVariadic(_: (any Sendable)...) {}
32+
// CHECK-LABEL: sil private [ossa] @$s31sendable_preconcurrency_erasure12testVariadicL2_yyypd_tF
33+
}
34+
35+
public struct Data {
36+
@preconcurrency var test: (any Sendable, Array<(Int, any Sendable)>)? = nil
37+
// CHECK-LABEL: sil [transparent] [ossa] @$s31sendable_preconcurrency_erasure4DataV4testyp_SaySi_yptGtSgvpfi
38+
}

0 commit comments

Comments
 (0)