Skip to content

[CS] Simplify getAlternativeLiteralTypes #40015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2290,10 +2290,6 @@ class ConstraintSystem {
llvm::DenseMap<std::pair<Type, DeclNameRef>, Optional<LookupResult>>
MemberLookups;

/// Cached sets of "alternative" literal types.
static const unsigned NumAlternativeLiteralTypes = 13;
Optional<ArrayRef<Type>> AlternativeLiteralTypes[NumAlternativeLiteralTypes];

/// Folding set containing all of the locators used in this
/// constraint system.
llvm::FoldingSetVector<ConstraintLocator> ConstraintLocators;
Expand Down Expand Up @@ -3059,7 +3055,8 @@ class ConstraintSystem {

/// Retrieve the set of "alternative" literal types that we'll explore
/// for a given literal protocol kind.
ArrayRef<Type> getAlternativeLiteralTypes(KnownProtocolKind kind);
ArrayRef<Type> getAlternativeLiteralTypes(KnownProtocolKind kind,
SmallVectorImpl<Type> &scratch);

/// Create a new type variable.
TypeVariableType *createTypeVariable(ConstraintLocator *locator,
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,8 @@ bool TypeVarBindingProducer::computeNext() {
if (NumTries == 0 && binding.hasDefaultedLiteralProtocol()) {
auto knownKind =
*(binding.getDefaultedLiteralProtocol()->getKnownProtocolKind());
for (auto altType : CS.getAlternativeLiteralTypes(knownKind)) {
SmallVector<Type, 2> scratch;
for (auto altType : CS.getAlternativeLiteralTypes(knownKind, scratch)) {
addNewBinding(binding.withSameSource(altType, BindingKind::Subtypes));
}
}
Expand Down
77 changes: 8 additions & 69 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,80 +303,19 @@ LookupResult &ConstraintSystem::lookupMember(Type base, DeclNameRef name) {
return *result;
}

ArrayRef<Type> ConstraintSystem::
getAlternativeLiteralTypes(KnownProtocolKind kind) {
unsigned index;

switch (kind) {
#define PROTOCOL_WITH_NAME(Id, Name) \
case KnownProtocolKind::Id: llvm_unreachable("Not a literal protocol");
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, __, ___)
#include "swift/AST/KnownProtocols.def"

case KnownProtocolKind::ExpressibleByArrayLiteral: index = 0; break;
case KnownProtocolKind::ExpressibleByDictionaryLiteral:index = 1; break;
case KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral: index = 2;
break;
case KnownProtocolKind::ExpressibleByFloatLiteral: index = 3; break;
case KnownProtocolKind::ExpressibleByIntegerLiteral: index = 4; break;
case KnownProtocolKind::ExpressibleByStringInterpolation: index = 5; break;
case KnownProtocolKind::ExpressibleByStringLiteral: index = 6; break;
case KnownProtocolKind::ExpressibleByNilLiteral: index = 7; break;
case KnownProtocolKind::ExpressibleByBooleanLiteral: index = 8; break;
case KnownProtocolKind::ExpressibleByUnicodeScalarLiteral: index = 9; break;
case KnownProtocolKind::ExpressibleByColorLiteral: index = 10; break;
case KnownProtocolKind::ExpressibleByImageLiteral: index = 11; break;
case KnownProtocolKind::ExpressibleByFileReferenceLiteral: index = 12; break;
}
static_assert(NumAlternativeLiteralTypes == 13, "Wrong # of literal types");

// If we already looked for alternative literal types, return those results.
if (AlternativeLiteralTypes[index])
return *AlternativeLiteralTypes[index];

SmallVector<Type, 4> types;

// Some literal kinds are related.
switch (kind) {
#define PROTOCOL_WITH_NAME(Id, Name) \
case KnownProtocolKind::Id: llvm_unreachable("Not a literal protocol");
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, __, ___)
#include "swift/AST/KnownProtocols.def"

case KnownProtocolKind::ExpressibleByArrayLiteral:
case KnownProtocolKind::ExpressibleByDictionaryLiteral:
break;

case KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral:
case KnownProtocolKind::ExpressibleByStringInterpolation:
case KnownProtocolKind::ExpressibleByStringLiteral:
case KnownProtocolKind::ExpressibleByUnicodeScalarLiteral:
break;

case KnownProtocolKind::ExpressibleByIntegerLiteral:
ArrayRef<Type>
ConstraintSystem::getAlternativeLiteralTypes(KnownProtocolKind kind,
SmallVectorImpl<Type> &scratch) {
assert(scratch.empty());
if (kind == KnownProtocolKind::ExpressibleByIntegerLiteral) {
// Integer literals can be treated as floating point literals.
if (auto floatProto = getASTContext().getProtocol(
KnownProtocolKind::ExpressibleByFloatLiteral)) {
if (auto defaultType = TypeChecker::getDefaultType(floatProto, DC)) {
types.push_back(defaultType);
}
if (auto defaultType = TypeChecker::getDefaultType(floatProto, DC))
scratch.push_back(defaultType);
}
break;

case KnownProtocolKind::ExpressibleByFloatLiteral:
break;

case KnownProtocolKind::ExpressibleByNilLiteral:
case KnownProtocolKind::ExpressibleByBooleanLiteral:
break;
case KnownProtocolKind::ExpressibleByColorLiteral:
case KnownProtocolKind::ExpressibleByImageLiteral:
case KnownProtocolKind::ExpressibleByFileReferenceLiteral:
break;
}

AlternativeLiteralTypes[index] = allocateCopy(types);
return *AlternativeLiteralTypes[index];
return scratch;
}

bool ConstraintSystem::containsCodeCompletionLoc(Expr *expr) const {
Expand Down