Skip to content

Diagnose attempts to use tuples with noncopyable elements. #65173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5372,6 +5372,8 @@ ERROR(tuple_single_element,none,
ERROR(tuple_pack_element_label,none,
"cannot use label with pack expansion tuple element",
())
ERROR(tuple_move_only_not_supported,none,
"tuples with noncopyable elements are not supported", ())
ERROR(vararg_not_allowed,none,
"variadic parameter cannot appear outside of a function parameter list",
())
Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ EXPERIMENTAL_FEATURE(MoveOnlyClasses, true)
EXPERIMENTAL_FEATURE(NoImplicitCopy, true)
EXPERIMENTAL_FEATURE(OldOwnershipOperatorSpellings, true)
EXPERIMENTAL_FEATURE(MoveOnlyEnumDeinits, true)
EXPERIMENTAL_FEATURE(MoveOnlyTuples, true)

EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3271,6 +3271,10 @@ static bool usesFeatureMoveOnlyClasses(Decl *decl) {
return isa<ClassDecl>(decl) && usesFeatureMoveOnly(decl);
}

static bool usesFeatureMoveOnlyTuples(Decl *decl) {
return false;
}

static bool usesFeatureNoImplicitCopy(Decl *decl) {
return decl->isNoImplicitCopy();
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,16 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
}
}

// Diagnose tuple expressions with duplicate element label.
if (auto *tupleExpr = dyn_cast<TupleExpr>(E)) {
// Diagnose tuple expressions with duplicate element label.
diagnoseDuplicateLabels(tupleExpr->getLoc(),
tupleExpr->getElementNames());

// Diagnose attempts to form a tuple with any noncopyable elements.
if (E->getType()->isPureMoveOnly()
&& !Ctx.LangOpts.hasFeature(Feature::MoveOnlyTuples)) {
Ctx.Diags.diagnose(E->getLoc(), diag::tuple_move_only_not_supported);
}
}

// Specially diagnose some checked casts that are illegal.
Expand Down
19 changes: 18 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4668,12 +4668,22 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,

bool hadError = false;
bool foundDupLabel = false;
Optional<unsigned> moveOnlyElementIndex = None;
for (unsigned i = 0, end = repr->getNumElements(); i != end; ++i) {
auto *tyR = repr->getElementType(i);

auto ty = resolveType(tyR, elementOptions);
if (ty->hasError())
if (ty->hasError()) {
hadError = true;
}
// Tuples with move-only elements aren't yet supported.
// Track the presence of a noncopyable field for diagnostic purposes.
// We don't need to re-diagnose if a tuple contains another tuple, though,
// since we should've diagnosed the inner tuple already.
if (ty->isPureMoveOnly() && !moveOnlyElementIndex.has_value()
&& !isa<TupleTypeRepr>(tyR)) {
moveOnlyElementIndex = i;
}

auto eltName = repr->getElementName(i);

Expand Down Expand Up @@ -4722,6 +4732,13 @@ NeverNullType TypeResolver::resolveTupleType(TupleTypeRepr *repr,
!elements[0].getType()->is<PackExpansionType>())
return ParenType::get(ctx, elements[0].getType());
}

if (moveOnlyElementIndex.has_value()
&& !options.contains(TypeResolutionFlags::SILType)
&& !ctx.LangOpts.hasFeature(Feature::MoveOnlyTuples)) {
diagnose(repr->getElementType(*moveOnlyElementIndex)->getLoc(),
diag::tuple_move_only_not_supported);
}

return TupleType::get(elements, ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/moveonly_constraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func checkStdlibTypes(_ mo: __shared MO) {
let _: [MO] = // expected-error {{move-only type 'MO' cannot be used with generics yet}}
[]
let _: [String: MO] = // expected-error {{move-only type 'MO' cannot be used with generics yet}}
["hello" : MO()]
["hello" : MO()] // expected-error{{tuples with noncopyable elements are not supported}}

// i think this one's only caught b/c of the 'Any' change
_ = [MO()] // expected-error {{move-only type 'MO' cannot be used with generics yet}}
Expand Down
27 changes: 27 additions & 0 deletions test/Constraints/moveonly_tuples.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-swift-frontend -typecheck -verify %s

// Tuples with noncopyable elements are not yet supported. Make sure we reject
// them when code attempts to form such a type explicitly or by inference.

@_moveOnly struct Butt {
var x: Int
}

@_moveOnly
struct Foo {
var t: (Int, Butt) // expected-error{{tuples with noncopyable elements are not supported}}
}
@_moveOnly
struct Bar<T> {
var t: (T, Butt) // expected-error{{tuples with noncopyable elements are not supported}}
var u: (Int, (T, Butt)) // expected-error{{tuples with noncopyable elements are not supported}}
}

func inferredTuples<T>(x: Int, y: borrowing Butt, z: T) {
let a = (x, y) // expected-error{{tuples with noncopyable elements are not supported}}
let b = (y, z) // expected-error{{tuples with noncopyable elements are not supported}}
let c = (x, y, z) // expected-error{{tuples with noncopyable elements are not supported}}
_ = a
_ = b
_ = c
}
7 changes: 4 additions & 3 deletions test/Interpreter/moveonly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ Tests.test("global destroyed once") {
expectEqual(0, LifetimeTracked.instances)
}

// TODO (rdar://107494072): Move-only types with deinits declared inside
// functions sometimes lose their deinit function.
// When that's fixed, FD2 can be moved back inside the test closure below.
@_moveOnly
struct FD2 {
var field = 5
Expand Down Expand Up @@ -83,6 +80,10 @@ Tests.test("deinit not called in init when assigned") {
}
}

do {
let haver = FDHaver()
let _ = haver
}
do {
let haver = FDHaver2()
let _ = haver
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-sil -sil-verify-all -verify -enable-experimental-feature MoveOnlyClasses %s
// RUN: %target-swift-emit-sil -sil-verify-all -verify -enable-experimental-feature MoveOnlyClasses -enable-experimental-feature MoveOnlyTuples %s

// This test validates that we properly emit errors if we partially invalidate
// through a type with a deinit.
Expand Down