Skip to content

Commit 1781fab

Browse files
committed
[CS] Upgrade tuple label mismatch warning to error for future lang mode
I missed upgrading this to an error for Swift 6 mode, let's upgrade it to an error for a future language mode. It's important we reject these cases since we're otherwise allowing subtyping to be a weaker constraint than conversion.
1 parent 1f93003 commit 1781fab

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,9 +1586,9 @@ WARNING(coercion_may_fail_warning,none,
15861586
"coercion from %0 to %1 may fail; use 'as?' or 'as!' instead",
15871587
(Type, Type))
15881588

1589-
WARNING(tuple_label_mismatch_warning,none,
1590-
"tuple conversion from %0 to %1 mismatches labels",
1591-
(Type, Type))
1589+
ERROR(tuple_label_mismatch,none,
1590+
"tuple conversion from %0 to %1 mismatches labels",
1591+
(Type, Type))
15921592

15931593
ERROR(missing_explicit_conversion,none,
15941594
"%0 is not implicitly convertible to %1; "

include/swift/Sema/CSFix.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3458,7 +3458,8 @@ class AllowInvalidStaticMemberRefOnProtocolMetatype final
34583458
}
34593459
};
34603460

3461-
/// Emit a warning for mismatched tuple labels.
3461+
/// Emit a warning for mismatched tuple labels, which is upgraded to an error
3462+
/// for a future language mode.
34623463
class AllowTupleLabelMismatch final : public ContextualMismatch {
34633464
AllowTupleLabelMismatch(ConstraintSystem &cs, Type fromType, Type toType,
34643465
ConstraintLocator *locator)

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9419,8 +9419,9 @@ bool InvalidWeakAttributeUse::diagnoseAsError() {
94199419
}
94209420

94219421
bool TupleLabelMismatchWarning::diagnoseAsError() {
9422-
emitDiagnostic(diag::tuple_label_mismatch_warning, getFromType(), getToType())
9423-
.highlight(getSourceRange());
9422+
emitDiagnostic(diag::tuple_label_mismatch, getFromType(), getToType())
9423+
.highlight(getSourceRange())
9424+
.warnUntilFutureSwiftVersion();
94249425
return true;
94259426
}
94269427

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3009,7 +3009,8 @@ class InvalidWeakAttributeUse final : public FailureDiagnostic {
30093009
bool diagnoseAsError() override;
30103010
};
30113011

3012-
/// Emit a warning for mismatched tuple labels.
3012+
/// Emit a warning for mismatched tuple labels, which is upgraded to an error
3013+
/// for a future language mode.
30133014
class TupleLabelMismatchWarning final : public ContextualFailure {
30143015
public:
30153016
TupleLabelMismatchWarning(const Solution &solution, Type fromType,

test/Constraints/tuple.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -342,27 +342,6 @@ optionalTuple = (bignum, 1) // expected-error {{cannot assign value of type '(In
342342
optionalTuple = optionalTuple2 // expected-error {{cannot assign value of type '(Int64, Int)?' to type '(Int, Int)?'}}
343343
// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('(Int64, Int)' and '(Int, Int)') are expected to be equal}}
344344

345-
func testTupleLabelMismatchFuncConversion(fn1: @escaping ((x: Int, y: Int)) -> Void,
346-
fn2: @escaping () -> (x: Int, Int)) {
347-
// Warn on mismatches
348-
let _: ((a: Int, b: Int)) -> Void = fn1 // expected-warning {{tuple conversion from '(a: Int, b: Int)' to '(x: Int, y: Int)' mismatches labels}}
349-
let _: ((x: Int, b: Int)) -> Void = fn1 // expected-warning {{tuple conversion from '(x: Int, b: Int)' to '(x: Int, y: Int)' mismatches labels}}
350-
351-
let _: () -> (y: Int, Int) = fn2 // expected-warning {{tuple conversion from '(x: Int, Int)' to '(y: Int, Int)' mismatches labels}}
352-
let _: () -> (y: Int, k: Int) = fn2 // expected-warning {{tuple conversion from '(x: Int, Int)' to '(y: Int, k: Int)' mismatches labels}}
353-
354-
// Attempting to shuffle has always been illegal here
355-
let _: () -> (y: Int, x: Int) = fn2 // expected-error {{cannot convert value of type '() -> (x: Int, Int)' to specified type '() -> (y: Int, x: Int)'}}
356-
357-
// Losing labels is okay though.
358-
let _: () -> (Int, Int) = fn2
359-
360-
// Gaining labels also okay.
361-
let _: ((x: Int, Int)) -> Void = fn1
362-
let _: () -> (x: Int, y: Int) = fn2
363-
let _: () -> (Int, y: Int) = fn2
364-
}
365-
366345
func testTupleLabelMismatchKeyPath() {
367346
// FIXME: The warning should be upgraded to an error for key paths.
368347
let _: KeyPath<(x: Int, y: Int), Int> = \(a: Int, b: Int).x
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %target-typecheck-verify-swift -language-mode 6 -verify-additional-prefix swift6-
2+
// RUN: %target-typecheck-verify-swift -language-mode 7 -verify-additional-prefix swift7-
3+
// REQUIRES: swift7
4+
5+
func testTupleLabelMismatchFuncConversion(fn1: @escaping ((x: Int, y: Int)) -> Void,
6+
fn2: @escaping () -> (x: Int, Int)) {
7+
// Warn on mismatches in Swift 6, upgrading to an error for Swift 7
8+
let _: ((a: Int, b: Int)) -> Void = fn1
9+
// expected-swift6-warning@-1 {{tuple conversion from '(a: Int, b: Int)' to '(x: Int, y: Int)' mismatches labels}}
10+
// expected-swift7-error@-2 {{tuple conversion from '(a: Int, b: Int)' to '(x: Int, y: Int)' mismatches labels}}
11+
let _: ((x: Int, b: Int)) -> Void = fn1
12+
// expected-swift6-warning@-1 {{tuple conversion from '(x: Int, b: Int)' to '(x: Int, y: Int)' mismatches labels}}
13+
// expected-swift7-error@-2 {{tuple conversion from '(x: Int, b: Int)' to '(x: Int, y: Int)' mismatches labels}}
14+
15+
let _: () -> (y: Int, Int) = fn2
16+
// expected-swift6-warning@-1 {{tuple conversion from '(x: Int, Int)' to '(y: Int, Int)' mismatches labels}}
17+
// expected-swift7-error@-2 {{tuple conversion from '(x: Int, Int)' to '(y: Int, Int)' mismatches labels}}
18+
let _: () -> (y: Int, k: Int) = fn2
19+
// expected-swift6-warning@-1 {{tuple conversion from '(x: Int, Int)' to '(y: Int, k: Int)' mismatches labels}}
20+
// expected-swift7-error@-2 {{tuple conversion from '(x: Int, Int)' to '(y: Int, k: Int)' mismatches labels}}
21+
22+
// Attempting to shuffle has always been illegal here
23+
let _: () -> (y: Int, x: Int) = fn2
24+
// expected-error@-1 {{cannot convert value of type '() -> (x: Int, Int)' to specified type '() -> (y: Int, x: Int)'}}
25+
26+
// Losing labels is okay though.
27+
let _: () -> (Int, Int) = fn2
28+
29+
// Gaining labels also okay.
30+
let _: ((x: Int, Int)) -> Void = fn1
31+
let _: () -> (x: Int, y: Int) = fn2
32+
let _: () -> (Int, y: Int) = fn2
33+
}

0 commit comments

Comments
 (0)