Skip to content

Commit db4f790

Browse files
committed
[ConstraintSystem] Make it possible to use PotentialBinding in set/map
1 parent 7e7ab2f commit db4f790

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4638,13 +4638,13 @@ class ConstraintSystem {
46384638

46394639
PotentialBinding(Type type, AllowedBindingKind kind,
46404640
PointerUnion<Constraint *, ConstraintLocator *> source)
4641-
: BindingType(type->getWithoutParens()), Kind(kind),
4642-
BindingSource(source) {}
4641+
: BindingType(type), Kind(kind), BindingSource(source) {}
46434642

46444643
public:
46454644
PotentialBinding(Type type, AllowedBindingKind kind, Constraint *source)
4646-
: BindingType(type->getWithoutParens()), Kind(kind),
4647-
BindingSource(source) {}
4645+
: PotentialBinding(
4646+
type->getWithoutParens(), kind,
4647+
PointerUnion<Constraint *, ConstraintLocator *>(source)) {}
46484648

46494649
bool isDefaultableBinding() const {
46504650
if (auto *constraint = BindingSource.dyn_cast<Constraint *>())
@@ -4695,6 +4695,11 @@ class ConstraintSystem {
46954695
AllowedBindingKind::Exact,
46964696
/*source=*/locator};
46974697
}
4698+
4699+
static PotentialBinding forPlaceholder(Type placeholderTy) {
4700+
return {placeholderTy, AllowedBindingKind::Exact,
4701+
PointerUnion<Constraint *, ConstraintLocator *>()};
4702+
}
46984703
};
46994704

47004705
struct LiteralRequirement {
@@ -6233,6 +6238,53 @@ struct DenseMapInfo<swift::constraints::SolutionApplicationTargetsKey> {
62336238
}
62346239
};
62356240

6241+
template <>
6242+
struct DenseMapInfo<swift::constraints::ConstraintSystem::PotentialBinding> {
6243+
using Binding = swift::constraints::ConstraintSystem::PotentialBinding;
6244+
6245+
static Binding getEmptyKey() {
6246+
return placeholderKey(llvm::DenseMapInfo<swift::TypeBase *>::getEmptyKey());
6247+
}
6248+
6249+
static Binding getTombstoneKey() {
6250+
return placeholderKey(
6251+
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey());
6252+
}
6253+
6254+
static unsigned getHashValue(const Binding &Val) {
6255+
return DenseMapInfo<swift::Type>::getHashValue(Val.BindingType);
6256+
}
6257+
6258+
static bool isEqual(const Binding &LHS, const Binding &RHS) {
6259+
auto lhsTy = LHS.BindingType.getPointer();
6260+
auto rhsTy = RHS.BindingType.getPointer();
6261+
6262+
// Fast path: pointer equality.
6263+
if (DenseMapInfo<swift::TypeBase *>::isEqual(lhsTy, rhsTy))
6264+
return true;
6265+
6266+
// If either side is empty or tombstone, let's use pointer equality.
6267+
{
6268+
auto emptyTy = llvm::DenseMapInfo<swift::TypeBase *>::getEmptyKey();
6269+
auto tombstoneTy =
6270+
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey();
6271+
6272+
if (lhsTy == emptyTy || lhsTy == tombstoneTy)
6273+
return lhsTy == rhsTy;
6274+
6275+
if (rhsTy == emptyTy || rhsTy == tombstoneTy)
6276+
return lhsTy == rhsTy;
6277+
}
6278+
6279+
// Otherwise let's drop the sugar and check.
6280+
return LHS.BindingType->isEqual(RHS.BindingType);
6281+
}
6282+
6283+
private:
6284+
static Binding placeholderKey(swift::Type type) {
6285+
return Binding::forPlaceholder(type);
6286+
}
6287+
};
62366288
}
62376289

62386290
#endif // LLVM_SWIFT_SEMA_CONSTRAINT_SYSTEM_H

0 commit comments

Comments
 (0)