Skip to content

Commit 2434781

Browse files
committed
[Diag] Reword the tuple shuffle diagnostic
"reorder" seems a bit less jargony than "shuffle", and include the labels that are being reordered.
1 parent 6510eb0 commit 2434781

File tree

9 files changed

+42
-21
lines changed

9 files changed

+42
-21
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7833,9 +7833,9 @@ ERROR(result_builder_buildpartialblock_accumulated_not_accessible,none,
78337833
// MARK: Tuple Shuffle Diagnostics
78347834
//------------------------------------------------------------------------------
78357835

7836-
WARNING(warn_reordering_tuple_shuffle_deprecated,Deprecation,
7837-
"expression shuffles the elements of this tuple; "
7838-
"this behavior is deprecated", ())
7836+
WARNING(warn_reordering_tuple_shuffle_deprecated,Deprecation,
7837+
"implicit reordering of tuple elements from '%0' to '%1' is deprecated",
7838+
(StringRef, StringRef))
78397839

78407840
//------------------------------------------------------------------------------
78417841
// MARK: Implicit conversion diagnostics

lib/Sema/CSApply.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5818,22 +5818,23 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr,
58185818

58195819
// Convert each OpaqueValueExpr to the correct type.
58205820
SmallVector<Expr *, 4> converted;
5821+
SmallVector<Identifier, 4> origLabels;
58215822
SmallVector<Identifier, 4> labels;
58225823
SmallVector<TupleTypeElt, 4> convertedElts;
58235824

58245825
bool anythingShuffled = false;
58255826
for (unsigned i = 0, e = sources.size(); i != e; ++i) {
58265827
unsigned source = sources[i];
58275828
auto *fromElt = destructured[source];
5829+
auto fromLabel = fromTuple->getElement(i).getName();
58285830

58295831
// Actually convert the source element.
58305832
auto toEltType = toTuple->getElementType(i);
58315833
auto toLabel = toTuple->getElement(i).getName();
58325834

58335835
// If we're shuffling positions and labels, we have to warn about this
58345836
// conversion.
5835-
if (i != sources[i] &&
5836-
fromTuple->getElement(i).getName() != toLabel)
5837+
if (i != sources[i] && fromLabel != toLabel)
58375838
anythingShuffled = true;
58385839

58395840
auto *toElt
@@ -5845,15 +5846,30 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr,
58455846

58465847
converted.push_back(toElt);
58475848
labels.push_back(toLabel);
5849+
origLabels.push_back(fromLabel);
58485850
convertedElts.emplace_back(toEltType, toLabel);
58495851
}
58505852

58515853
// Shuffling tuple elements is an anti-pattern worthy of a diagnostic. We
58525854
// will form the shuffle for now, but a future compiler should decline to
58535855
// do so and begin the process of removing them altogether.
58545856
if (anythingShuffled) {
5855-
ctx.Diags.diagnose(
5856-
expr->getLoc(), diag::warn_reordering_tuple_shuffle_deprecated);
5857+
auto concatLabels = [](SmallVectorImpl<Identifier> &labels,
5858+
SmallVectorImpl<char> &out) {
5859+
llvm::raw_svector_ostream OS(out);
5860+
for (auto label : labels) {
5861+
DeclName(label).print(OS, /*skipEmpty*/ false, /*escapeIfNeeded*/ true);
5862+
OS << ':';
5863+
}
5864+
};
5865+
SmallString<16> fromLabelStr;
5866+
concatLabels(origLabels, fromLabelStr);
5867+
SmallString<16> toLabelStr;
5868+
concatLabels(labels, toLabelStr);
5869+
5870+
ctx.Diags.diagnose(expr->getLoc(),
5871+
diag::warn_reordering_tuple_shuffle_deprecated,
5872+
fromLabelStr, toLabelStr);
58575873
}
58585874

58595875
// Create the result tuple, written in terms of the destructured

test/Constraints/tuple_shuffle.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,32 @@ func consume<T>(_ x: T) {} // Suppress unused variable warnings
55
func shuffle_through_initialization() {
66
let a = (x: 1, y: 2)
77
let b: (y: Int, x: Int)
8-
b = a // expected-warning {{expression shuffles the elements of this tuple}}
8+
b = a // expected-warning {{implicit reordering of tuple elements from 'x:y:' to 'y:x:' is deprecated}}
99
consume(b)
1010
}
1111

12+
func shuffle_raw_label(_ t: (`a b`: Int, `c d`: Int)) {
13+
let _: (`c d`: Int, `a b`: Int) = t
14+
// expected-warning@-1 {{implicit reordering of tuple elements from '`a b`:`c d`:' to '`c d`:`a b`:' is deprecated}}
15+
}
16+
1217
func shuffle_through_destructuring() {
1318
let a = (x: 1, y: 2)
14-
let (y: b, x: c) = a // expected-warning {{expression shuffles the elements of this tuple}}
19+
let (y: b, x: c) = a // expected-warning {{implicit reordering of tuple elements from 'x:y:' to 'y:x:' is deprecated}}
1520
consume((b, c))
1621
}
1722

1823
func shuffle_through_call() {
1924
func foo(_ : (x: Int, y: Int)) {}
20-
foo((y: 5, x: 10)) // expected-warning {{expression shuffles the elements of this tuple}}
25+
foo((y: 5, x: 10)) // expected-warning {{implicit reordering of tuple elements from 'y:x:' to 'x:y:' is deprecated}}
2126
}
2227

2328
func shuffle_through_cast() {
24-
let x = ((a: Int(), b: Int()) as (b: Int, a: Int)).0 // expected-warning {{expression shuffles the elements of this tuple}}
29+
let x = ((a: Int(), b: Int()) as (b: Int, a: Int)).0 // expected-warning {{implicit reordering of tuple elements from 'a:b:' to 'b:a:' is deprecated}}
2530

2631
// Ah, the famous double-shuffle
2732
let (c1, (c2, c3)): (c: Int, (b: Int, a: Int)) = ((a: Int(), b: Int()), c: Int())
28-
// expected-warning@-1 {{expression shuffles the elements of this tuple}}
29-
// expected-warning@-2 {{expression shuffles the elements of this tuple}}
33+
// expected-warning@-1 {{implicit reordering of tuple elements from 'a:b:' to 'b:a:' is deprecated}}
34+
// expected-warning@-2 {{implicit reordering of tuple elements from '_:c:' to 'c:_:' is deprecated}}
3035
consume((x, c1, c2, c3))
3136
}

test/Parse/omit_return.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func ff_implicitInjectIntoOptionalExpr(_ int: Int) -> Int? {
474474
}
475475

476476
func ff_implicitTupleShuffle(_ input: (one: Int, two: Int)) -> (two: Int, one: Int) {
477-
input // expected-warning {{expression shuffles the elements of this tuple; this behavior is deprecated}}
477+
input // expected-warning {{implicit reordering of tuple elements from 'one:two:' to 'two:one:' is deprecated}}
478478
}
479479

480480
func ff_implicitCollectionUpcast(_ derived: [Derived]) -> [Base] {

test/Parse/omit_return_ifdecl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ func ff_implicitInjectIntoOptionalExpr(_ int: Int) -> Int? {
676676

677677
func ff_implicitTupleShuffle(_ input: (one: Int, two: Int)) -> (two: Int, one: Int) {
678678
#if true
679-
input // expected-warning {{expression shuffles the elements of this tuple; this behavior is deprecated}}
679+
input // expected-warning {{implicit reordering of tuple elements from 'one:two:' to 'two:one:' is deprecated}}
680680
#endif
681681
}
682682

test/Sema/diag_unowned_immediate_deallocation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ func testGenericWeakClassDiag() {
452452
// The diagnostic doesn't currently support tuple shuffles.
453453
func testDontDiagnoseThroughTupleShuffles() {
454454
unowned let (c1, (c2, c3)): (c: C, (b: C, a: C)) = ((a: D(), b: C()), c: D())
455-
// expected-warning@-1 {{expression shuffles the elements of this tuple; this behavior is deprecated}}
456-
// expected-warning@-2 {{expression shuffles the elements of this tuple; this behavior is deprecated}}
455+
// expected-warning@-1 {{implicit reordering of tuple elements from 'a:b:' to 'b:a:' is deprecated}}
456+
// expected-warning@-2 {{implicit reordering of tuple elements from '_:c:' to 'c:_:' is deprecated}}
457457
unowned let c4 = ((a: C(), b: C()) as (b: C, a: C)).0
458-
// expected-warning@-1 {{expression shuffles the elements of this tuple; this behavior is deprecated}}
458+
// expected-warning@-1 {{implicit reordering of tuple elements from 'a:b:' to 'b:a:' is deprecated}}
459459

460460
_ = c1; _ = c2; _ = c3; _ = c4
461461
}

test/decl/func/operator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var f2 : (Int) -> Int = (+-+)
155155
var f3 : (inout Int) -> Int = (-+-) // expected-error{{ambiguous use of operator '-+-'}}
156156
var f4 : (inout Int, Int) -> Int = (+-+=)
157157
var r5 : (a : (Int, Int) -> Int, b : (Int, Int) -> Int) = (+, -)
158-
var r6 : (a : (Int, Int) -> Int, b : (Int, Int) -> Int) = (b : +, a : -) // expected-warning {{expression shuffles the elements of this tuple; this behavior is deprecated}}
158+
var r6 : (a : (Int, Int) -> Int, b : (Int, Int) -> Int) = (b : +, a : -) // expected-warning {{implicit reordering of tuple elements from 'b:a:' to 'a:b:' is deprecated}}
159159

160160
struct f6_S {
161161
subscript(op : (Int, Int) -> Int) -> Int {

test/expr/closure/closures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func funcdecl5(_ a: Int, _ y: Int) {
5656
var b = a.1+a.f
5757

5858
// Tuple expressions with named elements.
59-
var i : (y : Int, x : Int) = (x : 42, y : 11) // expected-warning {{expression shuffles the elements of this tuple; this behavior is deprecated}}
59+
var i : (y : Int, x : Int) = (x : 42, y : 11) // expected-warning {{implicit reordering of tuple elements from 'x:y:' to 'y:x:' is deprecated}}
6060
funcdecl1(123, 444)
6161

6262
// Calls.

test/expr/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func test5() {
194194

195195

196196
let c: (a: Int, b: Int) = (1,2)
197-
let _: (b: Int, a: Int) = c // expected-warning {{expression shuffles the elements of this tuple; this behavior is deprecated}}
197+
let _: (b: Int, a: Int) = c // expected-warning {{implicit reordering of tuple elements from 'a:b:' to 'b:a:' is deprecated}}
198198
}
199199

200200

0 commit comments

Comments
 (0)