Skip to content

Commit 94e9fd1

Browse files
committed
[Diagnostics] Use a generic "tuple type not convertible" error message
when there is no contextual type.
1 parent 50abedd commit 94e9fd1

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ ERROR(types_not_convertible_use_bool_value,none,
955955
ERROR(tuple_types_not_convertible_nelts,none,
956956
"%0 is not convertible to %1, "
957957
"tuples have a different number of elements", (Type, Type))
958+
ERROR(tuple_types_not_convertible,none,
959+
"tuple type %0 is not convertible to tuple type %1", (Type, Type))
958960

959961
ERROR(invalid_force_unwrap,none,
960962
"cannot force unwrap value of non-optional type %0", (Type))

lib/Sema/CSDiagnostics.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,14 +2804,19 @@ ContextualFailure::getDiagnosticFor(ContextualTypePurpose context,
28042804
}
28052805

28062806
bool TupleContextualFailure::diagnoseAsError() {
2807-
auto diagnostic = isNumElementsMismatch()
2808-
? diag::tuple_types_not_convertible_nelts
2809-
: getDiagnosticFor(getContextualTypePurpose(),
2810-
/*forProtocol=*/false);
2811-
if (!diagnostic)
2807+
Diag<Type, Type> diagnostic;
2808+
auto purpose = getContextualTypePurpose();
2809+
auto &cs = getConstraintSystem();
2810+
if (isNumElementsMismatch())
2811+
diagnostic = diag::tuple_types_not_convertible_nelts;
2812+
else if ((purpose == CTP_Initialization) && !cs.getContextualType())
2813+
diagnostic = diag::tuple_types_not_convertible;
2814+
else if (auto diag = getDiagnosticFor(purpose, /*forProtocol=*/false))
2815+
diagnostic = *diag;
2816+
else
28122817
return false;
28132818

2814-
emitDiagnostic(getAnchor()->getLoc(), *diagnostic, getFromType(), getToType());
2819+
emitDiagnostic(getAnchor()->getLoc(), diagnostic, getFromType(), getToType());
28152820
return true;
28162821
}
28172822

test/decl/var/variables.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func tuplePatternDestructuring(_ x : Int, y : Int) {
9696
_ = i+j
9797

9898
// <rdar://problem/20395243> QoI: type variable reconstruction failing for tuple types
99-
let (x: g1, a: h1) = (b: x, a: y) // expected-error {{cannot convert value of type '(b: Int, a: Int)' to specified type '(x: Int, a: Int)'}}
99+
let (x: g1, a: h1) = (b: x, a: y) // expected-error {{tuple type '(b: Int, a: Int)' is not convertible to tuple type '(x: Int, a: Int)'}}
100100
}
101101

102102
// <rdar://problem/21057425> Crash while compiling attached test-app.

0 commit comments

Comments
 (0)