Skip to content

temporarily prevent Copyable types from using consuming and borrowing #65570

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 2 commits into from
May 4, 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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7119,6 +7119,11 @@ ERROR(moveonly_parameter_missing_ownership, none,
"noncopyable parameter must specify its ownership", ())
NOTE(moveonly_parameter_ownership_suggestion, none,
"add '%0' %1", (StringRef, StringRef))
ERROR(ownership_specifier_copyable,none,
"Copyable types cannot be 'consuming' or 'borrowing' yet", ())
ERROR(self_ownership_specifier_copyable,none,
"%0 is not yet valid on %1s in a Copyable type",
(SelfAccessKind, DescriptiveDeclKind))

//------------------------------------------------------------------------------
// MARK: Runtime discoverable attributes (@runtimeMetadata)
Expand Down
23 changes: 22 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void AttributeChecker::visitMutationAttr(DeclAttribute *attr) {
}

auto DC = FD->getDeclContext();
// mutation attributes may only appear in type context.
// self-ownership attributes may only appear in type context.
if (auto contextTy = DC->getDeclaredInterfaceType()) {
// 'mutating' and 'nonmutating' are not valid on types
// with reference semantics.
Expand All @@ -487,6 +487,27 @@ void AttributeChecker::visitMutationAttr(DeclAttribute *attr) {
break;
}
}

