Skip to content

Commit 4863c62

Browse files
committed
describe more cases of noncopyable in a tuple; also improve existing message a bit.
1 parent 43c1857 commit 4863c62

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5448,7 +5448,9 @@ ERROR(tuple_pack_element_label,none,
54485448
"cannot use label with pack expansion tuple element",
54495449
())
54505450
ERROR(tuple_move_only_not_supported,none,
5451-
"tuples with noncopyable elements are not supported", ())
5451+
"tuple with noncopyable element type %0 is not supported", (Type))
5452+
ERROR(tuple_containing_move_only_not_supported,none,
5453+
"type %0 containing noncopyable element is not supported", (Type))
54525454
ERROR(vararg_not_allowed,none,
54535455
"variadic parameter cannot appear outside of a function parameter list",
54545456
())

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6111,6 +6111,13 @@ bool NotCopyableFailure::diagnoseAsError() {
61116111

61126112
case NoncopyableMatchFailure::CopyableConstraint: {
61136113
auto *loc = getLocator();
6114+
6115+
if (loc->isLastElement<LocatorPathElt::AnyTupleElement>()) {
6116+
assert(!noncopyableTy->is<TupleType>() && "will use poor wording");
6117+
emitDiagnostic(diag::tuple_move_only_not_supported, noncopyableTy);
6118+
return true;
6119+
}
6120+
61146121
// a bit paranoid of nulls and such...
61156122
if (auto *genericParam = loc->getGenericParameter()) {
61166123
if (auto *paramDecl = genericParam->getDecl()) {

lib/Sema/MiscDiagnostics.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
329329
// Diagnose attempts to form a tuple with any noncopyable elements.
330330
if (E->getType()->isPureMoveOnly()
331331
&& !Ctx.LangOpts.hasFeature(Feature::MoveOnlyTuples)) {
332-
Ctx.Diags.diagnose(E->getLoc(), diag::tuple_move_only_not_supported);
332+
auto noncopyableTy = E->getType();
333+
assert(noncopyableTy->is<TupleType>() && "will use poor wording");
334+
Ctx.Diags.diagnose(E->getLoc(),
335+
diag::tuple_containing_move_only_not_supported,
336+
noncopyableTy);
333337
}
334338
}
335339

lib/Sema/TypeCheckType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4786,8 +4786,10 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,
47864786
if (moveOnlyElementIndex.has_value()
47874787
&& !options.contains(TypeResolutionFlags::SILType)
47884788
&& !ctx.LangOpts.hasFeature(Feature::MoveOnlyTuples)) {
4789-
diagnose(repr->getElementType(*moveOnlyElementIndex)->getLoc(),
4790-
diag::tuple_move_only_not_supported);
4789+
auto noncopyableTy = elements[*moveOnlyElementIndex].getType();
4790+
auto loc = repr->getElementType(*moveOnlyElementIndex)->getLoc();
4791+
assert(!noncopyableTy->is<TupleType>() && "will use poor wording");
4792+
diagnose(loc, diag::tuple_move_only_not_supported, noncopyableTy);
47914793
}
47924794

47934795
return TupleType::get(elements, ctx);

test/Constraints/moveonly_constraints.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ func testBasic(_ mo: borrowing MO) {
8888
genericVarArg(5)
8989
genericVarArg(mo) // expected-error {{noncopyable type 'MO' cannot be substituted for copyable generic parameter 'T' in 'genericVarArg'}}
9090

91-
takeGeneric( (mo, 5) ) // expected-error {{noncopyable type 'MO' cannot be used with generics yet}}
92-
takeGenericSendable((mo, mo)) // expected-error 2{{noncopyable type 'MO' cannot be used with generics yet}}
91+
takeGeneric( (mo, 5) ) // expected-error {{tuple with noncopyable element type 'MO' is not supported}}
92+
takeGeneric( ((mo, 5), 19) ) // expected-error {{tuple with noncopyable element type 'MO' is not supported}}
93+
takeGenericSendable((mo, mo)) // expected-error 2{{tuple with noncopyable element type 'MO' is not supported}}
9394

9495
let singleton : (MO) = (mo)
9596
takeGeneric(singleton) // expected-error {{noncopyable type 'MO' cannot be substituted for copyable generic parameter 'T' in 'takeGeneric'}}
@@ -235,7 +236,7 @@ func checkStdlibTypes(_ mo: borrowing MO) {
235236
let _: [MO] = // expected-error {{noncopyable type 'MO' cannot be used with generic type 'Array<Element>' yet}}
236237
[]
237238
let _: [String: MO] = // expected-error {{noncopyable type 'MO' cannot be used with generic type 'Dictionary<Key, Value>' yet}}
238-
["hello" : MO()] // expected-error{{tuples with noncopyable elements are not supported}}
239+
["hello" : MO()] // expected-error{{type '(String, MO)' containing noncopyable element is not supported}}
239240

240241
_ = [MO()] // expected-error {{noncopyable type 'MO' cannot be substituted for copyable generic parameter 'Element' in 'Array'}}
241242

test/Constraints/moveonly_tuples.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
@_moveOnly
1111
struct Foo {
12-
var t: (Int, Butt) // expected-error{{tuples with noncopyable elements are not supported}}
12+
var t: (Int, Butt) // expected-error{{tuple with noncopyable element type 'Butt' is not supported}}
1313
}
1414
@_moveOnly
1515
struct Bar<T> {
16-
var t: (T, Butt) // expected-error{{tuples with noncopyable elements are not supported}}
17-
var u: (Int, (T, Butt)) // expected-error{{tuples with noncopyable elements are not supported}}
16+
var t: (T, Butt) // expected-error{{tuple with noncopyable element type 'Butt' is not supported}}
17+
var u: (Int, (T, Butt)) // expected-error{{tuple with noncopyable element type 'Butt' is not supported}}
1818
}
1919

2020
func inferredTuples<T>(x: Int, y: borrowing Butt, z: T) {
21-
let a = (x, y) // expected-error{{tuples with noncopyable elements are not supported}}
22-
let b = (y, z) // expected-error{{tuples with noncopyable elements are not supported}}
23-
let c = (x, y, z) // expected-error{{tuples with noncopyable elements are not supported}}
21+
let a = (x, y) // expected-error{{type '(Int, Butt)' containing noncopyable element is not supported}}
22+
let b = (y, z) // expected-error{{type '(Butt, T)' containing noncopyable element is not supported}}
23+
let c = (x, y, z) // expected-error{{type '(Int, Butt, T)' containing noncopyable element is not supported}}
2424
_ = a
2525
_ = b
2626
_ = c

0 commit comments

Comments
 (0)