|
10 | 10 | // MARK: Declarations //
|
11 | 11 | ////////////////////////
|
12 | 12 |
|
13 |
| -class NonSendableKlass { // expected-complete-note 98{{}} |
| 13 | +class NonSendableKlass { |
14 | 14 | var field: NonSendableKlass? = nil
|
15 | 15 | }
|
16 | 16 |
|
@@ -821,3 +821,209 @@ actor ActorContainingSendableStruct {
|
821 | 821 | }
|
822 | 822 |
|
823 | 823 |
|
| 824 | +//////////////////// |
| 825 | +// MARK: Closures // |
| 826 | +//////////////////// |
| 827 | + |
| 828 | +func closureTests() async { |
| 829 | + func sendingClosure(_ x: sending () -> ()) { |
| 830 | + } |
| 831 | + |
| 832 | + func testLetOneNSVariableError() async { |
| 833 | + let x = NonSendableKlass() |
| 834 | + sendingClosure { _ = x } // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 835 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 836 | + sendingClosure { _ = x } // expected-note {{access can happen concurrently}} |
| 837 | + } |
| 838 | + |
| 839 | + func testLetNonIsolatedUnsafeNSVariableNoError() async { |
| 840 | + nonisolated(unsafe) let x = NonSendableKlass() |
| 841 | + sendingClosure { _ = x } |
| 842 | + sendingClosure { _ = x } |
| 843 | + } |
| 844 | + |
| 845 | + func testLetOneNSVariableSVariableError() async { |
| 846 | + let x = NonSendableKlass() |
| 847 | + let y = CustomActorInstance() |
| 848 | + sendingClosure { // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 849 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 850 | + _ = x |
| 851 | + _ = y |
| 852 | + } |
| 853 | + sendingClosure { // expected-note {{access can happen concurrently}} |
| 854 | + _ = x |
| 855 | + _ = y |
| 856 | + } |
| 857 | + } |
| 858 | + |
| 859 | + func testLetNonIsolatedUnsafeNSSVariableNoError() async { |
| 860 | + nonisolated(unsafe) let x = NonSendableKlass() |
| 861 | + let y = CustomActorInstance() |
| 862 | + sendingClosure { |
| 863 | + _ = x |
| 864 | + _ = y |
| 865 | + } |
| 866 | + sendingClosure { |
| 867 | + _ = x |
| 868 | + _ = y |
| 869 | + } |
| 870 | + } |
| 871 | + |
| 872 | + func testLetTwoNSVariableError() async { |
| 873 | + let x = NonSendableKlass() |
| 874 | + let y = NonSendableKlass() |
| 875 | + sendingClosure { // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 876 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 877 | + _ = x |
| 878 | + _ = y |
| 879 | + } |
| 880 | + sendingClosure { // expected-note {{access can happen concurrently}} |
| 881 | + _ = x |
| 882 | + _ = y |
| 883 | + } |
| 884 | + } |
| 885 | + |
| 886 | + func testLetTwoNSVariableError2() async { |
| 887 | + nonisolated(unsafe) let x = NonSendableKlass() |
| 888 | + let y = NonSendableKlass() |
| 889 | + sendingClosure { // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 890 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 891 | + _ = x |
| 892 | + _ = y |
| 893 | + } |
| 894 | + sendingClosure { // expected-note {{access can happen concurrently}} |
| 895 | + _ = x |
| 896 | + _ = y |
| 897 | + } |
| 898 | + } |
| 899 | + |
| 900 | + func testLetTwoNSVariableError3() async { |
| 901 | + nonisolated(unsafe) let x = NonSendableKlass() |
| 902 | + nonisolated(unsafe) let y = NonSendableKlass() |
| 903 | + sendingClosure { |
| 904 | + _ = x |
| 905 | + _ = y |
| 906 | + } |
| 907 | + sendingClosure { |
| 908 | + _ = x |
| 909 | + _ = y |
| 910 | + } |
| 911 | + } |
| 912 | + |
| 913 | + func testVarOneNSVariableError() async { |
| 914 | + var x = NonSendableKlass() |
| 915 | + x = NonSendableKlass() |
| 916 | + |
| 917 | + sendingClosure { _ = x } // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 918 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 919 | + sendingClosure { _ = x } // expected-note {{access can happen concurrently}} |
| 920 | + } |
| 921 | + |
| 922 | + func testVarNonIsolatedUnsafeNSVariableNoError() async { |
| 923 | + nonisolated(unsafe) var x = NonSendableKlass() |
| 924 | + x = NonSendableKlass() |
| 925 | + |
| 926 | + sendingClosure { _ = x } |
| 927 | + sendingClosure { _ = x } |
| 928 | + } |
| 929 | + |
| 930 | + func testVarOneNSVariableSVariableError() async { |
| 931 | + var x = NonSendableKlass() |
| 932 | + x = NonSendableKlass() |
| 933 | + var y = CustomActorInstance() |
| 934 | + y = CustomActorInstance() |
| 935 | + sendingClosure { // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 936 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 937 | + _ = x |
| 938 | + _ = y |
| 939 | + } |
| 940 | + sendingClosure { // expected-note {{access can happen concurrently}} |
| 941 | + _ = x |
| 942 | + _ = y |
| 943 | + } |
| 944 | + } |
| 945 | + |
| 946 | + func testVarNonIsolatedUnsafeNSSVariableNoError() async { |
| 947 | + nonisolated(unsafe) var x = NonSendableKlass() |
| 948 | + x = NonSendableKlass() |
| 949 | + var y = CustomActorInstance() |
| 950 | + y = CustomActorInstance() |
| 951 | + sendingClosure { |
| 952 | + _ = x |
| 953 | + _ = y |
| 954 | + } |
| 955 | + sendingClosure { |
| 956 | + _ = x |
| 957 | + _ = y |
| 958 | + } |
| 959 | + } |
| 960 | + |
| 961 | + func testVarTwoNSVariableError() async { |
| 962 | + var x = NonSendableKlass() |
| 963 | + x = NonSendableKlass() |
| 964 | + var y = NonSendableKlass() |
| 965 | + y = NonSendableKlass() |
| 966 | + sendingClosure { // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 967 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 968 | + _ = x |
| 969 | + _ = y |
| 970 | + } |
| 971 | + sendingClosure { // expected-note {{access can happen concurrently}} |
| 972 | + _ = x |
| 973 | + _ = y |
| 974 | + } |
| 975 | + } |
| 976 | + |
| 977 | + func testVarTwoNSVariableError2() async { |
| 978 | + nonisolated(unsafe) var x = NonSendableKlass() |
| 979 | + x = NonSendableKlass() |
| 980 | + var y = NonSendableKlass() |
| 981 | + y = NonSendableKlass() |
| 982 | + sendingClosure { // expected-warning {{sending value of non-Sendable type '() -> ()' risks causing data races}} |
| 983 | + // expected-note @-1 {{Passing value of non-Sendable type '() -> ()' as a 'sending' argument to local function 'sendingClosure' risks causing races in between local and caller code}} |
| 984 | + _ = x |
| 985 | + _ = y |
| 986 | + } |
| 987 | + sendingClosure { // expected-note {{access can happen concurrently}} |
| 988 | + _ = x |
| 989 | + _ = y |
| 990 | + } |
| 991 | + } |
| 992 | + |
| 993 | + func testVarTwoNSVariableError3() async { |
| 994 | + nonisolated(unsafe) var x = NonSendableKlass() |
| 995 | + x = NonSendableKlass() |
| 996 | + nonisolated(unsafe) var y = NonSendableKlass() |
| 997 | + y = NonSendableKlass() |
| 998 | + sendingClosure { |
| 999 | + _ = x |
| 1000 | + _ = y |
| 1001 | + } |
| 1002 | + sendingClosure { |
| 1003 | + _ = x |
| 1004 | + _ = y |
| 1005 | + } |
| 1006 | + } |
| 1007 | + |
| 1008 | + func testWithTaskDetached() async { |
| 1009 | + let x1 = NonSendableKlass() |
| 1010 | + Task.detached { _ = x1 } // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}} |
| 1011 | + // expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(priority:operation:)' risks causing races in between local and caller code}} |
| 1012 | + Task.detached { _ = x1 } // expected-note {{access can happen concurrently}} |
| 1013 | + |
| 1014 | + nonisolated(unsafe) let x2 = NonSendableKlass() |
| 1015 | + Task.detached { _ = x2 } |
| 1016 | + Task.detached { _ = x2 } |
| 1017 | + |
| 1018 | + nonisolated(unsafe) let x3a = NonSendableKlass() |
| 1019 | + nonisolated(unsafe) let x3b = NonSendableKlass() |
| 1020 | + Task.detached { _ = x3a; _ = x3b } |
| 1021 | + Task.detached { _ = x3a; _ = x3b } |
| 1022 | + |
| 1023 | + nonisolated(unsafe) let x4a = NonSendableKlass() |
| 1024 | + let x4b = NonSendableKlass() |
| 1025 | + Task.detached { _ = x4a; _ = x4b } // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}} |
| 1026 | + // expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(priority:operation:)' risks causing races in between local and caller code}} |
| 1027 | + Task.detached { _ = x4a; _ = x4b } // expected-note {{access can happen concurrently}} |
| 1028 | + } |
| 1029 | +} |
0 commit comments