@@ -2884,7 +2884,8 @@ namespace {
2884
2884
auto type = simplifyType (openedType);
2885
2885
cs.setType (expr, type);
2886
2886
2887
- assert (!type->is <UnresolvedType>());
2887
+ if (type->is <UnresolvedType>())
2888
+ return expr;
2888
2889
2889
2890
auto &ctx = cs.getASTContext ();
2890
2891
@@ -2907,11 +2908,14 @@ namespace {
2907
2908
ConcreteDeclRef witness = conformance.getWitnessByName (
2908
2909
conformingType->getRValueType (), constrName);
2909
2910
2910
- auto selectedOverload = solution.getOverloadChoice (
2911
+ auto selectedOverload = solution.getOverloadChoiceIfAvailable (
2911
2912
cs.getConstraintLocator (expr, ConstraintLocator::ConstructorMember));
2912
2913
2914
+ if (!selectedOverload)
2915
+ return nullptr ;
2916
+
2913
2917
auto fnType =
2914
- simplifyType (selectedOverload. openedType )->castTo <FunctionType>();
2918
+ simplifyType (selectedOverload-> openedType )->castTo <FunctionType>();
2915
2919
2916
2920
auto newArg = coerceCallArguments (
2917
2921
expr->getArg (), fnType, witness,
@@ -3211,8 +3215,18 @@ namespace {
3211
3215
// Determine the declaration selected for this overloaded reference.
3212
3216
auto memberLocator = cs.getConstraintLocator (expr,
3213
3217
ConstraintLocator::Member);
3214
- auto selected = solution.getOverloadChoice (memberLocator);
3218
+ auto selectedElt = solution.getOverloadChoiceIfAvailable (memberLocator);
3219
+
3220
+ if (!selectedElt) {
3221
+ // If constraint solving resolved this to an UnresolvedType, then we're
3222
+ // in an ambiguity tolerant mode used for diagnostic generation. Just
3223
+ // leave this as whatever type of member reference it already is.
3224
+ Type resultTy = simplifyType (cs.getType (expr));
3225
+ cs.setType (expr, resultTy);
3226
+ return expr;
3227
+ }
3215
3228
3229
+ auto selected = *selectedElt;
3216
3230
if (!selected.choice .getBaseType ()) {
3217
3231
// This is one of the "outer alternatives", meaning the innermost
3218
3232
// methods didn't work out.
@@ -3444,19 +3458,40 @@ namespace {
3444
3458
Expr *visitSubscriptExpr (SubscriptExpr *expr) {
3445
3459
auto *memberLocator =
3446
3460
cs.getConstraintLocator (expr, ConstraintLocator::SubscriptMember);
3447
- auto overload = solution.getOverloadChoice (memberLocator);
3461
+ auto overload = solution.getOverloadChoiceIfAvailable (memberLocator);
3462
+
3463
+ // Handles situation where there was a solution available but it didn't
3464
+ // have a proper overload selected from subscript call, might be because
3465
+ // solver was allowed to return free or unresolved types, which can
3466
+ // happen while running diagnostics on one of the expressions.
3467
+ if (!overload) {
3468
+ auto *base = expr->getBase ();
3469
+ auto &de = cs.getASTContext ().Diags ;
3470
+ auto baseType = cs.getType (base);
3448
3471
3449
- if (overload.choice .getKind () ==
3472
+ if (auto errorType = baseType->getAs <ErrorType>()) {
3473
+ de.diagnose (base->getLoc (), diag::cannot_subscript_base,
3474
+ errorType->getOriginalType ())
3475
+ .highlight (base->getSourceRange ());
3476
+ } else {
3477
+ de.diagnose (base->getLoc (), diag::cannot_subscript_ambiguous_base)
3478
+ .highlight (base->getSourceRange ());
3479
+ }
3480
+
3481
+ return nullptr ;
3482
+ }
3483
+
3484
+ if (overload->choice .getKind () ==
3450
3485
OverloadChoiceKind::KeyPathDynamicMemberLookup) {
3451
3486
return buildDynamicMemberLookupRef (
3452
3487
expr, expr->getBase (), expr->getIndex ()->getStartLoc (), SourceLoc (),
3453
- overload, memberLocator);
3488
+ * overload, memberLocator);
3454
3489
}
3455
3490
3456
3491
return buildSubscript (
3457
3492
expr->getBase (), expr->getIndex (), expr->getArgumentLabels (),
3458
3493
expr->hasTrailingClosure (), cs.getConstraintLocator (expr),
3459
- expr->isImplicit (), expr->getAccessSemantics (), overload);
3494
+ expr->isImplicit (), expr->getAccessSemantics (), * overload);
3460
3495
}
3461
3496
3462
3497
// / "Finish" an array expression by filling in the semantic expression.
@@ -4327,7 +4362,8 @@ namespace {
4327
4362
expr->getSubPattern ()->forEachVariable ([](VarDecl *VD) {
4328
4363
VD->setInvalid ();
4329
4364
});
4330
- if (!SuppressDiagnostics) {
4365
+ if (!SuppressDiagnostics
4366
+ && !cs.getType (simplified)->is <UnresolvedType>()) {
4331
4367
auto &de = cs.getASTContext ().Diags ;
4332
4368
de.diagnose (simplified->getLoc (), diag::pattern_in_expr,
4333
4369
expr->getSubPattern ()->getKind ());
@@ -4806,12 +4842,18 @@ namespace {
4806
4842
// If this is an unresolved link, make sure we resolved it.
4807
4843
if (kind == KeyPathExpr::Component::Kind::UnresolvedProperty ||
4808
4844
kind == KeyPathExpr::Component::Kind::UnresolvedSubscript) {
4809
- auto foundDecl = solution.getOverloadChoice (locator);
4845
+ auto foundDecl = solution.getOverloadChoiceIfAvailable (locator);
4846
+ if (!foundDecl) {
4847
+ // If we couldn't resolve the component, leave it alone.
4848
+ resolvedComponents.push_back (origComponent);
4849
+ componentTy = origComponent.getComponentType ();
4850
+ continue ;
4851
+ }
4810
4852
4811
4853
isDynamicMember =
4812
- foundDecl. choice .getKind () ==
4854
+ foundDecl-> choice .getKind () ==
4813
4855
OverloadChoiceKind::DynamicMemberLookup ||
4814
- foundDecl. choice .getKind () ==
4856
+ foundDecl-> choice .getKind () ==
4815
4857
OverloadChoiceKind::KeyPathDynamicMemberLookup;
4816
4858
4817
4859
// If this was a @dynamicMemberLookup property, then we actually
@@ -4842,9 +4884,11 @@ namespace {
4842
4884
case KeyPathExpr::Component::Kind::OptionalChain: {
4843
4885
didOptionalChain = true ;
4844
4886
// Chaining always forces the element to be an rvalue.
4845
- assert (!componentTy->hasUnresolvedType ());
4846
4887
auto objectTy =
4847
4888
componentTy->getWithoutSpecifierType ()->getOptionalObjectType ();
4889
+ if (componentTy->hasUnresolvedType () && !objectTy) {
4890
+ objectTy = componentTy;
4891
+ }
4848
4892
assert (objectTy);
4849
4893
4850
4894
auto loc = origComponent.getLoc ();
@@ -4896,8 +4940,8 @@ namespace {
4896
4940
}
4897
4941
4898
4942
// Wrap a non-optional result if there was chaining involved.
4899
- assert (!componentTy->hasUnresolvedType ());
4900
4943
if (didOptionalChain && componentTy &&
4944
+ !componentTy->hasUnresolvedType () &&
4901
4945
!componentTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
4902
4946
assert (leafTy->getOptionalObjectType ()->isEqual (
4903
4947
componentTy->getWithoutSpecifierType ()));
@@ -4916,8 +4960,8 @@ namespace {
4916
4960
4917
4961
// The final component type ought to line up with the leaf type of the
4918
4962
// key path.
4919
- assert (!componentTy->hasUnresolvedType ());
4920
- assert ( componentTy->getWithoutSpecifierType ()->isEqual (leafTy));
4963
+ assert (!componentTy || componentTy ->hasUnresolvedType ()
4964
+ || componentTy->getWithoutSpecifierType ()->isEqual (leafTy));
4921
4965
4922
4966
if (!isFunctionType)
4923
4967
return E;
@@ -5022,13 +5066,18 @@ namespace {
5022
5066
5023
5067
// Unwrap the last component type, preserving @lvalue-ness.
5024
5068
auto optionalTy = components.back ().getComponentType ();
5025
- assert (!optionalTy->hasUnresolvedType ());
5026
5069
Type objectTy;
5027
5070
if (auto lvalue = optionalTy->getAs <LValueType>()) {
5028
5071
objectTy = lvalue->getObjectType ()->getOptionalObjectType ();
5072
+ if (optionalTy->hasUnresolvedType () && !objectTy) {
5073
+ objectTy = optionalTy;
5074
+ }
5029
5075
objectTy = LValueType::get (objectTy);
5030
5076
} else {
5031
5077
objectTy = optionalTy->getOptionalObjectType ();
5078
+ if (optionalTy->hasUnresolvedType () && !objectTy) {
5079
+ objectTy = optionalTy;
5080
+ }
5032
5081
}
5033
5082
assert (objectTy);
5034
5083
@@ -5457,9 +5506,11 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType) {
5457
5506
Type toInstanceType = toType;
5458
5507
5459
5508
// Look through metatypes
5460
- while (fromInstanceType->is <AnyMetatypeType>() &&
5509
+ while ((fromInstanceType->is <UnresolvedType>() ||
5510
+ fromInstanceType->is <AnyMetatypeType>()) &&
5461
5511
toInstanceType->is <ExistentialMetatypeType>()) {
5462
- fromInstanceType = fromInstanceType->castTo <AnyMetatypeType>()->getInstanceType ();
5512
+ if (!fromInstanceType->is <UnresolvedType>())
5513
+ fromInstanceType = fromInstanceType->castTo <AnyMetatypeType>()->getInstanceType ();
5463
5514
toInstanceType = toInstanceType->castTo <ExistentialMetatypeType>()->getInstanceType ();
5464
5515
}
5465
5516
@@ -5659,6 +5710,11 @@ Expr *ExprRewriter::coerceCallArguments(
5659
5710
LocatorPathElt::ApplyArgToParam (argIdx, paramIdx, flags));
5660
5711
};
5661
5712
5713
+ bool matchCanFail =
5714
+ llvm::any_of (params, [](const AnyFunctionType::Param ¶m) {
5715
+ return param.getPlainType ()->hasUnresolvedType ();
5716
+ });
5717
+
5662
5718
// Determine whether this application has curried self.
5663
5719
bool skipCurriedSelf = apply ? hasCurriedSelf (cs, callee, apply) : true ;
5664
5720
// Determine the parameter bindings.
@@ -5716,7 +5772,9 @@ Expr *ExprRewriter::coerceCallArguments(
5716
5772
args, params, paramInfo, unlabeledTrailingClosureIndex,
5717
5773
/* allowFixes=*/ false , listener, trailingClosureMatching);
5718
5774
5719
- assert (callArgumentMatch && " Call arguments did not match up?" );
5775
+ assert ((matchCanFail || callArgumentMatch) &&
5776
+ " Call arguments did not match up?" );
5777
+ (void )matchCanFail;
5720
5778
5721
5779
auto parameterBindings = std::move (callArgumentMatch->parameterBindings );
5722
5780
@@ -6453,7 +6511,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
6453
6511
if (knownRestriction != solution.ConstraintRestrictions .end ()) {
6454
6512
switch (knownRestriction->second ) {
6455
6513
case ConversionRestrictionKind::DeepEquality: {
6456
- assert (!toType->hasUnresolvedType ());
6514
+ if (toType->hasUnresolvedType ())
6515
+ break ;
6457
6516
6458
6517
// HACK: Fix problem related to Swift 4 mode (with assertions),
6459
6518
// since Swift 4 mode allows passing arguments with extra parens
@@ -6999,8 +7058,9 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
6999
7058
break ;
7000
7059
}
7001
7060
7002
- assert (!fromType->hasUnresolvedType ());
7003
- assert (!toType->hasUnresolvedType ());
7061
+ // Unresolved types come up in diagnostics for lvalue and inout types.
7062
+ if (fromType->hasUnresolvedType () || toType->hasUnresolvedType ())
7063
+ return cs.cacheType (new (ctx) UnresolvedTypeConversionExpr (expr, toType));
7004
7064
7005
7065
// Use an opaque type to abstract a value of the underlying concrete type.
7006
7066
if (toType->getAs <OpaqueTypeArchetypeType>()) {
@@ -7577,14 +7637,19 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7577
7637
// FIXME: handle unwrapping everywhere else
7578
7638
assert (!unwrapResult);
7579
7639
7580
- assert (!cs.getType (fn)->is <UnresolvedType>());
7640
+ // If this is an UnresolvedType in the system, preserve it.
7641
+ if (cs.getType (fn)->is <UnresolvedType>()) {
7642
+ cs.setType (apply, cs.getType (fn));
7643
+ return apply;
7644
+ }
7581
7645
7582
7646
// We have a type constructor.
7583
7647
auto metaTy = cs.getType (fn)->castTo <AnyMetatypeType>();
7584
7648
auto ty = metaTy->getInstanceType ();
7585
7649
7586
7650
// If we're "constructing" a tuple type, it's simply a conversion.
7587
7651
if (auto tupleTy = ty->getAs <TupleType>()) {
7652
+ // FIXME: Need an AST to represent this properly.
7588
7653
return coerceToType (apply->getArg (), tupleTy, locator);
7589
7654
}
7590
7655
@@ -7593,14 +7658,19 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7593
7658
auto *ctorLocator =
7594
7659
cs.getConstraintLocator (locator, {ConstraintLocator::ApplyFunction,
7595
7660
ConstraintLocator::ConstructorMember});
7596
- auto selected = solution.getOverloadChoice (ctorLocator);
7661
+ auto selected = solution.getOverloadChoiceIfAvailable (ctorLocator);
7662
+ if (!selected) {
7663
+ assert (ty->hasError () || ty->hasUnresolvedType ());
7664
+ cs.setType (apply, ty);
7665
+ return apply;
7666
+ }
7597
7667
7598
7668
assert (ty->getNominalOrBoundGenericNominal () || ty->is <DynamicSelfType>() ||
7599
7669
ty->isExistentialType () || ty->is <ArchetypeType>());
7600
7670
7601
7671
// Consider the constructor decl reference expr 'implicit', but the
7602
7672
// constructor call expr itself has the apply's 'implicitness'.
7603
- Expr *declRef = buildMemberRef (fn, /* dotLoc=*/ SourceLoc (), selected,
7673
+ Expr *declRef = buildMemberRef (fn, /* dotLoc=*/ SourceLoc (), * selected,
7604
7674
DeclNameLoc (fn->getEndLoc ()), locator,
7605
7675
ctorLocator, /* implicit=*/ true ,
7606
7676
AccessSemantics::Ordinary);
0 commit comments