Skip to content

Commit 485bdc7

Browse files
committed
describe more cases of noncopyable in a tuple; also improve existing message a bit.
1 parent c40985d commit 485bdc7

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
@@ -5514,7 +5514,9 @@ ERROR(tuple_pack_element_label,none,
55145514
"cannot use label with pack expansion tuple element",
55155515
())
55165516
ERROR(tuple_move_only_not_supported,none,
5517-
"tuples with noncopyable elements are not supported", ())
5517+
"tuple with noncopyable element type %0 is not supported", (Type))
5518+
ERROR(tuple_containing_move_only_not_supported,none,
5519+
"type %0 containing noncopyable element is not supported", (Type))
55185520
ERROR(vararg_not_allowed,none,
55195521
"variadic parameter cannot appear outside of a function parameter list",
55205522
())

lib/Sema/CSDiagnostics.cpp

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

61576157
case NoncopyableMatchFailure::CopyableConstraint: {
61586158
auto *loc = getLocator();
6159+
6160+
if (loc->isLastElement<LocatorPathElt::AnyTupleElement>()) {
6161+
assert(!noncopyableTy->is<TupleType>() && "will use poor wording");
6162+
emitDiagnostic(diag::tuple_move_only_not_supported, noncopyableTy);
6163+
return true;
6164+
}
6165+
61596166
// a bit paranoid of nulls and such...
61606167
if (auto *genericParam = loc->getGenericParameter()) {
61616168
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
@@ -4794,8 +4794,10 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,
47944794
if (moveOnlyElementIndex.has_value()
47954795
&& !options.contains(TypeResolutionFlags::SILType)
47964796
&& !ctx.LangOpts.hasFeature(Feature::MoveOnlyTuples)) {
4797-
diagnose(repr->getElementType(*moveOnlyElementIndex)->getLoc(),
4798-
diag::tuple_move_only_not_supported);
4797+
auto noncopyableTy = elements[*moveOnlyElementIndex].getType();
4798+
auto loc = repr->getElementType(*moveOnlyElementIndex)->getLoc();
4799+
assert(!noncopyableTy->is<TupleType>() && "will use poor wording");
4800+
diagnose(loc, diag::tuple_move_only_not_supported, noncopyableTy);
47994801
}
48004802

48014803
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)