Skip to content

Commit 93fde73

Browse files
authored
Merge pull request #5283 from DougGregor/remote-literal-conformance-proto
2 parents 053f3b4 + 49b833b commit 93fde73

File tree

7 files changed

+101
-74
lines changed

7 files changed

+101
-74
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2920,7 +2920,8 @@ bool FailureDiagnosis::diagnoseGeneralConversionFailure(Constraint *constraint){
29202920
// If simplification has turned this into the same types, then this isn't the
29212921
// broken constraint that we're looking for.
29222922
if (fromType->isEqual(toType) &&
2923-
constraint->getKind() != ConstraintKind::ConformsTo)
2923+
constraint->getKind() != ConstraintKind::ConformsTo &&
2924+
constraint->getKind() != ConstraintKind::LiteralConformsTo)
29242925
return false;
29252926

29262927

lib/Sema/CSGen.cpp

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -500,49 +500,47 @@ namespace {
500500
return false;
501501
}
502502

503-
/// Determine whether the given parameter and argument type should be
503+
/// Determine whether the given parameter type and argument should be
504504
/// "favored" because they match exactly.
505505
bool isFavoredParamAndArg(ConstraintSystem &CS,
506506
Type paramTy,
507+
Expr *arg,
507508
Type argTy,
508-
Type otherArgTy) {
509-
if (argTy->getAs<LValueType>())
510-
argTy = argTy->getLValueOrInOutObjectType();
511-
512-
if (!otherArgTy.isNull() &&
513-
otherArgTy->getAs<LValueType>())
514-
otherArgTy = otherArgTy->getLValueOrInOutObjectType();
515-
509+
Expr *otherArg = nullptr,
510+
Type otherArgTy = Type()) {
511+
// Determine the argument type.
512+
argTy = argTy->getLValueOrInOutObjectType();
513+
516514
// Do the types match exactly?
517515
if (paramTy->isEqual(argTy))
518516
return true;
519-
520-
// If the argument is a type variable created for a literal that has a
521-
// default type, this is a favored param/arg pair if the parameter is of
522-
// that default type.
523-
// Is the argument a type variable...
524-
if (auto argTypeVar = argTy->getAs<TypeVariableType>()) {
525-
if (auto proto = argTypeVar->getImpl().literalConformanceProto) {
526-
// If it's a struct type associated with the literal conformance,
527-
// test against it directly. This helps to avoid 'widening' the
528-
// favored type to the default type for the literal.
529-
if (!otherArgTy.isNull() &&
530-
otherArgTy->getAs<StructType>()) {
531-
532-
if (CS.TC.conformsToProtocol(otherArgTy,
533-
proto,
534-
CS.DC,
535-
ConformanceCheckFlags::InExpression)) {
536-
return otherArgTy->isEqual(paramTy);
537-
}
538-
} else if (auto defaultTy = CS.TC.getDefaultType(proto, CS.DC)) {
539-
if (paramTy->isEqual(defaultTy)) {
540-
return true;
541-
}
542-
}
543-
}
517+
518+
// If the argument is a literal, this is a favored param/arg pair if
519+
// the parameter is of that default type.
520+
auto &tc = CS.getTypeChecker();
521+
auto literalProto = tc.getLiteralProtocol(arg->getSemanticsProvidingExpr());
522+
if (!literalProto) return false;
523+
524+
// Dig out the second argument type.
525+
if (otherArgTy)
526+
otherArgTy = otherArgTy->getLValueOrInOutObjectType();
527+
528+
// If there is another, concrete argument, check whether it's type
529+
// conforms to the literal protocol and test against it directly.
530+
// This helps to avoid 'widening' the favored type to the default type for
531+
// the literal.
532+
if (otherArgTy && otherArgTy->getAnyNominal()) {
533+
return otherArgTy->isEqual(paramTy) &&
534+
tc.conformsToProtocol(otherArgTy, literalProto, CS.DC,
535+
ConformanceCheckFlags::InExpression);
544536
}
545-
537+
538+
// If there is a default type for the literal protocol, check whether
539+
// it is the same as the parameter type.
540+
// Check whether there is a default type to compare against.
541+
if (Type defaultType = tc.getDefaultType(literalProto, CS.DC))
542+
return paramTy->isEqual(defaultType);
543+
546544
return false;
547545
}
548546

@@ -742,9 +740,6 @@ namespace {
742740
/// for the operand and contextual type.
743741
void favorMatchingUnaryOperators(ApplyExpr *expr,
744742
ConstraintSystem &CS) {
745-
// Find the argument type.
746-
auto argTy = expr->getArg()->getType()->getWithoutParens();
747-
748743
// Determine whether the given declaration is favored.
749744
auto isFavoredDecl = [&](ValueDecl *value) -> bool {
750745
auto valueTy = value->getType();
@@ -762,7 +757,8 @@ namespace {
762757
auto resultTy = fnTy->getResult();
763758
auto contextualTy = CS.getContextualType(expr);
764759

765-
return isFavoredParamAndArg(CS, paramTy, argTy, Type()) &&
760+
return isFavoredParamAndArg(CS, paramTy, expr->getArg(),
761+
expr->getArg()->getType()->getWithoutParens()) &&
766762
(!contextualTy || contextualTy->isEqual(resultTy));
767763
};
768764

@@ -881,8 +877,10 @@ namespace {
881877
if (!fnTy)
882878
return false;
883879

884-
auto firstFavoredTy = CS.getFavoredType(argTupleExpr->getElement(0));
885-
auto secondFavoredTy = CS.getFavoredType(argTupleExpr->getElement(1));
880+
Expr *firstArg = argTupleExpr->getElement(0);
881+
auto firstFavoredTy = CS.getFavoredType(firstArg);
882+
Expr *secondArg = argTupleExpr->getElement(1);
883+
auto secondFavoredTy = CS.getFavoredType(secondArg);
886884

887885
auto favoredExprTy = CS.getFavoredType(expr);
888886

@@ -926,8 +924,10 @@ namespace {
926924
auto contextualTy = CS.getContextualType(expr);
927925

928926
return
929-
(isFavoredParamAndArg(CS, firstParamTy, firstArgTy, secondArgTy) ||
930-
isFavoredParamAndArg(CS, secondParamTy, secondArgTy, firstArgTy)) &&
927+
(isFavoredParamAndArg(CS, firstParamTy, firstArg, firstArgTy,
928+
secondArg, secondArgTy) ||
929+
isFavoredParamAndArg(CS, secondParamTy, secondArg, secondArgTy,
930+
firstArg, firstArgTy)) &&
931931
firstParamTy->isEqual(secondParamTy) &&
932932
(!contextualTy || contextualTy->isEqual(resultTy));
933933
};
@@ -1083,7 +1083,7 @@ namespace {
10831083
auto keyTy = dictTy->first;
10841084
auto valueTy = dictTy->second;
10851085

1086-
if (isFavoredParamAndArg(CS, keyTy, index->getType(), Type())) {
1086+
if (isFavoredParamAndArg(CS, keyTy, index, index->getType())) {
10871087
outputTy = OptionalType::get(valueTy);
10881088

10891089
if (isLValueBase)
@@ -1164,10 +1164,7 @@ namespace {
11641164

11651165
auto tv = CS.createTypeVariable(CS.getConstraintLocator(expr),
11661166
TVO_PrefersSubtypeBinding);
1167-
1168-
tv->getImpl().literalConformanceProto = protocol;
1169-
1170-
CS.addConstraint(ConstraintKind::ConformsTo, tv,
1167+
CS.addConstraint(ConstraintKind::LiteralConformsTo, tv,
11711168
protocol->getDeclaredType(),
11721169
CS.getConstraintLocator(expr));
11731170
return tv;
@@ -1190,8 +1187,7 @@ namespace {
11901187
// ExpressibleByStringInterpolation protocol.
11911188
auto locator = CS.getConstraintLocator(expr);
11921189
auto tv = CS.createTypeVariable(locator, TVO_PrefersSubtypeBinding);
1193-
tv->getImpl().literalConformanceProto = interpolationProto;
1194-
CS.addConstraint(ConstraintKind::ConformsTo, tv,
1190+
CS.addConstraint(ConstraintKind::LiteralConformsTo, tv,
11951191
interpolationProto->getDeclaredType(),
11961192
locator);
11971193

@@ -1264,9 +1260,7 @@ namespace {
12641260
auto tv = CS.createTypeVariable(CS.getConstraintLocator(expr),
12651261
TVO_PrefersSubtypeBinding);
12661262

1267-
tv->getImpl().literalConformanceProto = protocol;
1268-
1269-
CS.addConstraint(ConstraintKind::ConformsTo, tv,
1263+
CS.addConstraint(ConstraintKind::LiteralConformsTo, tv,
12701264
protocol->getDeclaredType(),
12711265
CS.getConstraintLocator(expr));
12721266

@@ -1683,7 +1677,7 @@ namespace {
16831677
contextualArrayElementType =
16841678
CS.getBaseTypeForArrayType(contextualType.getPointer());
16851679

1686-
CS.addConstraint(ConstraintKind::ConformsTo, contextualType,
1680+
CS.addConstraint(ConstraintKind::LiteralConformsTo, contextualType,
16871681
arrayProto->getDeclaredType(),
16881682
locator);
16891683

@@ -1703,7 +1697,7 @@ namespace {
17031697
auto arrayTy = CS.createTypeVariable(locator, TVO_PrefersSubtypeBinding);
17041698

17051699
// The array must be an array literal type.
1706-
CS.addConstraint(ConstraintKind::ConformsTo, arrayTy,
1700+
CS.addConstraint(ConstraintKind::LiteralConformsTo, arrayTy,
17071701
arrayProto->getDeclaredType(),
17081702
locator);
17091703

@@ -1769,8 +1763,8 @@ namespace {
17691763
auto dictionaryTy = CS.createTypeVariable(locator,
17701764
TVO_PrefersSubtypeBinding);
17711765

1772-
// The array must be a dictionary literal type.
1773-
CS.addConstraint(ConstraintKind::ConformsTo, dictionaryTy,
1766+
// The dictionary must be a dictionary literal type.
1767+
CS.addConstraint(ConstraintKind::LiteralConformsTo, dictionaryTy,
17741768
dictionaryProto->getDeclaredType(),
17751769
locator);
17761770

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
22882288
return SolutionKind::Solved;
22892289
break;
22902290
case ConstraintKind::ConformsTo:
2291+
case ConstraintKind::LiteralConformsTo:
22912292
// Check whether this type conforms to the protocol.
22922293
if (TC.conformsToProtocol(type, protocol, DC,
22932294
ConformanceCheckFlags::InExpression))
@@ -3467,6 +3468,7 @@ static TypeMatchKind getTypeMatchKind(ConstraintKind kind) {
34673468
llvm_unreachable("Overload binding constraints don't involve type matches");
34683469

34693470
case ConstraintKind::ConformsTo:
3471+
case ConstraintKind::LiteralConformsTo:
34703472
case ConstraintKind::SelfObjectOfProtocol:
34713473
llvm_unreachable("Conformance constraints don't involve type matches");
34723474

@@ -4126,6 +4128,7 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
41264128
return SolutionKind::Solved;
41274129

41284130
case ConstraintKind::ConformsTo:
4131+
case ConstraintKind::LiteralConformsTo:
41294132
case ConstraintKind::SelfObjectOfProtocol:
41304133
return simplifyConformsToConstraint(
41314134
constraint.getFirstType(),

lib/Sema/CSSolver.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,24 @@ static Optional<Type> checkTypeOfBinding(ConstraintSystem &cs,
6262
return None;
6363

6464
// If the type is a type variable itself, don't permit the binding.
65-
// FIXME: This is a hack. We need to be smarter about whether there's enough
66-
// structure in the type to produce an interesting binding, or not.
6765
if (auto bindingTypeVar = type->getRValueType()->getAs<TypeVariableType>()) {
68-
if (isNilLiteral &&
69-
bindingTypeVar->getImpl().literalConformanceProto &&
70-
bindingTypeVar->getImpl().literalConformanceProto->isSpecificProtocol(
71-
KnownProtocolKind::ExpressibleByNilLiteral))
72-
*isNilLiteral = true;
66+
if (isNilLiteral) {
67+
*isNilLiteral = false;
68+
69+
// Look for a literal-conformance constraint on the type variable.
70+
SmallVector<Constraint *, 8> constraints;
71+
cs.getConstraintGraph().gatherConstraints(bindingTypeVar, constraints);
72+
for (auto constraint : constraints) {
73+
if (constraint->getKind() == ConstraintKind::LiteralConformsTo &&
74+
constraint->getProtocol()->isSpecificProtocol(
75+
KnownProtocolKind::ExpressibleByNilLiteral) &&
76+
cs.simplifyType(constraint->getFirstType())
77+
->isEqual(bindingTypeVar)) {
78+
*isNilLiteral = true;
79+
break;
80+
}
81+
}
82+
}
7383

7484
return None;
7585
}
@@ -667,6 +677,7 @@ static bool shouldBindToValueType(Constraint *constraint)
667677
case ConstraintKind::Equal:
668678
case ConstraintKind::BindParam:
669679
case ConstraintKind::ConformsTo:
680+
case ConstraintKind::LiteralConformsTo:
670681
case ConstraintKind::CheckedCast:
671682
case ConstraintKind::SelfObjectOfProtocol:
672683
case ConstraintKind::ApplicableFunction:
@@ -782,8 +793,19 @@ static PotentialBindings getPotentialBindings(ConstraintSystem &cs,
782793
result.InvolvesTypeVariables = true;
783794
continue;
784795

785-
case ConstraintKind::ConformsTo:
796+
case ConstraintKind::LiteralConformsTo:
797+
// If there is a 'nil' literal constraint, we might need optional
798+
// supertype bindings.
799+
if (constraint->getProtocol()->isSpecificProtocol(
800+
KnownProtocolKind::ExpressibleByNilLiteral))
801+
addOptionalSupertypeBindings = true;
802+
803+
SWIFT_FALLTHROUGH;
804+
805+
case ConstraintKind::ConformsTo:
786806
case ConstraintKind::SelfObjectOfProtocol: {
807+
// FIXME: Only for LiteralConformsTo?
808+
787809
// If there is a default literal type for this protocol, it's a
788810
// potential binding.
789811
auto defaultType = tc.getDefaultType(constraint->getProtocol(), cs.DC);
@@ -908,13 +930,17 @@ static PotentialBindings getPotentialBindings(ConstraintSystem &cs,
908930
// Check whether we can perform this binding.
909931
// FIXME: this has a super-inefficient extraneous simplifyType() in it.
910932
bool isNilLiteral = false;
911-
if (auto boundType = checkTypeOfBinding(cs, typeVar, type, &isNilLiteral)) {
933+
bool *isNilLiteralPtr = nullptr;
934+
if (!addOptionalSupertypeBindings && kind == AllowedBindingKind::Supertypes)
935+
isNilLiteralPtr = &isNilLiteral;
936+
if (auto boundType = checkTypeOfBinding(cs, typeVar, type,
937+
isNilLiteralPtr)) {
912938
type = *boundType;
913939
if (type->hasTypeVariable())
914940
result.InvolvesTypeVariables = true;
915941
} else {
916942
// If the bound is a 'nil' literal type, add optional supertype bindings.
917-
if (isNilLiteral && kind == AllowedBindingKind::Supertypes) {
943+
if (isNilLiteral) {
918944
addOptionalSupertypeBindings = true;
919945
continue;
920946
}

lib/Sema/Constraint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
6161
case ConstraintKind::OperatorArgumentTupleConversion:
6262
case ConstraintKind::OperatorArgumentConversion:
6363
case ConstraintKind::ConformsTo:
64+
case ConstraintKind::LiteralConformsTo:
6465
case ConstraintKind::CheckedCast:
6566
case ConstraintKind::SelfObjectOfProtocol:
6667
case ConstraintKind::DynamicTypeOf:
@@ -144,6 +145,7 @@ Constraint::Constraint(ConstraintKind kind, Fix fix,
144145

145146
ProtocolDecl *Constraint::getProtocol() const {
146147
assert((Kind == ConstraintKind::ConformsTo ||
148+
Kind == ConstraintKind::LiteralConformsTo ||
147149
Kind == ConstraintKind::SelfObjectOfProtocol)
148150
&& "Not a conformance constraint");
149151
return Types.Second->castTo<ProtocolType>()->getDecl();
@@ -162,6 +164,7 @@ Constraint *Constraint::clone(ConstraintSystem &cs) const {
162164
case ConstraintKind::OperatorArgumentTupleConversion:
163165
case ConstraintKind::OperatorArgumentConversion:
164166
case ConstraintKind::ConformsTo:
167+
case ConstraintKind::LiteralConformsTo:
165168
case ConstraintKind::CheckedCast:
166169
case ConstraintKind::DynamicTypeOf:
167170
case ConstraintKind::SelfObjectOfProtocol:
@@ -238,6 +241,7 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
238241
case ConstraintKind::OperatorArgumentConversion:
239242
Out << " operator arg conv "; break;
240243
case ConstraintKind::ConformsTo: Out << " conforms to "; break;
244+
case ConstraintKind::LiteralConformsTo: Out << " literal conforms to "; break;
241245
case ConstraintKind::CheckedCast: Out << " checked cast to "; break;
242246
case ConstraintKind::SelfObjectOfProtocol: Out << " Self type of "; break;
243247
case ConstraintKind::ApplicableFunction: Out << " applicable fn "; break;
@@ -486,6 +490,7 @@ gatherReferencedTypeVars(Constraint *constraint,
486490
case ConstraintKind::BindOverload:
487491
case ConstraintKind::Class:
488492
case ConstraintKind::ConformsTo:
493+
case ConstraintKind::LiteralConformsTo:
489494
case ConstraintKind::SelfObjectOfProtocol:
490495
constraint->getFirstType()->getTypeVariables(typeVars);
491496

lib/Sema/Constraint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ enum class ConstraintKind : char {
8181
/// \brief The first type must conform to the second type (which is a
8282
/// protocol type).
8383
ConformsTo,
84+
/// \brief The first type describes a literal that conforms to the second
85+
/// type, which is one of the known expressible-by-literal protocols.
86+
LiteralConformsTo,
8487
/// A checked cast from the first type to the second.
8588
CheckedCast,
8689
/// \brief The first type can act as the Self type of the second type (which
@@ -473,6 +476,7 @@ class Constraint final : public llvm::ilist_node<Constraint>,
473476
case ConstraintKind::OperatorArgumentTupleConversion:
474477
case ConstraintKind::OperatorArgumentConversion:
475478
case ConstraintKind::ConformsTo:
479+
case ConstraintKind::LiteralConformsTo:
476480
case ConstraintKind::CheckedCast:
477481
case ConstraintKind::SelfObjectOfProtocol:
478482
case ConstraintKind::ApplicableFunction:

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ class TypeVariableType::Implementation {
158158
friend class constraints::SavedTypeVariableBinding;
159159

160160
public:
161-
162-
/// \brief If this type variable is an opened literal expression, keep track
163-
/// of the associated literal conformance for optimization and diagnostic
164-
/// purposes.
165-
ProtocolDecl *literalConformanceProto = nullptr;
166-
167161
explicit Implementation(constraints::ConstraintLocator *locator,
168162
unsigned options)
169163
: Options(options), locator(locator),

0 commit comments

Comments
 (0)