Skip to content

Commit f5e2505

Browse files
committed
AST: Merge GenericSignature::get{Non,}DependentUpperBounds()
1 parent c298997 commit f5e2505

File tree

4 files changed

+24
-54
lines changed

4 files changed

+24
-54
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
362362
/// Determine whether the given dependent type is required to be a class.
363363
bool requiresClass(Type type) const;
364364

365-
Type getUpperBound(Type type, bool wantDependentUpperBound = false) const;
366-
367365
/// Determine the superclass bound on the given dependent type.
368366
Type getSuperclassBound(Type type) const;
369367

@@ -457,19 +455,12 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
457455
/// generic parameter types by their sugared form.
458456
Type getSugaredType(Type type) const;
459457

460-
/// Given a type parameter, compute the most specific supertype (upper bound)
461-
/// that is not dependent on other type parameters.
462-
///
463-
/// \note If the upper bound is a protocol or protocol composition,
464-
/// will return an instance of \c ExistentialType.
465-
Type getNonDependentUpperBounds(Type type) const;
466-
467-
/// Given a type parameter, compute the most specific supertype (upper bound)
468-
/// that is possibly dependent on other type parameters.
458+
/// Given a type parameter, compute the most specific supertype (upper bound),
459+
/// possibly dependent on other type parameters.
469460
///
470461
/// \note If the upper bound is a protocol or protocol composition,
471462
/// will return an instance of \c ExistentialType.
472-
Type getDependentUpperBounds(Type type) const;
463+
Type getUpperBound(Type type) const;
473464

474465
static void Profile(llvm::FoldingSetNodeID &ID,
475466
ArrayRef<GenericTypeParamType *> genericParams,

lib/AST/GenericSignature.cpp

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -635,26 +635,23 @@ unsigned GenericSignatureImpl::getGenericParamOrdinal(
635635
return GenericParamKey(param).findIndexIn(getGenericParams());
636636
}
637637

638-
Type GenericSignatureImpl::getNonDependentUpperBounds(Type type) const {
639-
return getUpperBound(type);
640-
}
641-
642-
Type GenericSignatureImpl::getDependentUpperBounds(Type type) const {
643-
return getUpperBound(type, /*wantDependentBound=*/true);
644-
}
645-
646-
Type GenericSignatureImpl::getUpperBound(Type type,
647-
bool wantDependentBound) const {
638+
Type GenericSignatureImpl::getUpperBound(Type type) const {
648639
assert(type->isTypeParameter());
649640

650-
bool hasExplicitAnyObject = requiresClass(type);
651-
652641
llvm::SmallVector<Type, 2> types;
642+
unsigned rootDepth = type->getRootGenericParam()->getDepth();
643+
644+
bool hasExplicitAnyObject = requiresClass(type);
653645

654646
if (Type superclass = getSuperclassBound(type)) {
655647
do {
656648
superclass = getReducedType(superclass);
657-
if (wantDependentBound || !superclass->hasTypeParameter()) {
649+
650+
if (!superclass.findIf([&](Type t) {
651+
if (auto *paramTy = t->getAs<GenericTypeParamType>())
652+
return (paramTy->getDepth() == rootDepth);
653+
return false;
654+
})) {
658655
break;
659656
}
660657
} while ((superclass = superclass->getSuperclass()));
@@ -684,31 +681,13 @@ Type GenericSignatureImpl::getUpperBound(Type type,
684681

685682
// If the reduced type is at a lower depth than the root generic
686683
// parameter of T, then it's constrained.
687-
bool hasOuterGenericParam = false;
688-
bool hasInnerGenericParam = false;
689-
reducedType.visit([&](Type t) {
690-
if (auto *paramTy = t->getAs<GenericTypeParamType>()) {
691-
unsigned rootDepth = type->getRootGenericParam()->getDepth();
692-
if (paramTy->getDepth() == rootDepth)
693-
hasInnerGenericParam = true;
694-
else {
695-
assert(paramTy->getDepth() < rootDepth);
696-
hasOuterGenericParam = true;
697-
}
698-
}
699-
});
700-
701-
if (hasInnerGenericParam && hasOuterGenericParam) {
702-
llvm::errs() << "Weird same-type requirements?\n";
703-
llvm::errs() << "Interface type: " << type << "\n";
704-
llvm::errs() << "Member type: " << memberType << "\n";
705-
llvm::errs() << "Reduced member type: " << reducedType << "\n";
706-
llvm::errs() << GenericSignature(this) << "\n";
707-
abort();
708-
}
709-
710-
if (!hasInnerGenericParam && (wantDependentBound || !hasOuterGenericParam))
684+
if (!reducedType.findIf([&](Type t) {
685+
if (auto *paramTy = t->getAs<GenericTypeParamType>())
686+
return (paramTy->getDepth() == rootDepth);
687+
return false;
688+
})) {
711689
argTypes.push_back(reducedType);
690+
}
712691
}
713692

714693
// If we have constrained all primary associated types, create a
@@ -721,7 +700,8 @@ Type GenericSignatureImpl::getUpperBound(Type type,
721700
//
722701
// In that case just add the base type in the default branch below.
723702
if (argTypes.size() == primaryAssocTypes.size()) {
724-
types.push_back(ParameterizedProtocolType::get(getASTContext(), baseType, argTypes));
703+
types.push_back(ParameterizedProtocolType::get(
704+
getASTContext(), baseType, argTypes));
725705
continue;
726706
}
727707
}

lib/AST/Type.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,7 @@ Type TypeBase::typeEraseOpenedArchetypesWithRoot(
673673
if (root->isEqual(archetype)) {
674674
erasedTy = root->getExistentialType();
675675
} else {
676-
erasedTy =
677-
sig->getNonDependentUpperBounds(archetype->getInterfaceType());
676+
erasedTy = sig->getUpperBound(archetype->getInterfaceType());
678677
}
679678

680679
if (metatypeDepth) {
@@ -3687,7 +3686,7 @@ Type ArchetypeType::getExistentialType() const {
36873686
auto interfaceType = getInterfaceType();
36883687
auto genericSig = genericEnv->getGenericSignature();
36893688

3690-
auto upperBound = genericSig->getDependentUpperBounds(interfaceType);
3689+
auto upperBound = genericSig->getUpperBound(interfaceType);
36913690

36923691
return genericEnv->mapTypeIntoContext(upperBound);
36933692
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@ static Type typeEraseExistentialSelfReferences(Type refTy, Type baseTy,
22322232
if (t->is<GenericTypeParamType>()) {
22332233
erasedTy = baseTy;
22342234
} else {
2235-
erasedTy = existentialSig->getNonDependentUpperBounds(t);
2235+
erasedTy = existentialSig->getUpperBound(t);
22362236
}
22372237

22382238
if (metatypeDepth) {

0 commit comments

Comments
 (0)