Skip to content

[CSGen] Fix for exponential dictionary literal behavior in SR-3668 #6973

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
Jan 23, 2017
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
36 changes: 36 additions & 0 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,42 @@ namespace {
C.getIdentifier("Value")).front());

auto locator = CS.getConstraintLocator(expr);
auto contextualType = CS.getContextualType(expr);
Type contextualDictionaryType = nullptr;
Type contextualDictionaryKeyType = nullptr;
Type contextualDictionaryValueType = nullptr;

// If a contextual type exists for this expression, apply it directly.
Optional<std::pair<Type, Type>> dictionaryKeyValue;
if (contextualType &&
(dictionaryKeyValue = ConstraintSystem::isDictionaryType(contextualType))) {
// Is the contextual type a dictionary type?
contextualDictionaryType = contextualType;
std::tie(contextualDictionaryKeyType,
contextualDictionaryValueType) = *dictionaryKeyValue;

// Form an explicit tuple type from the contextual type's key and value types.
TupleTypeElt tupleElts[2] = { TupleTypeElt(contextualDictionaryKeyType),
TupleTypeElt(contextualDictionaryValueType) };
Type contextualDictionaryElementType = TupleType::get(tupleElts, C);

CS.addConstraint(ConstraintKind::LiteralConformsTo, contextualType,
dictionaryProto->getDeclaredType(),
locator);

unsigned index = 0;
for (auto element : expr->getElements()) {
CS.addConstraint(ConstraintKind::Conversion,
element->getType(),
contextualDictionaryElementType,
CS.getConstraintLocator(expr,
LocatorPathElt::
getTupleElement(index++)));
}

return contextualDictionaryType;
}

auto dictionaryTy = CS.createTypeVariable(locator,
TVO_PrefersSubtypeBinding);

Expand Down
18 changes: 18 additions & 0 deletions test/Sema/complex_expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,21 @@ let v4 = [1 + 2 + 3, 4] as [UInt32] + [2 * 3] as [UInt32]
let v5 = ([1 + 2 + 3, 4] as [UInt32]) + ([2 * 3] as [UInt32])
let v6 = [1 + 2 + 3, 4] as Set<UInt32>
let v7: [UInt32] = [55 * 8, 0]

// SR-3668
// "Expression was too complex" errors for short dictionary literals
// of simple closure expressions

let sr3668Dict1: Dictionary<Int, (Int, Int) -> Bool> =
[ 0: { $0 == $1 }, 1: { $0 == $1 }, 2: { $0 == $1 }, 3: { $0 == $1 },
4: { $0 == $1 }, 5: { $0 == $1 }, 6: { $0 == $1 }, 7: { $0 == $1 },
8: { $0 == $1 }, 9: { $0 == $1 }, 10: { $0 == $1 }, 11: { $0 == $1 },
12: { $0 == $1 }, 13: { $0 == $1 }, 14: { $0 == $1 }, 15: { $0 == $1 },
16: { $0 == $1 }, 17: { $0 == $1 }, 18: { $0 == $1 }, 19: { $0 == $1 } ]

let sr3668Dict2: [Int: (Int, Int) -> Bool] =
[ 0: { $0 != $1 }, 1: { $0 != $1 }, 2: { $0 != $1 }, 3: { $0 != $1 },
4: { $0 != $1 }, 5: { $0 != $1 }, 6: { $0 != $1 }, 7: { $0 != $1 },
8: { $0 != $1 }, 9: { $0 != $1 }, 10: { $0 != $1 }, 11: { $0 != $1 },
12: { $0 != $1 }, 13: { $0 != $1 }, 14: { $0 != $1 }, 15: { $0 != $1 },
16: { $0 != $1 }, 17: { $0 != $1 }, 18: { $0 != $1 }, 19: { $0 != $1 } ]