@@ -2050,6 +2050,7 @@ TypeChecker::typeCheckExpression(
20502050 target.setExpr (expr);
20512051 return None;
20522052 }
2053+ target.setExpr (expr);
20532054
20542055 // Construct a constraint system from this expression.
20552056 ConstraintSystemOptions csOptions = ConstraintSystemFlags::AllowFixes;
@@ -2070,46 +2071,34 @@ TypeChecker::typeCheckExpression(
20702071 // diagnostics and is a hint for various performance optimizations.
20712072 // FIXME: Look through LoadExpr. This is an egregious hack due to the
20722073 // way typeCheckExprIndependently works.
2073- TypeLoc convertType = target.getExprConversionTypeLoc ();
20742074 Expr *contextualTypeExpr = expr;
20752075 if (auto loadExpr = dyn_cast_or_null<LoadExpr>(contextualTypeExpr))
20762076 contextualTypeExpr = loadExpr->getSubExpr ();
20772077 cs.setContextualType (
2078- contextualTypeExpr, convertType,
2079- target.getExprContextualTypePurpose (),
2080- target.infersOpaqueReturnType ());
2081-
2082- // If the convertType is *only* provided for that hint, then null it out so
2083- // that we don't later treat it as an actual conversion constraint.
2084- if (target.contextualTypeIsOnlyAHint ())
2085- convertType = TypeLoc ();
2078+ contextualTypeExpr,
2079+ target.getExprContextualTypeLoc (),
2080+ target.getExprContextualTypePurpose ());
20862081
20872082 // If the client can handle unresolved type variables, leave them in the
20882083 // system.
20892084 auto allowFreeTypeVariables = FreeTypeVariableBinding::Disallow;
20902085 if (options.contains (TypeCheckExprFlags::AllowUnresolvedTypeVariables))
20912086 allowFreeTypeVariables = FreeTypeVariableBinding::UnresolvedType;
20922087
2093- Type convertTo = convertType.getType ();
2094-
2088+ // If the target requires an optional of some type, form a new appropriate
2089+ // type variable and update the target's type with an optional of that
2090+ // type variable.
20952091 if (target.isOptionalSomePatternInit ()) {
2096- assert (!convertTo && " convertType and type check options conflict" );
2092+ assert (!target.getExprContextualType () &&
2093+ " some pattern cannot have contextual type pre-configured" );
20972094 auto *convertTypeLocator =
20982095 cs.getConstraintLocator (expr, LocatorPathElt::ContextualType ());
20992096 Type var = cs.createTypeVariable (convertTypeLocator, TVO_CanBindToNoEscape);
2100- convertTo = getOptionalType (expr->getLoc (), var);
2101- } else if (target.getExprContextualTypePurpose ()
2102- == CTP_AutoclosureDefaultParameter) {
2103- // FIXME: Hack around the convertTo adjustment below, which we want to
2104- // eliminate.
2105- convertTo = Type (target.getAsAutoclosureParamType ());
2097+ target.setExprConversionType (getOptionalType (expr->getLoc (), var));
21062098 }
21072099
21082100 // Attempt to solve the constraint system.
2109- SolutionApplicationTarget innerTarget (
2110- expr, dc, target.getExprContextualTypePurpose (), convertTo,
2111- target.isDiscardedExpr ());
2112- auto viable = cs.solve (innerTarget, listener, allowFreeTypeVariables);
2101+ auto viable = cs.solve (target, listener, allowFreeTypeVariables);
21132102 if (!viable) {
21142103 target.setExpr (expr);
21152104 return None;
@@ -2120,7 +2109,8 @@ TypeChecker::typeCheckExpression(
21202109 // because they will leak out into arbitrary places in the resultant AST.
21212110 if (options.contains (TypeCheckExprFlags::AllowUnresolvedTypeVariables) &&
21222111 (viable->size () != 1 ||
2123- (convertType.getType () && convertType.getType ()->hasUnresolvedType ()))) {
2112+ (target.getExprConversionType () &&
2113+ target.getExprConversionType ()->hasUnresolvedType ()))) {
21242114 // FIXME: This hack should only be needed for CSDiag.
21252115 unresolvedTypeExprs = true ;
21262116 return target;
@@ -2134,10 +2124,6 @@ TypeChecker::typeCheckExpression(
21342124 // Apply the solution to the expression.
21352125 bool performingDiagnostics =
21362126 options.contains (TypeCheckExprFlags::SubExpressionDiagnostics);
2137- // FIXME: HACK! Copy over the inner target's expression info.
2138- target.setExpr (innerTarget.getAsExpr ());
2139- if (convertTo.isNull ())
2140- target.setExprConversionType (convertTo);
21412127 auto resultTarget = cs.applySolution (solution, target, performingDiagnostics);
21422128 if (!resultTarget) {
21432129 // Failure already diagnosed, above, as part of applying the solution.
0 commit comments