Skip to content

Commit ebe93b8

Browse files
authored
Merge pull request #40015 from hamishknight/caching-out
2 parents 09f160a + fb37969 commit ebe93b8

File tree

3 files changed

+12
-75
lines changed

3 files changed

+12
-75
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,10 +2290,6 @@ class ConstraintSystem {
22902290
llvm::DenseMap<std::pair<Type, DeclNameRef>, Optional<LookupResult>>
22912291
MemberLookups;
22922292

2293-
/// Cached sets of "alternative" literal types.
2294-
static const unsigned NumAlternativeLiteralTypes = 13;
2295-
Optional<ArrayRef<Type>> AlternativeLiteralTypes[NumAlternativeLiteralTypes];
2296-
22972293
/// Folding set containing all of the locators used in this
22982294
/// constraint system.
22992295
llvm::FoldingSetVector<ConstraintLocator> ConstraintLocators;
@@ -3059,7 +3055,8 @@ class ConstraintSystem {
30593055

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

30643061
/// Create a new type variable.
30653062
TypeVariableType *createTypeVariable(ConstraintLocator *locator,

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,8 @@ bool TypeVarBindingProducer::computeNext() {
17091709
if (NumTries == 0 && binding.hasDefaultedLiteralProtocol()) {
17101710
auto knownKind =
17111711
*(binding.getDefaultedLiteralProtocol()->getKnownProtocolKind());
1712-
for (auto altType : CS.getAlternativeLiteralTypes(knownKind)) {
1712+
SmallVector<Type, 2> scratch;
1713+
for (auto altType : CS.getAlternativeLiteralTypes(knownKind, scratch)) {
17131714
addNewBinding(binding.withSameSource(altType, BindingKind::Subtypes));
17141715
}
17151716
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -303,80 +303,19 @@ LookupResult &ConstraintSystem::lookupMember(Type base, DeclNameRef name) {
303303
return *result;
304304
}
305305

306-
ArrayRef<Type> ConstraintSystem::
307-
getAlternativeLiteralTypes(KnownProtocolKind kind) {
308-
unsigned index;
309-
310-
switch (kind) {
311-
#define PROTOCOL_WITH_NAME(Id, Name) \
312-
case KnownProtocolKind::Id: llvm_unreachable("Not a literal protocol");
313-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, __, ___)
314-
#include "swift/AST/KnownProtocols.def"
315-
316-
case KnownProtocolKind::ExpressibleByArrayLiteral: index = 0; break;
317-
case KnownProtocolKind::ExpressibleByDictionaryLiteral:index = 1; break;
318-
case KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral: index = 2;
319-
break;
320-
case KnownProtocolKind::ExpressibleByFloatLiteral: index = 3; break;
321-
case KnownProtocolKind::ExpressibleByIntegerLiteral: index = 4; break;
322-
case KnownProtocolKind::ExpressibleByStringInterpolation: index = 5; break;
323-
case KnownProtocolKind::ExpressibleByStringLiteral: index = 6; break;
324-
case KnownProtocolKind::ExpressibleByNilLiteral: index = 7; break;
325-
case KnownProtocolKind::ExpressibleByBooleanLiteral: index = 8; break;
326-
case KnownProtocolKind::ExpressibleByUnicodeScalarLiteral: index = 9; break;
327-
case KnownProtocolKind::ExpressibleByColorLiteral: index = 10; break;
328-
case KnownProtocolKind::ExpressibleByImageLiteral: index = 11; break;
329-
case KnownProtocolKind::ExpressibleByFileReferenceLiteral: index = 12; break;
330-
}
331-
static_assert(NumAlternativeLiteralTypes == 13, "Wrong # of literal types");
332-
333-
// If we already looked for alternative literal types, return those results.
334-
if (AlternativeLiteralTypes[index])
335-
return *AlternativeLiteralTypes[index];
336-
337-
SmallVector<Type, 4> types;
338-
339-
// Some literal kinds are related.
340-
switch (kind) {
341-
#define PROTOCOL_WITH_NAME(Id, Name) \
342-
case KnownProtocolKind::Id: llvm_unreachable("Not a literal protocol");
343-
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(Id, Name, __, ___)
344-
#include "swift/AST/KnownProtocols.def"
345-
346-
case KnownProtocolKind::ExpressibleByArrayLiteral:
347-
case KnownProtocolKind::ExpressibleByDictionaryLiteral:
348-
break;
349-
350-
case KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral:
351-
case KnownProtocolKind::ExpressibleByStringInterpolation:
352-
case KnownProtocolKind::ExpressibleByStringLiteral:
353-
case KnownProtocolKind::ExpressibleByUnicodeScalarLiteral:
354-
break;
355-
356-
case KnownProtocolKind::ExpressibleByIntegerLiteral:
306+
ArrayRef<Type>
307+
ConstraintSystem::getAlternativeLiteralTypes(KnownProtocolKind kind,
308+
SmallVectorImpl<Type> &scratch) {
309+
assert(scratch.empty());
310+
if (kind == KnownProtocolKind::ExpressibleByIntegerLiteral) {
357311
// Integer literals can be treated as floating point literals.
358312
if (auto floatProto = getASTContext().getProtocol(
359313
KnownProtocolKind::ExpressibleByFloatLiteral)) {
360-
if (auto defaultType = TypeChecker::getDefaultType(floatProto, DC)) {
361-
types.push_back(defaultType);
362-
}
314+
if (auto defaultType = TypeChecker::getDefaultType(floatProto, DC))
315+
scratch.push_back(defaultType);
363316
}
364-
break;
365-
366-
case KnownProtocolKind::ExpressibleByFloatLiteral:
367-
break;
368-
369-
case KnownProtocolKind::ExpressibleByNilLiteral:
370-
case KnownProtocolKind::ExpressibleByBooleanLiteral:
371-
break;
372-
case KnownProtocolKind::ExpressibleByColorLiteral:
373-
case KnownProtocolKind::ExpressibleByImageLiteral:
374-
case KnownProtocolKind::ExpressibleByFileReferenceLiteral:
375-
break;
376317
}
377-
378-
AlternativeLiteralTypes[index] = allocateCopy(types);
379-
return *AlternativeLiteralTypes[index];
318+
return scratch;
380319
}
381320

382321
bool ConstraintSystem::containsCodeCompletionLoc(Expr *expr) const {

0 commit comments

Comments
 (0)