Skip to content

Commit cd44ca8

Browse files
committed
[ConstraintSystem] Move getAtomicLiteralKind to TypeVariableType::Implementation
1 parent 43aafcd commit cd44ca8

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#include "llvm/ADT/SetVector.h"
3333
#include "llvm/Support/Compiler.h"
3434

35-
#include <unordered_set>
36-
3735
using namespace swift;
3836
using namespace constraints;
3937

@@ -10421,30 +10419,6 @@ void ConstraintSystem::addContextualConversionConstraint(
1042110419
convertTypeLocator, /*isFavored*/ true);
1042210420
}
1042310421

10424-
Optional<ExprKind>
10425-
ConstraintSystem::getAtomicLiteralKind(TypeVariableType *typeVar) const {
10426-
const std::unordered_set<ExprKind> atomicLiteralKinds = {
10427-
ExprKind::IntegerLiteral,
10428-
ExprKind::FloatLiteral,
10429-
ExprKind::StringLiteral,
10430-
ExprKind::BooleanLiteral,
10431-
ExprKind::NilLiteral,
10432-
};
10433-
10434-
if (!typeVar)
10435-
return None;
10436-
10437-
auto *locator = typeVar->getImpl().getLocator();
10438-
if (!locator->directlyAt<LiteralExpr>())
10439-
return None;
10440-
10441-
auto literalKind = getAsExpr(locator->getAnchor())->getKind();
10442-
if (!atomicLiteralKinds.count(literalKind))
10443-
return None;
10444-
10445-
return literalKind;
10446-
}
10447-
1044810422
void ConstraintSystem::addFixConstraint(ConstraintFix *fix, ConstraintKind kind,
1044910423
Type first, Type second,
1045010424
ConstraintLocatorBuilder locator,

lib/Sema/ConstraintSystem.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ class TypeVariableType::Implementation {
306306
/// Retrieve the generic parameter opened by this type variable.
307307
GenericTypeParamType *getGenericParameter() const;
308308

309+
/// Returns the \c ExprKind of this type variable if it's the type of an
310+
/// atomic literal expression, meaning the literal can't be composed of subexpressions.
311+
/// Otherwise, returns \c None.
312+
Optional<ExprKind> getAtomicLiteralKind() const;
313+
309314
/// Determine whether this type variable represents a closure type.
310315
bool isClosureType() const;
311316

@@ -2994,11 +2999,6 @@ class ConstraintSystem {
29942999
bool isDeclUnavailable(const Decl *D,
29953000
ConstraintLocator *locator = nullptr) const;
29963001

2997-
/// Returns the \c ExprKind of the given type variable if it's the type of an
2998-
/// atomic literal expression, meaning the literal can't be composed of subexpressions.
2999-
/// Otherwise, returns \c None.
3000-
Optional<ExprKind> getAtomicLiteralKind(TypeVariableType *typeVar) const;
3001-
30023002
public:
30033003

30043004
/// Whether we should attempt to fix problems.
@@ -3127,18 +3127,19 @@ class ConstraintSystem {
31273127
// We can merge the type variables of same-kind atomic literal expressions because they
31283128
// will all have the same set of constraints and therefore can never resolve to anything
31293129
// different.
3130-
auto *typeVar = type->getAs<TypeVariableType>();
3131-
if (auto literalKind = getAtomicLiteralKind(typeVar)) {
3132-
auto *&originalRep = representativeForKind[RawExprKind(*literalKind)];
3133-
auto *currentRep = getRepresentative(typeVar);
3134-
3135-
if (originalRep) {
3136-
if (originalRep != currentRep)
3137-
mergeEquivalenceClasses(currentRep, originalRep);
3138-
continue;
3139-
}
3130+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
3131+
if (auto literalKind = typeVar->getImpl().getAtomicLiteralKind()) {
3132+
auto *&originalRep = representativeForKind[RawExprKind(*literalKind)];
3133+
auto *currentRep = getRepresentative(typeVar);
3134+
3135+
if (originalRep) {
3136+
if (originalRep != currentRep)
3137+
mergeEquivalenceClasses(currentRep, originalRep);
3138+
continue;
3139+
}
31403140

3141-
originalRep = currentRep;
3141+
originalRep = currentRep;
3142+
}
31423143
}
31433144

31443145
// Introduce conversions from each input type to the supertype.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ TypeVariableType::Implementation::getGenericParameter() const {
8282
return locator ? locator->getGenericParameter() : nullptr;
8383
}
8484

85+
Optional<ExprKind>
86+
TypeVariableType::Implementation::getAtomicLiteralKind() const {
87+
if (!locator || !locator->directlyAt<LiteralExpr>())
88+
return None;
89+
90+
auto kind = getAsExpr(locator->getAnchor())->getKind();
91+
switch (kind) {
92+
case ExprKind::IntegerLiteral:
93+
case ExprKind::FloatLiteral:
94+
case ExprKind::StringLiteral:
95+
case ExprKind::BooleanLiteral:
96+
case ExprKind::NilLiteral:
97+
return kind;
98+
default:
99+
return None;
100+
}
101+
}
102+
85103
bool TypeVariableType::Implementation::isClosureType() const {
86104
if (!(locator && locator->getAnchor()))
87105
return false;

0 commit comments

Comments
 (0)