Skip to content

Commit

Permalink
Merge pull request swiftlang#6873 from slavapestov/fix-tuple-conversi…
Browse files Browse the repository at this point in the history
…on-mess

Sema: Follow-on fix for <swiftlang#6267>
  • Loading branch information
slavapestov authored Jan 18, 2017
2 parents 2b5eab4 + 81d46a3 commit 6a926d1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,8 @@ TupleExpr *TupleExpr::create(ASTContext &ctx,
ArrayRef<SourceLoc> ElementNameLocs,
SourceLoc RParenLoc, bool HasTrailingClosure,
bool Implicit, Type Ty) {
assert(!Ty || isa<TupleType>(Ty.getPointer()));

size_t size =
totalSizeToAlloc<Expr *, Identifier, SourceLoc>(SubExprs.size(),
ElementNames.size(),
Expand Down
17 changes: 16 additions & 1 deletion lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,22 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,

// Prepare a new tuple to hold the shuffled result.
RValue result(E->getType()->getCanonicalType());


// In Swift 3 mode, we might have to shuffle off labels.
//
// FIXME: Remove this eventually.
if (isa<ParenType>(E->getType().getPointer())) {
assert(E->getElementMapping().size() == 1);
auto shuffleIndex = E->getElementMapping()[0];
assert(shuffleIndex != TupleShuffleExpr::DefaultInitialize &&
shuffleIndex != TupleShuffleExpr::CallerDefaultInitialize &&
shuffleIndex != TupleShuffleExpr::Variadic &&
"Only argument tuples can have default initializers & varargs");

result.addElement(std::move(elements[shuffleIndex]));
return result;
}

auto outerFields = E->getType()->castTo<TupleType>()->getElements();
auto shuffleIndexIterator = E->getElementMapping().begin();
auto shuffleIndexEnd = E->getElementMapping().end();
Expand Down
10 changes: 8 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,8 @@ namespace {
}

Type argType = TupleType::get(typeElements, tc.Context);
assert(isa<TupleType>(argType.getPointer()));

Expr *arg =
TupleExpr::create(tc.Context, SourceLoc(),
expr->getElements(),
Expand Down Expand Up @@ -2760,6 +2762,8 @@ namespace {
}

Type argType = TupleType::get(typeElements, tc.Context);
assert(isa<TupleType>(argType.getPointer()));

Expr *arg =
TupleExpr::create(tc.Context, expr->getLBracketLoc(),
expr->getElements(),
Expand Down Expand Up @@ -5025,9 +5029,9 @@ Expr *ExprRewriter::coerceCallArguments(
auto paramType = param.Ty;
if (argType->isEqual(paramType)) {
toSugarFields.push_back(
TupleTypeElt(argType, param.Label, param.parameterFlags));
TupleTypeElt(argType, getArgLabel(argIdx), param.parameterFlags));
fromTupleExprFields[argIdx] =
TupleTypeElt(paramType, param.Label, param.parameterFlags);
TupleTypeElt(paramType, getArgLabel(argIdx), param.parameterFlags);
fromTupleExpr[argIdx] = arg;
continue;
}
Expand Down Expand Up @@ -5084,6 +5088,8 @@ Expr *ExprRewriter::coerceCallArguments(

// If anything about the TupleExpr changed, rebuild a new one.
Type argTupleType = TupleType::get(fromTupleExprFields, tc.Context);
assert(isa<TupleType>(argTupleType.getPointer()));

if (anyChanged || !cs.getType(argTuple)->isEqual(argTupleType)) {
auto EltNames = argTuple->getElementNames();
auto EltNameLocs = argTuple->getElementNameLocs();
Expand Down
14 changes: 14 additions & 0 deletions test/SILGen/argument_shuffle_swift3.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend -emit-silgen %s -swift-version 3 | %FileCheck %s

func fn(_: Any) {}

// CHECK-LABEL: sil hidden @_TF23argument_shuffle_swift31gFT1xP__T_ : $@convention(thin) (@in Any) -> () {
func g(x: Any) {
// CHECK: [[FN:%.*]] = function_ref @_TF23argument_shuffle_swift32fnFP_T_ : $@convention(thin) (@in Any) -> ()
// CHECK: apply [[FN:%.*]]({{.*}}) : $@convention(thin) (@in Any) -> ()
fn(data: 123)
// CHECK: [[FN:%.*]] = function_ref @_TF23argument_shuffle_swift32fnFP_T_ : $@convention(thin) (@in Any) -> ()
// CHECK: apply [[FN:%.*]]({{.*}}) : $@convention(thin) (@in Any) -> ()
fn(data: x)
// CHECK: return
}

0 comments on commit 6a926d1

Please sign in to comment.