@@ -2416,35 +2416,16 @@ namespace {
2416
2416
2417
2417
Type toType = simplifyType (expr->getType ());
2418
2418
2419
- // Don't allow lvalues when indexing into a tuple expr.
2420
- // If we get any lvalues, add load exprs to convert the tuple to be all rvalues.
2421
- if (auto *tupleExpr = dyn_cast<TupleExpr>(base)) {
2422
- unsigned count = tupleExpr->getNumElements ();
2423
- unsigned lvalues = 0 ;
2424
- auto &tc = cs.getTypeChecker ();
2425
- SmallVector<TupleTypeElt, 4 > tupleElts;
2426
- for (unsigned i = 0 ; i < count; i++) {
2427
- Expr *elementExpr = tupleExpr->getElement (i);
2428
- Type elementType = elementExpr->getType ();
2429
- if (elementType->isLValueType ()) {
2430
- lvalues++;
2431
- elementExpr->propagateLValueAccessKind (AccessKind::Read, true );
2432
- elementExpr = new (tc.Context ) LoadExpr (elementExpr, elementType->getRValueType ());
2433
- tupleExpr->setElement (i, elementExpr);
2434
- }
2435
- tupleElts.push_back (elementExpr->getType ());
2436
- }
2437
-
2438
- if (lvalues > 0 ) {
2439
- auto &Context = tupleExpr->getType ()->getASTContext ();
2440
- tupleExpr->setType (TupleType::get (tupleElts, Context));
2441
- toType = toType->getRValueType ();
2442
- }
2443
- }
2419
+ // If the result type is an rvalue and the base contains lvalues, need a full
2420
+ // tuple coercion to properly load & set access kind on all underlying elements
2421
+ // before taking a single element.
2422
+ baseTy = base->getType ();
2423
+ if (!toType->isLValueType () && baseTy->isLValueType ())
2424
+ base = coerceToType (base, baseTy->getRValueType (), cs.getConstraintLocator (base));
2444
2425
2445
2426
return new (cs.getASTContext ()) TupleElementExpr (base, dotLoc,
2446
- selected.choice .getTupleIndex (),
2447
- nameLoc.getBaseNameLoc (), toType);
2427
+ selected.choice .getTupleIndex (),
2428
+ nameLoc.getBaseNameLoc (), toType);
2448
2429
}
2449
2430
2450
2431
case OverloadChoiceKind::BaseType: {
@@ -4834,6 +4815,9 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
4834
4815
}
4835
4816
4836
4817
case ConversionRestrictionKind::LValueToRValue: {
4818
+ if (toType->is <TupleType>() || fromType->is <TupleType>())
4819
+ break ;
4820
+
4837
4821
// Load from the lvalue.
4838
4822
expr->propagateLValueAccessKind (AccessKind::Read);
4839
4823
expr = new (tc.Context ) LoadExpr (expr, fromType->getRValueType ());
0 commit comments