SIL: Fix isPotentiallyAnyObject(), and move from AST to SIL

This commit is contained in:
Slava Pestov
2024-02-28 16:58:10 -05:00
parent 5f2c49bd0e
commit f6ec97db80
4 changed files with 18 additions and 20 deletions

View File

@@ -1118,20 +1118,6 @@ bool TypeBase::isAnyObject() {
return canTy.getExistentialLayout().isAnyObject();
}
// Distinguish between class-bound types that might be AnyObject vs other
// class-bound types. Only types that are potentially AnyObject might have a
// transparent runtime type wrapper like __SwiftValue. This must look through
// all optional types because dynamic casting sees through them.
bool TypeBase::isPotentiallyAnyObject() {
Type unwrappedTy = lookThroughAllOptionalTypes();
if (auto archetype = unwrappedTy->getAs<ArchetypeType>()) {
// Does archetype have any requirements that contradict AnyObject?
// 'T : AnyObject' requires a layout constraint, not a conformance.
return archetype->getConformsTo().empty() && !archetype->getSuperclass();
}
return unwrappedTy->isAnyObject();
}
bool ExistentialLayout::isErrorExistential() const {
auto protocols = getProtocols();
return (!hasExplicitAnyObject &&

View File

@@ -225,6 +225,22 @@ static CanType getHashableExistentialType(ModuleDecl *M) {
return hashable->getDeclaredInterfaceType()->getCanonicalType();
}
// Distinguish between class-bound types that might be AnyObject vs other
// class-bound types. Only types that are potentially AnyObject might have a
// transparent runtime type wrapper like __SwiftValue. This must look through
// all optional types because dynamic casting sees through them.
static bool isPotentiallyAnyObject(Type type) {
Type unwrappedTy = type->lookThroughAllOptionalTypes();
if (auto archetype = unwrappedTy->getAs<ArchetypeType>()) {
for (auto *proto : archetype->getConformsTo()) {
if (!proto->getInvertibleProtocolKind())
return false;
}
return !archetype->getSuperclass();
}
return unwrappedTy->isAnyObject();
}
// Returns true if casting \p sourceFormalType to \p targetFormalType preserves
// ownership.
//
@@ -321,11 +337,11 @@ bool swift::doesCastPreserveOwnershipForTypes(SILModule &module,
return false;
// (B2) unwrapping
if (sourceType->isPotentiallyAnyObject())
if (isPotentiallyAnyObject(sourceType))
return false;
// (B1) wrapping
if (targetType->isPotentiallyAnyObject()) {
if (isPotentiallyAnyObject(targetType)) {
// A class type cannot be wrapped in __SwiftValue, so casting
// from a class to AnyObject preserves ownership.
return

View File

@@ -1,7 +1,5 @@
// 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
// XFAIL: noncopyable_generics
// Test SILGen -enable-sil-opaque-values with tests that depend on the stdlib.
// FIXME: "HECK" lines all need to be updated for OSSA.

View File

@@ -1,7 +1,5 @@
// RUN: %target-sil-opt -rc-id-dumper -enable-sil-opaque-values -module-name Swift %s -o /dev/null | %FileCheck %s
// XFAIL: noncopyable_generics
import Builtin
typealias AnyObject = Builtin.AnyObject