Skip to content

Commit b8526ce

Browse files
author
ematejska
authored
Merge pull request #6899 from slavapestov/fix-tuple-conversion-mess-3.1
Sema: Follow-on fix for <#6267> [3.1]
2 parents 0295d1e + 70da983 commit b8526ce

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

lib/AST/Expr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,8 @@ TupleExpr *TupleExpr::create(ASTContext &ctx,
14111411
ArrayRef<SourceLoc> ElementNameLocs,
14121412
SourceLoc RParenLoc, bool HasTrailingClosure,
14131413
bool Implicit, Type Ty) {
1414+
assert(!Ty || isa<TupleType>(Ty.getPointer()));
1415+
14141416
size_t size =
14151417
totalSizeToAlloc<Expr *, Identifier, SourceLoc>(SubExprs.size(),
14161418
ElementNames.size(),

lib/SILGen/SILGenExpr.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,14 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
19241924
// If we're emitting into an initialization, we can try shuffling the
19251925
// elements of the initialization.
19261926
if (Initialization *I = C.getEmitInto()) {
1927-
if (I->canSplitIntoTupleElements()) {
1927+
// In Swift 3 mode, we might be stripping off labels from a
1928+
// one-element tuple; the destination type is a ParenType in
1929+
// that case.
1930+
//
1931+
// FIXME: Remove this eventually.
1932+
if (I->canSplitIntoTupleElements() &&
1933+
!(isa<ParenType>(E->getType().getPointer()) &&
1934+
SGF.getASTContext().isSwiftVersion3())) {
19281935
emitTupleShuffleExprInto(*this, E, I);
19291936
return RValue();
19301937
}
@@ -1940,7 +1947,25 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
19401947

19411948
// Prepare a new tuple to hold the shuffled result.
19421949
RValue result(E->getType()->getCanonicalType());
1943-
1950+
1951+
// In Swift 3 mode, we might be stripping off labels from a
1952+
// one-element tuple; the destination type is a ParenType in
1953+
// that case.
1954+
//
1955+
// FIXME: Remove this eventually.
1956+
if (isa<ParenType>(E->getType().getPointer()) &&
1957+
SGF.getASTContext().isSwiftVersion3()) {
1958+
assert(E->getElementMapping().size() == 1);
1959+
auto shuffleIndex = E->getElementMapping()[0];
1960+
assert(shuffleIndex != TupleShuffleExpr::DefaultInitialize &&
1961+
shuffleIndex != TupleShuffleExpr::CallerDefaultInitialize &&
1962+
shuffleIndex != TupleShuffleExpr::Variadic &&
1963+
"Only argument tuples can have default initializers & varargs");
1964+
1965+
result.addElement(std::move(elements[shuffleIndex]));
1966+
return result;
1967+
}
1968+
19441969
auto outerFields = E->getType()->castTo<TupleType>()->getElements();
19451970
auto shuffleIndexIterator = E->getElementMapping().begin();
19461971
auto shuffleIndexEnd = E->getElementMapping().end();

lib/Sema/CSApply.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,8 @@ namespace {
26832683
}
26842684

26852685
Type argType = TupleType::get(typeElements, tc.Context);
2686+
assert(isa<TupleType>(argType.getPointer()));
2687+
26862688
Expr *arg =
26872689
TupleExpr::create(tc.Context, SourceLoc(),
26882690
expr->getElements(),
@@ -2760,6 +2762,8 @@ namespace {
27602762
}
27612763

27622764
Type argType = TupleType::get(typeElements, tc.Context);
2765+
assert(isa<TupleType>(argType.getPointer()));
2766+
27632767
Expr *arg =
27642768
TupleExpr::create(tc.Context, expr->getLBracketLoc(),
27652769
expr->getElements(),
@@ -5064,9 +5068,9 @@ Expr *ExprRewriter::coerceCallArguments(
50645068
auto paramType = param.Ty;
50655069
if (argType->isEqual(paramType)) {
50665070
toSugarFields.push_back(
5067-
TupleTypeElt(argType, param.Label, param.parameterFlags));
5071+
TupleTypeElt(argType, getArgLabel(argIdx), param.parameterFlags));
50685072
fromTupleExprFields[argIdx] =
5069-
TupleTypeElt(paramType, param.Label, param.parameterFlags);
5073+
TupleTypeElt(paramType, getArgLabel(argIdx), param.parameterFlags);
50705074
fromTupleExpr[argIdx] = arg;
50715075
continue;
50725076
}
@@ -5123,6 +5127,8 @@ Expr *ExprRewriter::coerceCallArguments(
51235127

51245128
// If anything about the TupleExpr changed, rebuild a new one.
51255129
Type argTupleType = TupleType::get(fromTupleExprFields, tc.Context);
5130+
assert(isa<TupleType>(argTupleType.getPointer()));
5131+
51265132
if (anyChanged || !cs.getType(argTuple)->isEqual(argTupleType)) {
51275133
auto EltNames = argTuple->getElementNames();
51285134
auto EltNameLocs = argTuple->getElementNameLocs();

test/Compatibility/tuple_arguments.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,10 @@ do {
12741274
// with single 'Any' parameter
12751275
func takesAny(_: Any) {}
12761276

1277+
enum HasAnyCase {
1278+
case any(_: Any)
1279+
}
1280+
12771281
do {
12781282
let fn: (Any) -> () = { _ in }
12791283

@@ -1282,6 +1286,9 @@ do {
12821286

12831287
takesAny(123)
12841288
takesAny(data: 123)
1289+
1290+
_ = HasAnyCase.any(123)
1291+
_ = HasAnyCase.any(data: 123)
12851292
}
12861293

12871294
// rdar://problem/29739905 - protocol extension methods on Array had

test/Constraints/tuple_arguments.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,10 @@ do {
12591259
// with single 'Any' parameter
12601260
func takesAny(_: Any) {}
12611261

1262+
enum HasAnyCase {
1263+
case any(_: Any)
1264+
}
1265+
12621266
do {
12631267
let fn: (Any) -> () = { _ in }
12641268

@@ -1267,6 +1271,9 @@ do {
12671271

12681272
takesAny(123)
12691273
takesAny(data: 123) // expected-error {{extraneous argument label 'data:' in call}}
1274+
1275+
_ = HasAnyCase.any(123)
1276+
_ = HasAnyCase.any(data: 123) // expected-error {{extraneous argument label 'data:' in call}}
12701277
}
12711278

12721279
// rdar://problem/29739905 - protocol extension methods on Array had
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %target-swift-frontend -emit-silgen %s -swift-version 3 | %FileCheck %s
2+
3+
func fn(_: Any) {}
4+
5+
enum HasAnyCase {
6+
case any(_: Any)
7+
}
8+
9+
// CHECK-LABEL: sil hidden @_TF23argument_shuffle_swift31gFT1xP__T_ : $@convention(thin) (@in Any) -> () {
10+
func g(x: Any) {
11+
// CHECK: [[FN:%.*]] = function_ref @_TF23argument_shuffle_swift32fnFP_T_ : $@convention(thin) (@in Any) -> ()
12+
// CHECK: apply [[FN:%.*]]({{.*}}) : $@convention(thin) (@in Any) -> ()
13+
fn(data: 123)
14+
// CHECK: [[FN:%.*]] = function_ref @_TF23argument_shuffle_swift32fnFP_T_ : $@convention(thin) (@in Any) -> ()
15+
// CHECK: apply [[FN:%.*]]({{.*}}) : $@convention(thin) (@in Any) -> ()
16+
fn(data: x)
17+
18+
// CHECK: inject_enum_addr {{.*}} : $*HasAnyCase, #HasAnyCase.any!enumelt.1
19+
_ = HasAnyCase.any(123)
20+
21+
// CHECK: inject_enum_addr {{.*}} : $*HasAnyCase, #HasAnyCase.any!enumelt.1
22+
_ = HasAnyCase.any(data: 123)
23+
24+
// CHECK: return
25+
}

0 commit comments

Comments
 (0)