@@ -602,9 +602,46 @@ class ExprContextAnalyzer {
602
602
// Check context types of the array literal expression.
603
603
ExprContextInfo arrayCtxtInfo (DC, Parent);
604
604
for (auto arrayT : arrayCtxtInfo.getPossibleTypes ()) {
605
- if (auto boundGenericT = arrayT->getAs <BoundGenericType>())
605
+ if (auto boundGenericT = arrayT->getAs <BoundGenericType>()) {
606
+ // let _: [Element] = [#HERE#]
607
+ // In this case, 'Element' is the expected type.
606
608
if (boundGenericT->getDecl () == Context.getArrayDecl ())
607
609
recordPossibleType (boundGenericT->getGenericArgs ()[0 ]);
610
+
611
+ // let _: [Key : Value] = [#HERE#]
612
+ // In this case, 'Key' is the expected type.
613
+ if (boundGenericT->getDecl () == Context.getDictionaryDecl ())
614
+ recordPossibleType (boundGenericT->getGenericArgs ()[0 ]);
615
+ }
616
+ }
617
+ break ;
618
+ }
619
+ case ExprKind::Dictionary: {
620
+ // Check context types of the dictionary literal expression.
621
+ ExprContextInfo dictCtxtInfo (DC, Parent);
622
+
623
+ for (auto dictT : dictCtxtInfo.getPossibleTypes ()) {
624
+ if (auto boundGenericT = dictT->getAs <BoundGenericType>()) {
625
+ if (boundGenericT->getDecl () == Context.getDictionaryDecl ()) {
626
+ if (ParsedExpr->isImplicit () && isa<TupleExpr>(ParsedExpr)) {
627
+ // let _: [Key : Value] = [#HERE#:]
628
+ // let _: [Key : Value] = [#HERE#:val]
629
+ // let _: [Key : Value] = [key:#HERE#]
630
+ // In this case, this is called by 'ExprKind::Tuple' case. Return
631
+ // '(Key,Value)' here, 'ExprKind::Tuple' branch can decide which
632
+ // type in the tuple type is the exprected type.
633
+ SmallVector<TupleTypeElt, 2 > elts;
634
+ for (auto genericArg : boundGenericT->getGenericArgs ())
635
+ elts.emplace_back (genericArg);
636
+ recordPossibleType (TupleType::get (elts, DC->getASTContext ()));
637
+ } else {
638
+ // let _: [Key : Value] = [key: val, #HERE#]
639
+ // In this case, assume 'Key' is the expected type.
640
+ if (boundGenericT->getDecl () == Context.getDictionaryDecl ())
641
+ recordPossibleType (boundGenericT->getGenericArgs ()[0 ]);
642
+ }
643
+ }
644
+ }
608
645
}
609
646
break ;
610
647
}
@@ -629,13 +666,25 @@ class ExprContextAnalyzer {
629
666
break ;
630
667
}
631
668
case ExprKind::Tuple: {
632
- if (!Parent->getType () || !Parent->getType ()->is <TupleType>())
633
- return ;
669
+ TupleType *tupleT = nullptr ;
670
+ if (Parent->getType () && Parent->getType ()->is <TupleType>()) {
671
+ tupleT = Parent->getType ()->castTo <TupleType>();
672
+ } else {
673
+ ExprContextInfo tupleCtxtInfo (DC, Parent);
674
+ for (auto possibleT : tupleCtxtInfo.getPossibleTypes ()) {
675
+ if (auto possibleTupleT = possibleT->getAs <TupleType>()) {
676
+ tupleT = possibleTupleT;
677
+ break ;
678
+ }
679
+ }
680
+ if (!tupleT)
681
+ return ;
682
+ }
683
+
634
684
unsigned Position = 0 ;
635
685
bool HasName;
636
686
if (getPositionInArgs (*DC, Parent, ParsedExpr, Position, HasName)) {
637
- recordPossibleType (
638
- Parent->getType ()->castTo <TupleType>()->getElementType (Position));
687
+ recordPossibleType (tupleT->getElementType (Position));
639
688
}
640
689
break ;
641
690
}
@@ -811,6 +860,7 @@ class ExprContextAnalyzer {
811
860
case ExprKind::PrefixUnary:
812
861
case ExprKind::Assign:
813
862
case ExprKind::Array:
863
+ case ExprKind::Dictionary:
814
864
return true ;
815
865
case ExprKind::UnresolvedMember:
816
866
return true ;
0 commit comments