@@ -712,8 +712,7 @@ Type ConstraintSystem::getFixedTypeRecursive(Type type,
712
712
// / \param baseType - the type of the base on which this object
713
713
// / is being accessed; must be null if and only if this is not
714
714
// / a type member
715
- static bool doesStorageProduceLValue (TypeChecker &TC,
716
- AbstractStorageDecl *storage,
715
+ static bool doesStorageProduceLValue (AbstractStorageDecl *storage,
717
716
Type baseType, DeclContext *useDC,
718
717
const DeclRefExpr *base = nullptr ) {
719
718
// Unsettable storage decls always produce rvalues.
@@ -775,7 +774,7 @@ Type TypeChecker::getUnopenedTypeOfReference(
775
774
776
775
// Qualify storage declarations with an lvalue when appropriate.
777
776
// Otherwise, they yield rvalues (and the access must be a load).
778
- if (doesStorageProduceLValue (* this , value, baseType, UseDC, base) &&
777
+ if (doesStorageProduceLValue (value, baseType, UseDC, base) &&
779
778
!requestedType->hasError ()) {
780
779
return LValueType::get (requestedType);
781
780
}
@@ -1247,7 +1246,7 @@ ConstraintSystem::getTypeOfMemberReference(
1247
1246
if (auto *subscript = dyn_cast<SubscriptDecl>(value)) {
1248
1247
auto elementTy = subscript->getElementInterfaceType ();
1249
1248
1250
- if (doesStorageProduceLValue (TC, subscript, baseTy, useDC, base))
1249
+ if (doesStorageProduceLValue (subscript, baseTy, useDC, base))
1251
1250
elementTy = LValueType::get (elementTy);
1252
1251
1253
1252
// See ConstraintSystem::resolveOverload() -- optional and dynamic
@@ -1467,7 +1466,8 @@ static ArrayRef<OverloadChoice> partitionSIMDOperators(
1467
1466
1468
1467
// / Retrieve the type that will be used when matching the given overload.
1469
1468
static Type getEffectiveOverloadType (const OverloadChoice &overload,
1470
- bool allowMembers) {
1469
+ bool allowMembers,
1470
+ DeclContext *useDC) {
1471
1471
switch (overload.getKind ()) {
1472
1472
case OverloadChoiceKind::Decl:
1473
1473
// Declaration choices are handled below.
@@ -1513,13 +1513,36 @@ static Type getEffectiveOverloadType(const OverloadChoice &overload,
1513
1513
if (!allowMembers)
1514
1514
return Type ();
1515
1515
1516
- // FIXME: This is overly restrictive for now.
1517
- if ((!isa<FuncDecl>(decl) && !isa<EnumElementDecl>(decl)) ||
1518
- (decl->isInstanceMember () &&
1519
- (!overload.getBaseType () || !overload.getBaseType ()->getAnyNominal ())))
1520
- return Type ();
1516
+ if (auto subscript = dyn_cast<SubscriptDecl>(decl)) {
1517
+ auto elementTy = subscript->getElementInterfaceType ();
1521
1518
1522
- type = type->castTo <FunctionType>()->getResult ();
1519
+ if (doesStorageProduceLValue (subscript, overload.getBaseType (), useDC))
1520
+ elementTy = LValueType::get (elementTy);
1521
+
1522
+ // See ConstraintSystem::resolveOverload() -- optional and dynamic
1523
+ // subscripts are a special case, because the optionality is
1524
+ // applied to the result type and not the type of the reference.
1525
+ if (subscript->getAttrs ().hasAttribute <OptionalAttr>() ||
1526
+ overload.getKind () == OverloadChoiceKind::DeclViaDynamic)
1527
+ elementTy = OptionalType::get (elementTy->getRValueType ());
1528
+
1529
+ auto indices = subscript->getInterfaceType ()
1530
+ ->castTo <AnyFunctionType>()->getParams ();
1531
+ type = FunctionType::get (indices, elementTy);
1532
+ } else if (auto var = dyn_cast<VarDecl>(decl)) {
1533
+ type = var->getInterfaceType ();
1534
+ if (doesStorageProduceLValue (var, overload.getBaseType (), useDC))
1535
+ type = LValueType::get (type);
1536
+ } else if (isa<FuncDecl>(decl) || isa<EnumElementDecl>(decl)) {
1537
+ if (decl->isInstanceMember () &&
1538
+ (!overload.getBaseType () ||
1539
+ !overload.getBaseType ()->getAnyNominal ()))
1540
+ return Type ();
1541
+
1542
+ type = type->castTo <FunctionType>()->getResult ();
1543
+ } else {
1544
+ return type;
1545
+ }
1523
1546
}
1524
1547
1525
1548
return type;
@@ -1776,7 +1799,7 @@ Type ConstraintSystem::findCommonResultType(ArrayRef<OverloadChoice> choices) {
1776
1799
// If we can't even get a type for the overload, there's nothing more to
1777
1800
// do.
1778
1801
Type overloadType =
1779
- getEffectiveOverloadType (overload, /* allowMembers=*/ true );
1802
+ getEffectiveOverloadType (overload, /* allowMembers=*/ true , /* FIXME: */ DC );
1780
1803
if (!overloadType) {
1781
1804
return true ;
1782
1805
}
@@ -1827,7 +1850,7 @@ Type ConstraintSystem::findCommonOverloadType(
1827
1850
// If we can't even get a type for the overload, there's nothing more to
1828
1851
// do.
1829
1852
Type overloadType =
1830
- getEffectiveOverloadType (overload, /* allowMembers=*/ false );
1853
+ getEffectiveOverloadType (overload, /* allowMembers=*/ false , /* FIXME: */ DC );
1831
1854
if (!overloadType) {
1832
1855
return true ;
1833
1856
}
0 commit comments