Skip to content

Commit f4c3d8f

Browse files
authored
Merge pull request swiftlang#75966 from slavapestov/more-removal-of-root
Remove some usages of ArchetypeType::getRoot()
2 parents 9167694 + d2c0efe commit f4c3d8f

File tree

11 files changed

+47
-65
lines changed

11 files changed

+47
-65
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
752752
return getRecursiveProperties().hasParameterizedExistential();
753753
}
754754

755-
/// Determine whether the type involves the given opened existential
756-
/// archetype.
757-
bool hasOpenedExistentialWithRoot(const OpenedArchetypeType *root) const;
755+
/// Determine whether the type involves a local archetype from the given
756+
/// environment.
757+
bool hasLocalArchetypeFromEnvironment(GenericEnvironment *env) const;
758758

759759
/// Determine whether the type involves an opaque type.
760760
bool hasOpaqueArchetype() const {

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,8 +5972,8 @@ Type typeEraseOpenedExistentialReference(Type type, Type existentialBaseType,
59725972
/// Given a type that includes opened existential archetypes derived from
59735973
/// the given generic environment, replace the archetypes with their upper
59745974
/// bounds.
5975-
Type typeEraseOpenedArchetypesWithRoot(Type type,
5976-
const OpenedArchetypeType *root);
5975+
Type typeEraseOpenedArchetypesFromEnvironment(Type type,
5976+
GenericEnvironment *env);
59775977

59785978
/// Returns true if a reference to a member on a given base type will apply
59795979
/// its curried self parameter, assuming it has one.

lib/AST/Type.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,19 +520,14 @@ bool TypeBase::isSpecialized() {
520520
return false;
521521
}
522522

523-
bool TypeBase::hasOpenedExistentialWithRoot(
524-
const OpenedArchetypeType *root) const {
525-
assert(root->isRoot() && "Expected a root archetype");
526-
527-
if (!hasOpenedExistential())
523+
bool TypeBase::hasLocalArchetypeFromEnvironment(
524+
GenericEnvironment *env) const {
525+
if (!hasLocalArchetype())
528526
return false;
529527

530528
return getCanonicalType().findIf([&](Type type) -> bool {
531-
auto *opened = dyn_cast<OpenedArchetypeType>(type.getPointer());
532-
if (!opened)
533-
return false;
534-
535-
return opened->getRoot() == root;
529+
auto *local = dyn_cast<LocalArchetypeType>(type.getPointer());
530+
return local && local->getGenericEnvironment() == env;
536531
});
537532
}
538533

lib/AST/TypeSubstitution.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,14 +1123,8 @@ static bool canSubstituteTypeInto(Type ty, const DeclContext *dc,
11231123
bool isContextWholeModule) {
11241124
TypeDecl *typeDecl = ty->getAnyNominal();
11251125
if (!typeDecl) {
1126-
// The referenced type might be a different opaque result type.
1127-
1128-
// First, unwrap any nested associated types to get the root archetype.
1129-
if (auto nestedTy = ty->getAs<ArchetypeType>())
1130-
ty = nestedTy->getRoot();
1131-
1132-
// If the root archetype is an opaque result type, check that its
1133-
// descriptor is accessible.
1126+
// If the referenced type is a different opaque result type,
1127+
// check that its descriptor is accessible.
11341128
if (auto opaqueTy = ty->getAs<OpaqueTypeArchetypeType>())
11351129
typeDecl = opaqueTy->getDecl();
11361130
}

lib/IDE/CompletionLookup.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,13 @@ void CompletionLookup::foundFunction(const AnyFunctionType *AFT) {
259259

260260
bool CompletionLookup::canBeUsedAsRequirementFirstType(Type selfTy,
261261
TypeAliasDecl *TAD) {
262-
auto T = TAD->getDeclaredInterfaceType();
263-
auto subMap = selfTy->getMemberSubstitutionMap(TAD);
264-
T = T.subst(subMap)->getCanonicalType();
265-
266-
ArchetypeType *archeTy = T->getAs<ArchetypeType>();
267-
if (!archeTy)
262+
if (TAD->isGeneric())
268263
return false;
269-
archeTy = archeTy->getRoot();
270264

271-
// For protocol, the 'archeTy' should match with the 'baseTy' which is the
272-
// dynamic 'Self' type of the protocol. For nominal decls, 'archTy' should
273-
// be one of the generic params in 'selfTy'. Search 'archeTy' in 'baseTy'.
274-
return selfTy.findIf([&](Type T) { return archeTy->isEqual(T); });
265+
auto T = TAD->getDeclaredInterfaceType();
266+
auto subMap = selfTy->getContextSubstitutionMap(TAD->getDeclContext());
267+
268+
return T.subst(subMap)->is<ArchetypeType>();
275269
}
276270

277271
CompletionLookup::CompletionLookup(CodeCompletionResultSink &Sink,

lib/IRGen/GenType.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,11 +1981,8 @@ ArchetypeType *TypeConverter::getExemplarArchetype(ArchetypeType *t) {
19811981

19821982
assert(isa<PrimaryArchetypeType>(t) || isa<PackArchetypeType>(t));
19831983

1984-
// Get the root archetype.
1985-
auto root = t->getRoot();
1986-
19871984
// Retrieve the generic environment of the archetype.
1988-
auto genericEnv = root->getGenericEnvironment();
1985+
auto genericEnv = t->getGenericEnvironment();
19891986

19901987
// Dig out the canonical generic environment.
19911988
auto genericSig = genericEnv->getGenericSignature();
@@ -2815,11 +2812,10 @@ void IRGenFunction::setDynamicSelfMetadata(CanType selfClass,
28152812

28162813
#ifndef NDEBUG
28172814
bool TypeConverter::isExemplarArchetype(ArchetypeType *arch) const {
2818-
auto primary = arch->getRoot();
2819-
if (!isa<PrimaryArchetypeType>(primary) &&
2820-
!isa<PackArchetypeType>(primary))
2815+
if (!isa<PrimaryArchetypeType>(arch) &&
2816+
!isa<PackArchetypeType>(arch))
28212817
return true;
2822-
auto genericEnv = primary->getGenericEnvironment();
2818+
auto genericEnv = arch->getGenericEnvironment();
28232819

28242820
// Dig out the canonical generic environment.
28252821
auto genericSig = genericEnv->getGenericSignature();

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,9 @@ static bool canReplaceCopiedArg(FullApplySite Apply, SILValue Arg,
947947
static bool applyInvolvesOpenedArchetypeWithRoot(FullApplySite Apply,
948948
OpenedArchetypeType *RootOA,
949949
unsigned SkipArgIdx) {
950-
if (Apply.getType().getASTType()->hasOpenedExistentialWithRoot(RootOA)) {
950+
auto *env = RootOA->getGenericEnvironment();
951+
952+
if (Apply.getType().getASTType()->hasLocalArchetypeFromEnvironment(env)) {
951953
return true;
952954
}
953955

@@ -958,7 +960,7 @@ static bool applyInvolvesOpenedArchetypeWithRoot(FullApplySite Apply,
958960
if (Apply.getArgument(Idx)
959961
->getType()
960962
.getASTType()
961-
->hasOpenedExistentialWithRoot(RootOA)) {
963+
->hasLocalArchetypeFromEnvironment(env)) {
962964
return true;
963965
}
964966
}

lib/Sema/CSApply.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,12 @@ namespace {
956956
// If we had a return type of 'Self', erase it.
957957
Type resultTy;
958958
resultTy = cs.getType(result);
959-
if (resultTy->hasOpenedExistentialWithRoot(record.Archetype)) {
960-
Type erasedTy = constraints::typeEraseOpenedArchetypesWithRoot(
961-
resultTy, record.Archetype);
959+
960+
auto *env = record.Archetype->getGenericEnvironment();
961+
962+
if (resultTy->hasLocalArchetypeFromEnvironment(env)) {
963+
Type erasedTy = constraints::typeEraseOpenedArchetypesFromEnvironment(
964+
resultTy, env);
962965
auto range = result->getSourceRange();
963966
result = coerceToType(result, erasedTy, locator);
964967
// FIXME: Implement missing tuple-to-tuple conversion
@@ -1668,8 +1671,8 @@ namespace {
16681671
} else {
16691672
// Erase opened existentials from the type of the thunk; we're
16701673
// going to open the existential inside the thunk's body.
1671-
containerTy = constraints::typeEraseOpenedArchetypesWithRoot(
1672-
containerTy, knownOpened->second);
1674+
containerTy = constraints::typeEraseOpenedArchetypesFromEnvironment(
1675+
containerTy, knownOpened->second->getGenericEnvironment());
16731676
selfTy = containerTy;
16741677
}
16751678
}
@@ -1732,8 +1735,8 @@ namespace {
17321735
// If the base was an opened existential, erase the opened
17331736
// existential.
17341737
if (openedExistential) {
1735-
refType = constraints::typeEraseOpenedArchetypesWithRoot(
1736-
refType, baseTy->castTo<OpenedArchetypeType>());
1738+
refType = constraints::typeEraseOpenedArchetypesFromEnvironment(
1739+
refType, baseTy->castTo<OpenedArchetypeType>()->getGenericEnvironment());
17371740
}
17381741

17391742
return refType;
@@ -1958,8 +1961,8 @@ namespace {
19581961
getConstraintSystem().getConstraintLocator(memberLocator));
19591962
if (knownOpened != solution.OpenedExistentialTypes.end()) {
19601963
curryThunkTy =
1961-
constraints::typeEraseOpenedArchetypesWithRoot(
1962-
curryThunkTy, knownOpened->second)
1964+
constraints::typeEraseOpenedArchetypesFromEnvironment(
1965+
curryThunkTy, knownOpened->second->getGenericEnvironment())
19631966
->castTo<FunctionType>();
19641967
}
19651968
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,15 +2449,14 @@ Type constraints::typeEraseOpenedExistentialReference(
24492449
/*force=*/false);
24502450
}
24512451

2452-
Type constraints::typeEraseOpenedArchetypesWithRoot(
2453-
Type type, const OpenedArchetypeType *root) {
2454-
assert(root->isRoot() && "Expected a root archetype");
2452+
Type constraints::typeEraseOpenedArchetypesFromEnvironment(
2453+
Type type, GenericEnvironment *env) {
2454+
assert(env->getKind() == GenericEnvironment::Kind::OpenedExistential);
24552455

2456-
auto *env = root->getGenericEnvironment();
24572456
auto sig = env->getGenericSignature();
24582457

24592458
return typeEraseExistentialSelfReferences(
2460-
type, root->getExistentialType(), TypePosition::Covariant, sig,
2459+
type, env->getOpenedExistentialType(), TypePosition::Covariant, sig,
24612460
/*containsFn=*/[](Type t) {
24622461
return t->hasOpenedExistential();
24632462
},

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,10 +1007,9 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
10071007
return found->second;
10081008

10091009
if (auto archetypeType = dyn_cast<ArchetypeType>(origType)) {
1010-
auto root = archetypeType->getRoot();
1011-
// For other nested types, fail here so the default logic in subst()
1010+
// For nested types, fail here so the default logic in subst()
10121011
// for nested types applies.
1013-
if (root != archetypeType)
1012+
if (!archetypeType->getInterfaceType()->is<GenericTypeParamType>())
10141013
return Type();
10151014

10161015
auto locator = cs.getConstraintLocator({});

0 commit comments

Comments
 (0)