Skip to content

Commit 2a75075

Browse files
authored
Merge pull request #73158 from angela-laar/any-to-any-sendable-override
[Sema] Warn about 'Any' to 'any Sendable' override
2 parents b068398 + f6404cd commit 2a75075

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

lib/AST/Type.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,26 @@ unsigned int TypeBase::getOptionalityDepth() {
898898
}
899899

900900
Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
901+
902+
if (auto *arrayTy = dyn_cast<ArraySliceType>(this)) {
903+
auto newBaseTy =
904+
arrayTy->getBaseType()->stripConcurrency(recurse, dropGlobalActor);
905+
return newBaseTy->isEqual(arrayTy->getBaseType())
906+
? Type(this)
907+
: ArraySliceType::get(newBaseTy);
908+
}
909+
910+
if (auto *dictTy = dyn_cast<DictionaryType>(this)) {
911+
auto keyTy = dictTy->getKeyType();
912+
auto strippedKeyTy = keyTy->stripConcurrency(recurse, dropGlobalActor);
913+
auto valueTy = dictTy->getValueType();
914+
auto strippedValueTy = valueTy->stripConcurrency(recurse, dropGlobalActor);
915+
916+
return keyTy->isEqual(strippedKeyTy) && valueTy->isEqual(strippedValueTy)
917+
? Type(this)
918+
: DictionaryType::get(strippedKeyTy, strippedValueTy);
919+
}
920+
901921
// Look through optionals.
902922
if (Type optionalObject = getOptionalObjectType()) {
903923
Type newOptionalObject =
@@ -1083,25 +1103,6 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
10831103
return anyChanged ? TupleType::get(elts, getASTContext()) : Type(this);
10841104
}
10851105

1086-
if (auto *arrayTy = dyn_cast<ArraySliceType>(this)) {
1087-
auto newBaseTy =
1088-
arrayTy->getBaseType()->stripConcurrency(recurse, dropGlobalActor);
1089-
return newBaseTy->isEqual(arrayTy->getBaseType())
1090-
? Type(this)
1091-
: ArraySliceType::get(newBaseTy);
1092-
}
1093-
1094-
if (auto *dictTy = dyn_cast<DictionaryType>(this)) {
1095-
auto keyTy = dictTy->getKeyType();
1096-
auto strippedKeyTy = keyTy->stripConcurrency(recurse, dropGlobalActor);
1097-
auto valueTy = dictTy->getValueType();
1098-
auto strippedValueTy = valueTy->stripConcurrency(recurse, dropGlobalActor);
1099-
1100-
return keyTy->isEqual(strippedKeyTy) && valueTy->isEqual(strippedValueTy)
1101-
? Type(this)
1102-
: DictionaryType::get(strippedKeyTy, strippedValueTy);
1103-
}
1104-
11051106
return Type(this);
11061107
}
11071108

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,9 +1260,16 @@ bool OverrideMatcher::checkOverride(ValueDecl *baseDecl,
12601260
auto propertyTy = property->getInterfaceType();
12611261
auto parentPropertyTy = getSuperMemberDeclType(baseDecl);
12621262

1263+
// If @preconcurrency, strip concurrency from decl before matching
1264+
if (baseDecl->preconcurrency() && !baseDecl->isObjC()){
1265+
attempt = OverrideCheckingAttempt::MismatchedSendability;
1266+
propertyTy = propertyTy->stripConcurrency(true, true);
1267+
parentPropertyTy = parentPropertyTy->stripConcurrency(true, true);
1268+
}
12631269
CanType parentPropertyCanTy =
12641270
parentPropertyTy->getReducedType(
12651271
decl->getInnermostDeclContext()->getGenericSignatureOfContext());
1272+
12661273
if (!propertyTy->matches(parentPropertyCanTy,
12671274
TypeMatchFlags::AllowOverride)) {
12681275
diags.diagnose(property, diag::override_property_type_mismatch,
@@ -1282,6 +1289,7 @@ bool OverrideMatcher::checkOverride(ValueDecl *baseDecl,
12821289

12831290
// The overridden property must not be mutable.
12841291
if (cast<AbstractStorageDecl>(baseDecl)->supportsMutation() &&
1292+
attempt != OverrideCheckingAttempt::MismatchedSendability &&
12851293
!IsSilentDifference) {
12861294
diags.diagnose(property, diag::override_mutable_covariant_property,
12871295
property->getName(), parentPropertyTy, propertyTy);

test/Concurrency/predates_concurrency.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,20 @@ extension MainActorPreconcurrency: NotIsolated {
252252
}
253253
}
254254
}
255+
256+
open class Parent {
257+
init(userInfo: [String : Any]) {
258+
self.userInfo = userInfo
259+
}
260+
261+
@preconcurrency open var userInfo : [String : any Sendable] // expected-note {{overridden declaration is here}}
262+
}
263+
264+
class Child : Parent {
265+
override var userInfo: [String : Any] { // expected-warning {{declaration 'userInfo' has a type with different sendability from any potential overrides; this is an error in the Swift 6 language mode}}
266+
get {
267+
[:]
268+
}
269+
set {}
270+
}
271+
}

0 commit comments

Comments
 (0)