@@ -3285,8 +3285,7 @@ namespace {
32853285 }
32863286
32873287 Expr *visitSuperRefExpr (SuperRefExpr *expr) {
3288- simplifyExprType (expr);
3289- return expr;
3288+ return simplifyExprType (expr);
32903289 }
32913290
32923291 Expr *visitTypeExpr (TypeExpr *expr) {
@@ -3710,55 +3709,34 @@ namespace {
37103709 return expr;
37113710 }
37123711
3713- Expr *visitCopyExpr (CopyExpr *expr) {
3714- auto toType = simplifyType (cs.getType (expr));
3715- cs.setType (expr, toType);
3712+ // / Given an expression that has a single sub-expression,
3713+ // / resolve and set the type for the expression and coerce
3714+ // / its sub-expression to the resolved type. This is a common
3715+ // / operation for expressions like Copy, Consume, and *Try.
3716+ template <class E >
3717+ Expr *transformExprWithSubExpr (E *expr) {
3718+ simplifyExprType (expr);
37163719
37173720 auto *subExpr = expr->getSubExpr ();
3718- auto type = simplifyType (cs.getType (subExpr));
3719-
3720- // Let's load the value associated with this try.
3721- if (type->hasLValueType ()) {
3722- subExpr = coerceToType (subExpr, type->getRValueType (),
3723- cs.getConstraintLocator (subExpr));
3724-
3725- if (!subExpr)
3726- return nullptr ;
3727- }
3721+ subExpr = coerceToType (subExpr, cs.getType (expr),
3722+ cs.getConstraintLocator (subExpr));
3723+ if (!subExpr)
3724+ return nullptr ;
37283725
37293726 expr->setSubExpr (subExpr);
3730-
37313727 return expr;
37323728 }
37333729
3734- Expr *visitConsumeExpr (ConsumeExpr *expr) {
3735- auto toType = simplifyType (cs.getType (expr));
3736- cs.setType (expr, toType);
3737-
3738- auto *subExpr = expr->getSubExpr ();
3739- auto type = simplifyType (cs.getType (subExpr));
3740-
3741- // Let's load the value associated with this consume.
3742- if (type->hasLValueType ()) {
3743- subExpr = coerceToType (subExpr, type->getRValueType (),
3744- cs.getConstraintLocator (subExpr));
3745-
3746- if (!subExpr)
3747- return nullptr ;
3748- }
3749-
3750- expr->setSubExpr (subExpr);
3730+ Expr *visitCopyExpr (CopyExpr *expr) {
3731+ return transformExprWithSubExpr (expr);
3732+ }
37513733
3752- return expr;
3734+ Expr *visitConsumeExpr (ConsumeExpr *expr) {
3735+ return transformExprWithSubExpr (expr);
37533736 }
37543737
37553738 Expr *visitAnyTryExpr (AnyTryExpr *expr) {
3756- simplifyExprType (expr);
3757-
3758- auto *subExpr = expr->getSubExpr ();
3759- expr->setSubExpr (coerceToType (subExpr, cs.getType (expr),
3760- cs.getConstraintLocator (subExpr)));
3761- return expr;
3739+ return transformExprWithSubExpr (expr);
37623740 }
37633741
37643742 Expr *visitOptionalTryExpr (OptionalTryExpr *expr) {
@@ -3775,16 +3753,8 @@ namespace {
37753753 // Nothing to do for Swift 4 and earlier!
37763754 return simplifyExprType (expr);
37773755 }
3778-
3779- Type exprType = simplifyType (cs.getType (expr));
37803756
3781- auto subExpr = coerceToType (expr->getSubExpr (), exprType,
3782- cs.getConstraintLocator (expr));
3783- if (!subExpr) return nullptr ;
3784- expr->setSubExpr (subExpr);
3785-
3786- cs.setType (expr, exprType);
3787- return expr;
3757+ return transformExprWithSubExpr (expr);
37883758 }
37893759
37903760 Expr *visitParenExpr (ParenExpr *expr) {
@@ -3793,7 +3763,7 @@ namespace {
37933763
37943764 // A ParenExpr can end up with a tuple type if it contains
37953765 // a pack expansion. Rewrite it to a TupleExpr.
3796- if (dyn_cast <PackExpansionExpr>(expr->getSubExpr ())) {
3766+ if (isa <PackExpansionExpr>(expr->getSubExpr ())) {
37973767 result = TupleExpr::create (ctx, expr->getLParenLoc (),
37983768 {expr->getSubExpr ()},
37993769 /* elementNames=*/ {},
@@ -3948,8 +3918,7 @@ namespace {
39483918 }
39493919
39503920 Expr *visitTupleElementExpr (TupleElementExpr *expr) {
3951- simplifyExprType (expr);
3952- return expr;
3921+ return simplifyExprType (expr);
39533922 }
39543923
39553924 Expr *visitCaptureListExpr (CaptureListExpr *expr) {
@@ -3976,12 +3945,7 @@ namespace {
39763945 }
39773946
39783947 Expr *visitVarargExpansionExpr (VarargExpansionExpr *expr) {
3979- simplifyExprType (expr);
3980-
3981- auto arrayTy = cs.getType (expr);
3982- expr->setSubExpr (coerceToType (expr->getSubExpr (), arrayTy,
3983- cs.getConstraintLocator (expr)));
3984- return expr;
3948+ return transformExprWithSubExpr (expr);
39853949 }
39863950
39873951 Expr *visitPackExpansionExpr (PackExpansionExpr *expr) {
0 commit comments