// Unless we have the experimental no-implicit-copy feature enabled, Copyable
// types can't use 'consuming' or 'borrowing' ownership specifiers.
if (!Ctx.LangOpts.hasFeature(Feature::NoImplicitCopy)) {
if (!contextTy->isPureMoveOnly()) {
switch (attrModifier) { // check the modifier for the Copyable type.
case SelfAccessKind::NonMutating:
case SelfAccessKind::Mutating:
case SelfAccessKind::LegacyConsuming:
// already checked
break;

case SelfAccessKind::Consuming:
case SelfAccessKind::Borrowing:
diagnoseAndRemoveAttr(attr, diag::self_ownership_specifier_copyable,
attrModifier, FD->getDescriptiveKind());
break;
}
}
}

} else {
diagnoseAndRemoveAttr(attr, diag::mutating_invalid_global_scope,
attrModifier);
Expand Down
28 changes: 27 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4280,7 +4280,33 @@ TypeResolver::resolveOwnershipTypeRepr(OwnershipTypeRepr *repr,
// Remember that we've seen an ownership specifier for this base type.
options |= TypeResolutionFlags::HasOwnership;

return resolveType(repr->getBase(), options);
auto result = resolveType(repr->getBase(), options);
if (result->hasError())
return result;

// Unless we have the experimental no-implicit-copy feature enabled, Copyable
// types can't use 'consuming' or 'borrowing' ownership specifiers.
if (!getASTContext().LangOpts.hasFeature(Feature::NoImplicitCopy)) {
if (!result->isPureMoveOnly()) {
// Prevent copyable types from using the non-underscored ownership parameter
// specifiers, other than 'inout'.
switch (ownershipRepr->getSpecifier()) {
case ParamSpecifier::Default:
case ParamSpecifier::InOut:
case ParamSpecifier::LegacyShared:
case ParamSpecifier::LegacyOwned:break;

case ParamSpecifier::Borrowing:
case ParamSpecifier::Consuming:
diagnoseInvalid(ownershipRepr,
ownershipRepr->getLoc(),
diag::ownership_specifier_copyable);
return ErrorType::get(getASTContext());
}
}
}

return result;
}

NeverNullType
Expand Down
56 changes: 49 additions & 7 deletions test/Parse/ownership_modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
struct borrowing {}
struct consuming {}

struct Foo {}
struct Foo: ~Copyable {}

func foo(x: borrowing Foo) {}
func bar(x: consuming Foo) {}
Expand All @@ -18,13 +18,13 @@ func worst(x: (borrowing consuming Foo) -> ()) {} // expected-error{{at most one

func zim(x: borrowing) {}
func zang(x: consuming) {}
func zung(x: borrowing consuming) {}
func zip(x: consuming borrowing) {}
func zung(x: borrowing consuming) {} // expected-error{{Copyable types cannot be 'consuming' or 'borrowing' yet}}
func zip(x: consuming borrowing) {} // expected-error{{Copyable types cannot be 'consuming' or 'borrowing' yet}}
func zap(x: (borrowing, consuming) -> ()) {}
func zoop(x: (borrowing consuming, consuming borrowing) -> ()) {}
func zoop(x: (borrowing consuming, consuming borrowing) -> ()) {} // expected-error 2{{Copyable types cannot be 'consuming' or 'borrowing' yet}}

func worster(x: borrowing borrowing borrowing) {} // expected-error{{at most one}}
func worstest(x: (borrowing borrowing borrowing) -> ()) {} // expected-error{{at most one}}
func worster(x: borrowing borrowing borrowing) {} // expected-error{{at most one}} // expected-error{{Copyable types cannot be 'consuming' or 'borrowing' yet}}
func worstest(x: (borrowing borrowing borrowing) -> ()) {} // expected-error{{at most one}} // expected-error{{Copyable types cannot be 'consuming' or 'borrowing' yet}}

// Parameter specifier names are regular identifiers in other positions,
// including argument labels.
Expand All @@ -47,7 +47,7 @@ func argumentLabel(anonConsumingInClosure: (_ consuming: Int) -> ()) {}
func argumentLabel(anonSharedInClosure: (_ __shared: Int) -> ()) {}
func argumentLabel(anonOwnedInClosure: (_ __owned: Int) -> ()) {}

struct MethodModifiers {
struct MethodModifiers: ~Copyable {
mutating func mutating() {}
borrowing func borrowing() {}
consuming func consuming() {}
Expand All @@ -59,3 +59,45 @@ struct MethodModifiers {
borrowing consuming func tooManyC() {} // expected-error{{method must not be declared both 'borrowing' and 'consuming'}}
borrowing mutating consuming func tooManyD() {} // expected-error 2 {{method must not be declared both }}
}


func chalk(_ a: consuming String, // expected-error{{Copyable types cannot be 'consuming' or 'borrowing' yet}}
_ b: borrowing [Int], // expected-error{{Copyable types cannot be 'consuming' or 'borrowing' yet}}
_ c: __shared [String],
_ d: __owned Int?)
{}

struct Stepping {
consuming func perform() {} // expected-error {{'consuming' is not yet valid on instance methods in a Copyable type}}
borrowing func doIt() {} // expected-error {{'borrowing' is not yet valid on instance methods in a Copyable type}}
mutating func change() {}
var ex: Int {
__consuming get { 0 }
}
}

class Clapping {
consuming func perform() {} // expected-error {{'consuming' is not yet valid on instance methods in a Copyable type}}
borrowing func doIt() {} // expected-error {{'borrowing' is not yet valid on instance methods in a Copyable type}}
var ex: Int {
__consuming get { 0 }
}
}

protocol Popping {
consuming func perform() // expected-error {{'consuming' is not yet valid on instance methods in a Copyable type}}
borrowing func doIt() // expected-error {{'borrowing' is not yet valid on instance methods in a Copyable type}}
mutating func change()
var ex: Int {
__consuming get
}
}

enum Exercising {
consuming func perform() {} // expected-error {{'consuming' is not yet valid on instance methods in a Copyable type}}
borrowing func doIt() {} // expected-error {{'borrowing' is not yet valid on instance methods in a Copyable type}}
mutating func change() {}
var ex: Int {
__consuming get { 0 }
}
}
2 changes: 1 addition & 1 deletion test/Parse/ownership_modifiers_no_errors.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NoImplicitCopy

// This is a variation of `ownership_modifiers.swift` with the expected error
// lines removed, so that the file is parsed by the SwiftSyntax parser
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/consuming_parameter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
// RUN: %target-swift-emit-silgen -enable-experimental-feature NoImplicitCopy %s | %FileCheck %s

func bar(_: String) {}

Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/moveonly.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
// RUN: %target-swift-emit-silgen -enable-experimental-feature NoImplicitCopy %s | %FileCheck %s

//////////////////
// Declarations //
Expand Down
4 changes: 2 additions & 2 deletions test/SILGen/moveonly_escaping_closure.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-swift-emit-silgen -module-name moveonly_closure %s | %FileCheck %s
// RUN: %target-swift-emit-sil -module-name moveonly_closure -verify %s
// RUN: %target-swift-emit-silgen -enable-experimental-feature NoImplicitCopy -module-name moveonly_closure %s | %FileCheck %s
// RUN: %target-swift-emit-sil -enable-experimental-feature NoImplicitCopy -module-name moveonly_closure -verify %s

@_moveOnly
struct Empty {}
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/ownership_specifier_mangling.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
// RUN: %target-swift-emit-silgen -enable-experimental-feature NoImplicitCopy %s | %FileCheck %s

// The internal `__shared` and `__owned` modifiers would always affect
// symbol mangling, even if they don't have a concrete impact on ABI. The
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/consuming_parameter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -c -disable-availability-checking -Xllvm --sil-print-final-ossa-module -O -module-name=main -o /dev/null %s 2>&1 | %FileCheck %s
// RUN: %target-swift-frontend -c -enable-experimental-feature NoImplicitCopy -disable-availability-checking -Xllvm --sil-print-final-ossa-module -O -module-name=main -o /dev/null %s 2>&1 | %FileCheck %s

// REQUIRES: concurrency

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 -enable-experimental-feature MoveOnlyTuples %s
// RUN: %target-swift-emit-sil -sil-verify-all -verify -enable-experimental-feature NoImplicitCopy -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
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 NoImplicitCopy -enable-experimental-feature MoveOnlyClasses %s

//////////////////
// Declarations //
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 NoImplicitCopy -enable-experimental-feature MoveOnlyClasses %s

//////////////////
// Declarations //
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 %s
// RUN: %target-swift-emit-sil -enable-experimental-feature NoImplicitCopy -sil-verify-all -verify %s

//////////////////
// Declarations //
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 %s
// RUN: %target-swift-emit-sil -enable-experimental-feature NoImplicitCopy -sil-verify-all -verify %s

//////////////////
// Declarations //
Expand Down
7 changes: 4 additions & 3 deletions test/attr/lexical.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ func foo() {
_ = s3
}

@_moveOnly struct MoveOnly {}
struct MoveOnly: ~Copyable {}

@_eagerMove @_moveOnly struct MoveOnlyEagerly {} // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}
@_eagerMove struct MoveOnlyEagerly: ~Copyable {} // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}

func zoo(@_eagerMove _ : consuming MoveOnly) {} // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}

func zooo(@_noEagerMove _ : consuming C) {} // ok, only way to spell this behavior
// TODO: Copyable types can't be consuming right now (rdar://108383660)
//func zooo(@_noEagerMove _ : consuming C) {} // ok, only way to spell this behavior

extension MoveOnly {
@_eagerMove // expected-error {{@_eagerMove cannot be applied to NonCopyable types}}
Expand Down