Skip to content

[SR-628][Sema] Improved handling of mixed lvalues & rvalues in tuple exprs #1150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2416,35 +2416,16 @@ namespace {

Type toType = simplifyType(expr->getType());

// Don't allow lvalues when indexing into a tuple expr.
// If we get any lvalues, add load exprs to convert the tuple to be all rvalues.
if (auto *tupleExpr = dyn_cast<TupleExpr>(base)) {
unsigned count = tupleExpr->getNumElements();
unsigned lvalues = 0;
auto &tc = cs.getTypeChecker();
SmallVector<TupleTypeElt, 4> tupleElts;
for (unsigned i = 0; i < count; i++) {
Expr *elementExpr = tupleExpr->getElement(i);
Type elementType = elementExpr->getType();
if (elementType->isLValueType()) {
lvalues++;
elementExpr->propagateLValueAccessKind(AccessKind::Read, true);
elementExpr = new (tc.Context) LoadExpr(elementExpr, elementType->getRValueType());
tupleExpr->setElement(i, elementExpr);
}
tupleElts.push_back(elementExpr->getType());
}

if (lvalues > 0) {
auto &Context = tupleExpr->getType()->getASTContext();
tupleExpr->setType(TupleType::get(tupleElts, Context));
toType = toType->getRValueType();
}
}
// If the result type is an rvalue and the base contains lvalues, need a full
// tuple coercion to properly load & set access kind on all underlying elements
// before taking a single element.
baseTy = base->getType();
if (!toType->isLValueType() && baseTy->isLValueType())
base = coerceToType(base, baseTy->getRValueType(), cs.getConstraintLocator(base));

return new (cs.getASTContext()) TupleElementExpr(base, dotLoc,
selected.choice.getTupleIndex(),
nameLoc.getBaseNameLoc(), toType);
selected.choice.getTupleIndex(),
nameLoc.getBaseNameLoc(), toType);
}

case OverloadChoiceKind::BaseType: {
Expand Down Expand Up @@ -4834,6 +4815,9 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}

case ConversionRestrictionKind::LValueToRValue: {
if (toType->is<TupleType>() || fromType->is<TupleType>())
break;

// Load from the lvalue.
expr->propagateLValueAccessKind(AccessKind::Read);
expr = new (tc.Context) LoadExpr(expr, fromType->getRValueType());
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
} else {
// When the base is a tuple rvalue, the member is always an rvalue.
auto tuple = choice.getBaseType()->castTo<TupleType>();
refType = tuple->getElementType(choice.getTupleIndex());
refType = tuple->getElementType(choice.getTupleIndex())->getRValueType();
}
break;
}
Expand Down
2 changes: 2 additions & 0 deletions test/expr/expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,5 @@ let _ = (x, 3).1
(x,y) = (2,3)
(x,4) = (1,2) // expected-error {{cannot assign to value: function call returns immutable value}}
(x,y).1 = 7 // expected-error {{cannot assign to immutable expression of type 'Int'}}
x = (x,(3,y)).1.1