Skip to content

Commit 2281ff0

Browse files
committed
Sema: Fix another crash with tuple to Any conversion
An earlier patch fixed the case where some tuple elements were lvalue types. However this only looked into tuples nested one level deep, when a more correct fix checks the lvalue recursive property of the type.
1 parent 4817bd4 commit 2281ff0

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4748,31 +4748,10 @@ Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
47484748
cs.getType(result)));
47494749
}
47504750

4751-
// If the type we are trying to coerce is a tuple, let's look through
4752-
// its elements to see if there are any LValue types present, such requires
4753-
// load or address-of operation first before proceeding with erasure.
4751+
// Load tuples with lvalue elements.
47544752
if (auto tupleType = fromType->getAs<TupleType>()) {
4755-
bool coerceToRValue = false;
4756-
for (auto element : tupleType->getElements()) {
4757-
if (element.getType()->is<LValueType>()) {
4758-
coerceToRValue = true;
4759-
break;
4760-
}
4761-
}
4762-
4763-
// Tuple has one or more LValue types associated with it,
4764-
// which requires coercion to RValue, let's perform it here by creating
4765-
// new tuple type with LValue(s) stripped off and coercing
4766-
// expression to that type, which would do required transformation.
4767-
if (coerceToRValue) {
4768-
SmallVector<TupleTypeElt, 2> elements;
4769-
for (auto &element : tupleType->getElements())
4770-
elements.push_back(TupleTypeElt(element.getType()->getRValueType(),
4771-
element.getName(),
4772-
element.getParameterFlags()));
4773-
4774-
// New type is guaranteed to be a tuple because source type is one.
4775-
auto toTuple = TupleType::get(elements, ctx)->castTo<TupleType>();
4753+
if (tupleType->isLValueType()) {
4754+
auto toTuple = tupleType->getRValueType()->castTo<TupleType>();
47764755
SmallVector<int, 4> sources;
47774756
SmallVector<unsigned, 4> variadicArgs;
47784757
bool failed = computeTupleShuffle(tupleType, toTuple,

test/Constraints/tuple.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,14 @@ func f(a : r25271859<(Float, Int)>) {
202202
}
203203
}
204204

205+
// LValue to rvalue conversions.
206+
207+
func takesRValue(_: (Int, (Int, Int))) {}
208+
func takesAny(_: Any) {}
209+
210+
var x = 0
211+
var y = 0
212+
213+
let _ = (x, (y, 0))
214+
takesRValue((x, (y, 0)))
215+
takesAny((x, (y, 0)))

0 commit comments

Comments
 (0)