Skip to content

Commit 98567e8

Browse files
committed
AssociatedTypeInference: Delete old code for computing abstract type witnesses
1 parent 789f90d commit 98567e8

File tree

2 files changed

+5
-161
lines changed

2 files changed

+5
-161
lines changed

lib/Sema/TypeCheckProtocol.h

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -100,61 +100,6 @@ CheckTypeWitnessResult checkTypeWitness(Type type,
100100
const NormalProtocolConformance *Conf,
101101
SubstOptions options = None);
102102

103-
/// Describes the means of inferring an abstract type witness.
104-
enum class AbstractTypeWitnessKind : uint8_t {
105-
/// The type witness was inferred via a same-type-to-concrete constraint
106-
/// in a protocol requirement signature.
107-
Fixed,
108-
109-
/// The type witness was inferred via a defaulted associated type.
110-
Default,
111-
112-
/// The type witness was inferred to a generic parameter of the
113-
/// conforming type.
114-
GenericParam,
115-
};
116-
117-
/// A type witness inferred without the aid of a specific potential
118-
/// value witness.
119-
class AbstractTypeWitness {
120-
AbstractTypeWitnessKind Kind;
121-
AssociatedTypeDecl *AssocType;
122-
Type TheType;
123-
124-
/// When this is a default type witness, the declaration responsible for it.
125-
/// May not necessarilly match \c AssocType.
126-
AssociatedTypeDecl *DefaultedAssocType;
127-
128-
AbstractTypeWitness(AbstractTypeWitnessKind Kind,
129-
AssociatedTypeDecl *AssocType, Type TheType,
130-
AssociatedTypeDecl *DefaultedAssocType)
131-
: Kind(Kind), AssocType(AssocType), TheType(TheType),
132-
DefaultedAssocType(DefaultedAssocType) {
133-
assert(AssocType && TheType);
134-
}
135-
136-
public:
137-
static AbstractTypeWitness forFixed(AssociatedTypeDecl *assocType, Type type);
138-
139-
static AbstractTypeWitness forDefault(AssociatedTypeDecl *assocType,
140-
Type type,
141-
AssociatedTypeDecl *defaultedAssocType);
142-
143-
static AbstractTypeWitness forGenericParam(AssociatedTypeDecl *assocType,
144-
Type type);
145-
146-
public:
147-
AbstractTypeWitnessKind getKind() const { return Kind; }
148-
149-
AssociatedTypeDecl *getAssocType() const { return AssocType; }
150-
151-
Type getType() const { return TheType; }
152-
153-
AssociatedTypeDecl *getDefaultedAssocType() const {
154-
return DefaultedAssocType;
155-
}
156-
};
157-
158103
/// The set of associated types that have been inferred by matching
159104
/// the given value witness to its corresponding requirement.
160105
struct InferredAssociatedTypesByWitness {
@@ -1134,24 +1079,16 @@ class AssociatedTypeInference {
11341079
ConformanceChecker &checker,
11351080
const llvm::SetVector<AssociatedTypeDecl *> &assocTypes);
11361081

1137-
/// Compute a "fixed" type witness for an associated type, e.g.,
1138-
/// if the refined protocol requires it to be equivalent to some other type.
1139-
Type computeFixedTypeWitness(AssociatedTypeDecl *assocType);
1140-
11411082
/// Compute the default type witness from an associated type default,
11421083
/// if there is one.
1143-
Optional<AbstractTypeWitness>
1084+
Optional<std::pair<AssociatedTypeDecl *, Type>>
11441085
computeDefaultTypeWitness(AssociatedTypeDecl *assocType) const;
11451086

11461087
/// Compute the "derived" type witness for an associated type that is
11471088
/// known to the compiler.
11481089
std::pair<Type, TypeDecl *>
11491090
computeDerivedTypeWitness(AssociatedTypeDecl *assocType);
11501091

1151-
/// Compute a type witness without using a specific potential witness.
1152-
Optional<AbstractTypeWitness>
1153-
computeAbstractTypeWitness(AssociatedTypeDecl *assocType);
1154-
11551092
/// Collect abstract type witnesses and feed them to the given system.
11561093
void collectAbstractTypeWitnesses(
11571094
TypeWitnessSystem &system,

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,6 @@ STATISTIC(NumDuplicateSolutionStates,
4444

4545
using namespace swift;
4646

47-
AbstractTypeWitness AbstractTypeWitness::forFixed(AssociatedTypeDecl *assocType,
48-
Type type) {
49-
return AbstractTypeWitness(AbstractTypeWitnessKind::Fixed, assocType, type,
50-
nullptr);
51-
}
52-
53-
AbstractTypeWitness
54-
AbstractTypeWitness::forDefault(AssociatedTypeDecl *assocType, Type type,
55-
AssociatedTypeDecl *defaultedAssocType) {
56-
return AbstractTypeWitness(AbstractTypeWitnessKind::Default, assocType, type,
57-
defaultedAssocType);
58-
}
59-
60-
AbstractTypeWitness
61-
AbstractTypeWitness::forGenericParam(AssociatedTypeDecl *assocType, Type type) {
62-
return AbstractTypeWitness(AbstractTypeWitnessKind::GenericParam, assocType,
63-
type, nullptr);
64-
}
65-
6647
void InferredAssociatedTypesByWitness::dump() const {
6748
dump(llvm::errs(), 0);
6849
}
@@ -817,56 +798,7 @@ AssociatedTypeDecl *AssociatedTypeInference::findDefaultedAssociatedType(
817798
return results.size() == 1 ? results.front() : nullptr;
818799
}
819800

820-
Type AssociatedTypeInference::computeFixedTypeWitness(
821-
AssociatedTypeDecl *assocType) {
822-
Type resultType;
823-
824-
// Look at all of the inherited protocols to determine whether they
825-
// require a fixed type for this associated type.
826-
for (auto conformedProto : adoptee->getAnyNominal()->getAllProtocols()) {
827-
if (conformedProto != assocType->getProtocol() &&
828-
!conformedProto->inheritsFrom(assocType->getProtocol()))
829-
continue;
830-
831-
auto sig = conformedProto->getGenericSignature();
832-
833-
// FIXME: The RequirementMachine will assert on re-entrant construction.
834-
// We should find a more principled way of breaking this cycle.
835-
if (ctx.isRecursivelyConstructingRequirementMachine(sig.getCanonicalSignature()) ||
836-
ctx.isRecursivelyConstructingRequirementMachine(conformedProto) ||
837-
conformedProto->isComputingRequirementSignature())
838-
continue;
839-
840-
auto selfTy = conformedProto->getSelfInterfaceType();
841-
if (!sig->requiresProtocol(selfTy, assocType->getProtocol()))
842-
continue;
843-
844-
auto structuralTy = DependentMemberType::get(selfTy, assocType->getName());
845-
const auto ty = sig.getCanonicalTypeInContext(structuralTy);
846-
847-
// A dependent member type with an identical base and name indicates that
848-
// the protocol does not same-type constrain it in any way; move on to
849-
// the next protocol.
850-
if (auto *const memberTy = ty->getAs<DependentMemberType>()) {
851-
if (memberTy->getBase()->isEqual(selfTy) &&
852-
memberTy->getName() == assocType->getName())
853-
continue;
854-
}
855-
856-
if (!resultType) {
857-
resultType = ty;
858-
continue;
859-
}
860-
861-
// FIXME: Bailing out on ambiguity.
862-
if (!resultType->isEqual(ty))
863-
return Type();
864-
}
865-
866-
return resultType;
867-
}
868-
869-
Optional<AbstractTypeWitness>
801+
Optional<std::pair<AssociatedTypeDecl *, Type>>
870802
AssociatedTypeInference::computeDefaultTypeWitness(
871803
AssociatedTypeDecl *assocType) const {
872804
// Go find a default definition.
@@ -882,8 +814,7 @@ AssociatedTypeInference::computeDefaultTypeWitness(
882814
if (defaultType->hasError())
883815
return None;
884816

885-
return AbstractTypeWitness::forDefault(assocType, defaultType,
886-
defaultedAssocType);
817+
return std::make_pair(defaultedAssocType, defaultType);
887818
}
888819

889820
std::pair<Type, TypeDecl *>
@@ -914,29 +845,6 @@ AssociatedTypeInference::computeDerivedTypeWitness(
914845
return result;
915846
}
916847

917-
Optional<AbstractTypeWitness>
918-
AssociatedTypeInference::computeAbstractTypeWitness(
919-
AssociatedTypeDecl *assocType) {
920-
// We don't have a type witness for this associated type, so go
921-
// looking for more options.
922-
if (Type concreteType = computeFixedTypeWitness(assocType))
923-
return AbstractTypeWitness::forFixed(assocType, concreteType);
924-
925-
// If we can form a default type, do so.
926-
if (const auto &typeWitness = computeDefaultTypeWitness(assocType))
927-
return typeWitness;
928-
929-
// If there is a generic parameter of the named type, use that.
930-
if (auto genericSig = dc->getGenericSignatureOfContext()) {
931-
for (auto gp : genericSig.getInnermostGenericParams()) {
932-
if (gp->getName() == assocType->getName())
933-
return AbstractTypeWitness::forGenericParam(assocType, gp);
934-
}
935-
}
936-
937-
return None;
938-
}
939-
940848
void AssociatedTypeInference::collectAbstractTypeWitnesses(
941849
TypeWitnessSystem &system,
942850
ArrayRef<AssociatedTypeDecl *> unresolvedAssocTypes) const {
@@ -970,9 +878,8 @@ void AssociatedTypeInference::collectAbstractTypeWitnesses(
970878
}
971879

972880
// If we find a default type definition, feed it to the system.
973-
if (const auto &typeWitness = computeDefaultTypeWitness(assocType)) {
974-
system.addDefaultTypeWitness(typeWitness->getType(),
975-
typeWitness->getDefaultedAssocType());
881+
if (const auto declAndType = computeDefaultTypeWitness(assocType)) {
882+
system.addDefaultTypeWitness(declAndType->second, declAndType->first);
976883
} else {
977884
// As a last resort, look for a generic parameter that matches the name
978885
// of the associated type.

0 commit comments

Comments
 (0)