@@ -2894,7 +2894,8 @@ namespace {
2894
2894
auto type = simplifyType (openedType);
2895
2895
cs.setType (expr, type);
2896
2896
2897
- assert (!type->is <UnresolvedType>());
2897
+ if (type->is <UnresolvedType>())
2898
+ return expr;
2898
2899
2899
2900
auto &ctx = cs.getASTContext ();
2900
2901
@@ -2917,11 +2918,14 @@ namespace {
2917
2918
ConcreteDeclRef witness = conformance.getWitnessByName (
2918
2919
conformingType->getRValueType (), constrName);
2919
2920
2920
- auto selectedOverload = solution.getOverloadChoice (
2921
+ auto selectedOverload = solution.getOverloadChoiceIfAvailable (
2921
2922
cs.getConstraintLocator (expr, ConstraintLocator::ConstructorMember));
2922
2923
2924
+ if (!selectedOverload)
2925
+ return nullptr ;
2926
+
2923
2927
auto fnType =
2924
- simplifyType (selectedOverload. openedType )->castTo <FunctionType>();
2928
+ simplifyType (selectedOverload-> openedType )->castTo <FunctionType>();
2925
2929
2926
2930
auto newArg = coerceCallArguments (
2927
2931
expr->getArg (), fnType, witness,
@@ -3221,8 +3225,18 @@ namespace {
3221
3225
// Determine the declaration selected for this overloaded reference.
3222
3226
auto memberLocator = cs.getConstraintLocator (expr,
3223
3227
ConstraintLocator::Member);
3224
- auto selected = solution.getOverloadChoice (memberLocator);
3228
+ auto selectedElt = solution.getOverloadChoiceIfAvailable (memberLocator);
3229
+
3230
+ if (!selectedElt) {
3231
+ // If constraint solving resolved this to an UnresolvedType, then we're
3232
+ // in an ambiguity tolerant mode used for diagnostic generation. Just
3233
+ // leave this as whatever type of member reference it already is.
3234
+ Type resultTy = simplifyType (cs.getType (expr));
3235
+ cs.setType (expr, resultTy);
3236
+ return expr;
3237
+ }
3225
3238
3239
+ auto selected = *selectedElt;
3226
3240
if (!selected.choice .getBaseType ()) {
3227
3241
// This is one of the "outer alternatives", meaning the innermost
3228
3242
// methods didn't work out.
@@ -3454,19 +3468,40 @@ namespace {
3454
3468
Expr *visitSubscriptExpr (SubscriptExpr *expr) {
3455
3469
auto *memberLocator =
3456
3470
cs.getConstraintLocator (expr, ConstraintLocator::SubscriptMember);
3457
- auto overload = solution.getOverloadChoice (memberLocator);
3471
+ auto overload = solution.getOverloadChoiceIfAvailable (memberLocator);
3472
+
3473
+ // Handles situation where there was a solution available but it didn't
3474
+ // have a proper overload selected from subscript call, might be because
3475
+ // solver was allowed to return free or unresolved types, which can
3476
+ // happen while running diagnostics on one of the expressions.
3477
+ if (!overload) {
3478
+ auto *base = expr->getBase ();
3479
+ auto &de = cs.getASTContext ().Diags ;
3480
+ auto baseType = cs.getType (base);
3458
3481
3459
- if (overload.choice .getKind () ==
3482
+ if (auto errorType = baseType->getAs <ErrorType>()) {
3483
+ de.diagnose (base->getLoc (), diag::cannot_subscript_base,
3484
+ errorType->getOriginalType ())
3485
+ .highlight (base->getSourceRange ());
3486
+ } else {
3487
+ de.diagnose (base->getLoc (), diag::cannot_subscript_ambiguous_base)
3488
+ .highlight (base->getSourceRange ());
3489
+ }
3490
+
3491
+ return nullptr ;
3492
+ }
3493
+
3494
+ if (overload->choice .getKind () ==
3460
3495
OverloadChoiceKind::KeyPathDynamicMemberLookup) {
3461
3496
return buildDynamicMemberLookupRef (
3462
3497
expr, expr->getBase (), expr->getIndex ()->getStartLoc (), SourceLoc (),
3463
- overload, memberLocator);
3498
+ * overload, memberLocator);
3464
3499
}
3465
3500
3466
3501
return buildSubscript (
3467
3502
expr->getBase (), expr->getIndex (), expr->getArgumentLabels (),
3468
3503
expr->hasTrailingClosure (), cs.getConstraintLocator (expr),
3469
- expr->isImplicit (), expr->getAccessSemantics (), overload);
3504
+ expr->isImplicit (), expr->getAccessSemantics (), * overload);
3470
3505
}
3471
3506
3472
3507
// / "Finish" an array expression by filling in the semantic expression.
@@ -4337,7 +4372,8 @@ namespace {
4337
4372
expr->getSubPattern ()->forEachVariable ([](VarDecl *VD) {
4338
4373
VD->setInvalid ();
4339
4374
});
4340
- if (!SuppressDiagnostics) {
4375
+ if (!SuppressDiagnostics
4376
+ && !cs.getType (simplified)->is <UnresolvedType>()) {
4341
4377
auto &de = cs.getASTContext ().Diags ;
4342
4378
de.diagnose (simplified->getLoc (), diag::pattern_in_expr,
4343
4379
expr->getSubPattern ()->getKind ());
@@ -4816,12 +4852,18 @@ namespace {
4816
4852
// If this is an unresolved link, make sure we resolved it.
4817
4853
if (kind == KeyPathExpr::Component::Kind::UnresolvedProperty ||
4818
4854
kind == KeyPathExpr::Component::Kind::UnresolvedSubscript) {
4819
- auto foundDecl = solution.getOverloadChoice (locator);
4855
+ auto foundDecl = solution.getOverloadChoiceIfAvailable (locator);
4856
+ if (!foundDecl) {
4857
+ // If we couldn't resolve the component, leave it alone.
4858
+ resolvedComponents.push_back (origComponent);
4859
+ componentTy = origComponent.getComponentType ();
4860
+ continue ;
4861
+ }
4820
4862
4821
4863
isDynamicMember =
4822
- foundDecl. choice .getKind () ==
4864
+ foundDecl-> choice .getKind () ==
4823
4865
OverloadChoiceKind::DynamicMemberLookup ||
4824
- foundDecl. choice .getKind () ==
4866
+ foundDecl-> choice .getKind () ==
4825
4867
OverloadChoiceKind::KeyPathDynamicMemberLookup;
4826
4868
4827
4869
// If this was a @dynamicMemberLookup property, then we actually
@@ -4852,9 +4894,11 @@ namespace {
4852
4894
case KeyPathExpr::Component::Kind::OptionalChain: {
4853
4895
didOptionalChain = true ;
4854
4896
// Chaining always forces the element to be an rvalue.
4855
- assert (!componentTy->hasUnresolvedType ());
4856
4897
auto objectTy =
4857
4898
componentTy->getWithoutSpecifierType ()->getOptionalObjectType ();
4899
+ if (componentTy->hasUnresolvedType () && !objectTy) {
4900
+ objectTy = componentTy;
4901
+ }
4858
4902
assert (objectTy);
4859
4903
4860
4904
auto loc = origComponent.getLoc ();
@@ -4906,8 +4950,8 @@ namespace {
4906
4950
}
4907
4951
4908
4952
// Wrap a non-optional result if there was chaining involved.
4909
- assert (!componentTy->hasUnresolvedType ());
4910
4953
if (didOptionalChain && componentTy &&
4954
+ !componentTy->hasUnresolvedType () &&
4911
4955
!componentTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
4912
4956
assert (leafTy->getOptionalObjectType ()->isEqual (
4913
4957
componentTy->getWithoutSpecifierType ()));
@@ -4926,8 +4970,8 @@ namespace {
4926
4970
4927
4971
// The final component type ought to line up with the leaf type of the
4928
4972
// key path.
4929
- assert (!componentTy->hasUnresolvedType ());
4930
- assert ( componentTy->getWithoutSpecifierType ()->isEqual (leafTy));
4973
+ assert (!componentTy || componentTy ->hasUnresolvedType ()
4974
+ || componentTy->getWithoutSpecifierType ()->isEqual (leafTy));
4931
4975
4932
4976
if (!isFunctionType)
4933
4977
return E;
@@ -5038,13 +5082,18 @@ namespace {
5038
5082
5039
5083
// Unwrap the last component type, preserving @lvalue-ness.
5040
5084
auto optionalTy = components.back ().getComponentType ();
5041
- assert (!optionalTy->hasUnresolvedType ());
5042
5085
Type objectTy;
5043
5086
if (auto lvalue = optionalTy->getAs <LValueType>()) {
5044
5087
objectTy = lvalue->getObjectType ()->getOptionalObjectType ();
5088
+ if (optionalTy->hasUnresolvedType () && !objectTy) {
5089
+ objectTy = optionalTy;
5090
+ }
5045
5091
objectTy = LValueType::get (objectTy);
5046
5092
} else {
5047
5093
objectTy = optionalTy->getOptionalObjectType ();
5094
+ if (optionalTy->hasUnresolvedType () && !objectTy) {
5095
+ objectTy = optionalTy;
5096
+ }
5048
5097
}
5049
5098
assert (objectTy);
5050
5099
@@ -5473,9 +5522,11 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType) {
5473
5522
Type toInstanceType = toType;
5474
5523
5475
5524
// Look through metatypes
5476
- while (fromInstanceType->is <AnyMetatypeType>() &&
5525
+ while ((fromInstanceType->is <UnresolvedType>() ||
5526
+ fromInstanceType->is <AnyMetatypeType>()) &&
5477
5527
toInstanceType->is <ExistentialMetatypeType>()) {
5478
- fromInstanceType = fromInstanceType->castTo <AnyMetatypeType>()->getInstanceType ();
5528
+ if (!fromInstanceType->is <UnresolvedType>())
5529
+ fromInstanceType = fromInstanceType->castTo <AnyMetatypeType>()->getInstanceType ();
5479
5530
toInstanceType = toInstanceType->castTo <ExistentialMetatypeType>()->getInstanceType ();
5480
5531
}
5481
5532
@@ -5675,6 +5726,11 @@ Expr *ExprRewriter::coerceCallArguments(
5675
5726
LocatorPathElt::ApplyArgToParam (argIdx, paramIdx, flags));
5676
5727
};
5677
5728
5729
+ bool matchCanFail =
5730
+ llvm::any_of (params, [](const AnyFunctionType::Param ¶m) {
5731
+ return param.getPlainType ()->hasUnresolvedType ();
5732
+ });
5733
+
5678
5734
// Determine whether this application has curried self.
5679
5735
bool skipCurriedSelf = apply ? hasCurriedSelf (cs, callee, apply) : true ;
5680
5736
// Determine the parameter bindings.
@@ -5732,7 +5788,9 @@ Expr *ExprRewriter::coerceCallArguments(
5732
5788
args, params, paramInfo, unlabeledTrailingClosureIndex,
5733
5789
/* allowFixes=*/ false , listener, trailingClosureMatching);
5734
5790
5735
- assert (callArgumentMatch && " Call arguments did not match up?" );
5791
+ assert ((matchCanFail || callArgumentMatch) &&
5792
+ " Call arguments did not match up?" );
5793
+ (void )matchCanFail;
5736
5794
5737
5795
auto parameterBindings = std::move (callArgumentMatch->parameterBindings );
5738
5796
@@ -6469,7 +6527,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
6469
6527
if (knownRestriction != solution.ConstraintRestrictions .end ()) {
6470
6528
switch (knownRestriction->second ) {
6471
6529
case ConversionRestrictionKind::DeepEquality: {
6472
- assert (!toType->hasUnresolvedType ());
6530
+ if (toType->hasUnresolvedType ())
6531
+ break ;
6473
6532
6474
6533
// HACK: Fix problem related to Swift 4 mode (with assertions),
6475
6534
// since Swift 4 mode allows passing arguments with extra parens
@@ -7030,8 +7089,9 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
7030
7089
break ;
7031
7090
}
7032
7091
7033
- assert (!fromType->hasUnresolvedType ());
7034
- assert (!toType->hasUnresolvedType ());
7092
+ // Unresolved types come up in diagnostics for lvalue and inout types.
7093
+ if (fromType->hasUnresolvedType () || toType->hasUnresolvedType ())
7094
+ return cs.cacheType (new (ctx) UnresolvedTypeConversionExpr (expr, toType));
7035
7095
7036
7096
// Use an opaque type to abstract a value of the underlying concrete type.
7037
7097
if (toType->getAs <OpaqueTypeArchetypeType>()) {
@@ -7608,14 +7668,19 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7608
7668
// FIXME: handle unwrapping everywhere else
7609
7669
assert (!unwrapResult);
7610
7670
7611
- assert (!cs.getType (fn)->is <UnresolvedType>());
7671
+ // If this is an UnresolvedType in the system, preserve it.
7672
+ if (cs.getType (fn)->is <UnresolvedType>()) {
7673
+ cs.setType (apply, cs.getType (fn));
7674
+ return apply;
7675
+ }
7612
7676
7613
7677
// We have a type constructor.
7614
7678
auto metaTy = cs.getType (fn)->castTo <AnyMetatypeType>();
7615
7679
auto ty = metaTy->getInstanceType ();
7616
7680
7617
7681
// If we're "constructing" a tuple type, it's simply a conversion.
7618
7682
if (auto tupleTy = ty->getAs <TupleType>()) {
7683
+ // FIXME: Need an AST to represent this properly.
7619
7684
return coerceToType (apply->getArg (), tupleTy, locator);
7620
7685
}
7621
7686
@@ -7624,14 +7689,19 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7624
7689
auto *ctorLocator =
7625
7690
cs.getConstraintLocator (locator, {ConstraintLocator::ApplyFunction,
7626
7691
ConstraintLocator::ConstructorMember});
7627
- auto selected = solution.getOverloadChoice (ctorLocator);
7692
+ auto selected = solution.getOverloadChoiceIfAvailable (ctorLocator);
7693
+ if (!selected) {
7694
+ assert (ty->hasError () || ty->hasUnresolvedType ());
7695
+ cs.setType (apply, ty);
7696
+ return apply;
7697
+ }
7628
7698
7629
7699
assert (ty->getNominalOrBoundGenericNominal () || ty->is <DynamicSelfType>() ||
7630
7700
ty->isExistentialType () || ty->is <ArchetypeType>());
7631
7701
7632
7702
// Consider the constructor decl reference expr 'implicit', but the
7633
7703
// constructor call expr itself has the apply's 'implicitness'.
7634
- Expr *declRef = buildMemberRef (fn, /* dotLoc=*/ SourceLoc (), selected,
7704
+ Expr *declRef = buildMemberRef (fn, /* dotLoc=*/ SourceLoc (), * selected,
7635
7705
DeclNameLoc (fn->getEndLoc ()), locator,
7636
7706
ctorLocator, /* implicit=*/ true ,
7637
7707
AccessSemantics::Ordinary);
0 commit comments