Skip to content

Commit f6ec97d

Browse files
committed
SIL: Fix isPotentiallyAnyObject(), and move from AST to SIL
1 parent 5f2c49b commit f6ec97d

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

lib/AST/Type.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,20 +1118,6 @@ bool TypeBase::isAnyObject() {
11181118
return canTy.getExistentialLayout().isAnyObject();
11191119
}
11201120

1121-
// Distinguish between class-bound types that might be AnyObject vs other
1122-
// class-bound types. Only types that are potentially AnyObject might have a
1123-
// transparent runtime type wrapper like __SwiftValue. This must look through
1124-
// all optional types because dynamic casting sees through them.
1125-
bool TypeBase::isPotentiallyAnyObject() {
1126-
Type unwrappedTy = lookThroughAllOptionalTypes();
1127-
if (auto archetype = unwrappedTy->getAs<ArchetypeType>()) {
1128-
// Does archetype have any requirements that contradict AnyObject?
1129-
// 'T : AnyObject' requires a layout constraint, not a conformance.
1130-
return archetype->getConformsTo().empty() && !archetype->getSuperclass();
1131-
}
1132-
return unwrappedTy->isAnyObject();
1133-
}
1134-
11351121
bool ExistentialLayout::isErrorExistential() const {
11361122
auto protocols = getProtocols();
11371123
return (!hasExplicitAnyObject &&

lib/SIL/Utils/DynamicCasts.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,22 @@ static CanType getHashableExistentialType(ModuleDecl *M) {
225225
return hashable->getDeclaredInterfaceType()->getCanonicalType();
226226
}
227227

228+
// Distinguish between class-bound types that might be AnyObject vs other
229+
// class-bound types. Only types that are potentially AnyObject might have a
230+
// transparent runtime type wrapper like __SwiftValue. This must look through
231+
// all optional types because dynamic casting sees through them.
232+
static bool isPotentiallyAnyObject(Type type) {
233+
Type unwrappedTy = type->lookThroughAllOptionalTypes();
234+
if (auto archetype = unwrappedTy->getAs<ArchetypeType>()) {
235+
for (auto *proto : archetype->getConformsTo()) {
236+
if (!proto->getInvertibleProtocolKind())
237+
return false;
238+
}
239+
return !archetype->getSuperclass();
240+
}
241+
return unwrappedTy->isAnyObject();
242+
}
243+
228244
// Returns true if casting \p sourceFormalType to \p targetFormalType preserves
229245
// ownership.
230246
//
@@ -321,11 +337,11 @@ bool swift::doesCastPreserveOwnershipForTypes(SILModule &module,
321337
return false;
322338

323339
// (B2) unwrapping
324-
if (sourceType->isPotentiallyAnyObject())
340+
if (isPotentiallyAnyObject(sourceType))
325341
return false;
326342

327343
// (B1) wrapping
328-
if (targetType->isPotentiallyAnyObject()) {
344+
if (isPotentiallyAnyObject(targetType)) {
329345
// A class type cannot be wrapped in __SwiftValue, so casting
330346
// from a class to AnyObject preserves ownership.
331347
return

test/SILGen/opaque_values_silgen.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-swift-emit-silgen -enable-sil-opaque-values -Xllvm -sil-full-demangle -primary-file %s | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-runtime
22

3-
// XFAIL: noncopyable_generics
4-
53
// Test SILGen -enable-sil-opaque-values with tests that depend on the stdlib.
64

75
// FIXME: "HECK" lines all need to be updated for OSSA.

test/SILOptimizer/rcidentity_opaque.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-sil-opt -rc-id-dumper -enable-sil-opaque-values -module-name Swift %s -o /dev/null | %FileCheck %s
22

3-
// XFAIL: noncopyable_generics
4-
53
import Builtin
64

75
typealias AnyObject = Builtin.AnyObject

0 commit comments

Comments
 (0)