Skip to content

[6.0][sending] Do not allow for sending to be used together with borrowing. #74096

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
Jun 3, 2024
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
3 changes: 2 additions & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,8 @@ ERROR(sending_and_transferring_used_together,none,
WARNING(transferring_is_now_sendable,none,
"'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly",
())

ERROR(sending_cannot_be_used_with_borrowing,none,
"'%0' cannot be used together with 'borrowing'", (StringRef))

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
15 changes: 15 additions & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5510,6 +5510,14 @@ ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
.fixItRemove(TransferringLoc);
}

// If we already saw a specifier, check if we have borrowing. In such a
// case, emit an error.
if (SpecifierLoc.isValid() &&
Specifier == ParamDecl::Specifier::Borrowing) {
P.diagnose(Tok, diag::sending_cannot_be_used_with_borrowing,
"sending");
}

SendingLoc = P.consumeToken();
continue;
}
Expand Down Expand Up @@ -5548,6 +5556,13 @@ ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
P.diagnose(Tok, diag::sending_before_parameter_specifier,
getNameForParamSpecifier(Specifier));
}

// We cannot use transferring with borrowing.
if (TransferringLoc.isValid() &&
Specifier == ParamDecl::Specifier::Borrowing) {
P.diagnose(TransferringLoc, diag::sending_cannot_be_used_with_borrowing,
"transferring");
}
}
Tok.setKind(tok::contextual_keyword);
SpecifierLoc = P.consumeToken();
Expand Down
3 changes: 3 additions & 0 deletions test/Parse/sending.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ func testArgWithConsumingWrongOrder(_ x: sending consuming String, _ y: sending
func testArgWithConsumingWrongOrderType(_ x: (sending consuming String, sending inout String) -> ()) {}
// expected-error @-1 {{'sending' must be placed after specifier 'consuming'}}
// expected-error @-2 {{'sending' must be placed after specifier 'inout'}}

func testBorrowSending(_ x: borrowing sending String) {}
// expected-error @-1 {{'sending' cannot be used together with 'borrowing'}}
4 changes: 4 additions & 0 deletions test/Parse/transferring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ func testVarDeclTuple2(_ x: (transferring String)) {}
// expected-warning @-1 {{'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly}}
func testVarDeclTuple2(_ x: (transferring String, String)) {} // expected-error {{'transferring' cannot be applied to tuple elements}}
// expected-warning @-1 {{'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly}}

func testBorrowSending(_ x: transferring borrowing String) {}
// expected-warning @-1 {{'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly}}
// expected-error @-2 {{'transferring' cannot be used together with 'borrowing'}}
4 changes: 2 additions & 2 deletions test/SIL/Parser/sending.sil
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ sil_stage raw

// CHECK-LABEL: func transferValueDefault<T>(_ t: sending T)
func transferValueDefault<T>(_ t: sending T)
// CHECK-LABEL: func transferValueBorrowing<T>(_ t: borrowing sending T)
func transferValueBorrowing<T>(_ t: borrowing sending T)
// CHECK-LABEL: func transferValueBorrowing<T>(_ t: __shared sending T)
func transferValueBorrowing<T>(_ t: __shared sending T)
// CHECK-LABEL: func transferValueConsuming<T>(_ t: consuming sending T)
func transferValueConsuming<T>(_ t: consuming sending T)

Expand Down
4 changes: 2 additions & 2 deletions test/SIL/Parser/transferring.sil
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ sil_stage raw

// CHECK-LABEL: func transferValueDefault<T>(_ t: sending T)
func transferValueDefault<T>(_ t: transferring T)
// CHECK-LABEL: func transferValueBorrowing<T>(_ t: borrowing sending T)
func transferValueBorrowing<T>(_ t: transferring borrowing T)
// CHECK-LABEL: func transferValueBorrowing<T>(_ t: __shared sending T)
func transferValueBorrowing<T>(_ t: transferring __shared T)
// CHECK-LABEL: func transferValueConsuming<T>(_ t: consuming sending T)
func transferValueConsuming<T>(_ t: transferring consuming T)

Expand Down
8 changes: 4 additions & 4 deletions test/SIL/Serialization/sending.sil
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ sil_stage raw

// CHECK-LABEL: func transferValueDefault<T>(_ t: sending T)
func transferValueDefault<T>(_ t: transferring T)
// CHECK-LABEL: func transferValueBorrowing<T>(_ t: borrowing sending T)
func transferValueBorrowing<T>(_ t: transferring borrowing T)
// CHECK-LABEL: func transferValue__shared<T>(_ t: __shared sending T)
func transferValue__shared<T>(_ t: transferring __shared T)
// CHECK-LABEL: func transferValueConsuming<T>(_ t: consuming sending T)
func transferValueConsuming<T>(_ t: transferring consuming T)

// CHECK-LABEL: func sendingValueDefault<T>(_ t: sending T)
func sendingValueDefault<T>(_ t: sending T)
// CHECK-LABEL: func sendingValueBorrowing<T>(_ t: borrowing sending T)
func sendingValueBorrowing<T>(_ t: borrowing sending T)
// CHECK-LABEL: func sendingValue__shared<T>(_ t: __shared sending T)
func sendingValue__shared<T>(_ t: __shared sending T)
// CHECK-LABEL: func sendingValueConsuming<T>(_ t: consuming sending T)
func sendingValueConsuming<T>(_ t: consuming sending T)

Expand Down
6 changes: 2 additions & 4 deletions test/Sema/sending.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ func test_good(_ x: sending Int) {}

func test_consuming_after_sending(_ x: sending consuming Int) {} // expected-error {{'sending' must be placed after specifier 'consuming'}}

func test_borrowing_after_sending(_ x: sending borrowing Int) {} // expected-error {{'sending' must be placed after specifier 'borrowing'}}

func test_inout_after_sending(_ x: sending inout Int) {} // expected-error {{'sending' must be placed after specifier 'inout'}}

func test_repeated_sending(_ x: sending sending Int) {} // expected-error {{parameter may have at most one 'sending' specifier}}

func test_repeated_sending_mixed(_ x: sending borrowing sending inout Int) {}
// expected-error @-1 {{'sending' must be placed after specifier 'borrowing'}}
func test_repeated_sending_mixed(_ x: sending consuming sending inout Int) {}
// expected-error @-1 {{'sending' must be placed after specifier 'consuming'}}
// expected-error @-2 {{parameter may have at most one 'sending' specifier}}
// expected-error @-3 {{parameter may have at most one of the 'inout', 'borrowing', or 'consuming' specifiers}}

Expand Down
1 change: 1 addition & 0 deletions test/Sema/transferring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func test_repeated_transferring_mixed(_ x: transferring borrowing transferring i
// expected-error @-3 {{parameter may have at most one of the 'inout', 'borrowing', or 'consuming' specifiers}}
// expected-warning @-4 {{'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly}}
// expected-warning @-5 {{'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly}}
// expected-error @-6 {{'transferring' cannot be used together with 'borrowing'}}

// Just until we get the results setup.
func test_transferring_result_in_tuple() -> (transferring Int, Int) {}
Expand Down