@@ -53,19 +53,46 @@ func bar(x: Int) -> String { return "test" }
5353@section ( " mysection " ) let invalidFuncRef1 = foo ( )
5454// expected-error@-1{{not supported in a constant expression}}
5555@section ( " mysection " ) let invalidFuncRef2 = Bool . self. random
56- // expected-error@-1{{closures not supported in a constant expression}}
56+ // expected-error@-1{{not supported in a constant expression}}
5757@section ( " mysection " ) let invalidFuncRef3 = ( Bool . self as Bool . Type) . random
5858// expected-error@-1{{not supported in a constant expression}}
5959
6060// generic function references (should be rejected)
6161@section ( " mysection " ) let invalidGenericFunc = [ Int ] . randomElement
6262// expected-error@-1{{not supported in a constant expression}}
6363
64- // closures (should be rejected)
65- @section ( " mysection " ) let invalidClosure1 = { }
66- // expected-error@-1{{closures not supported in a constant expression}}
67- @section ( " mysection " ) let invalidClosure2 = { return 42 }
68- // expected-error@-1{{closures not supported in a constant expression}}
64+ // closures
65+ @section ( " mysection " ) let closure1 = { } // ok
66+ @section ( " mysection " ) let closure2 = { return 42 } // ok
67+ @section ( " mysection " ) let closure3 = { ( x: Int ) in return x + 1 } // ok
68+ @section ( " mysection " ) let closure4 : ( ) -> Void = { } // ok
69+ @section ( " mysection " ) let closure5 : ( Int ) -> Int = { x in x * 2 } // ok
70+ @section ( " mysection " ) let closure6 : @convention ( c) ( Int ) -> Int = { x in x * 2 } // ok
71+ struct W {
72+ @section ( " mysection " ) static let closure7 : @convention ( c) ( Int ) -> Int = { x in x * 2 } // ok
73+ }
74+
75+ let capturedVar = 10
76+ class TestClass { }
77+ var capturedMutableVar = TestClass ( )
78+
79+ // closures with captures (should be rejected)
80+ @section ( " mysection " ) let invalidClosure1 = { capturedVar }
81+ // expected-error@-1{{closures with captures not supported in a constant expression}}
82+ @section ( " mysection " ) let invalidClosure2 = { return capturedVar + 1 }
83+ // expected-error@-1{{closures with captures not supported in a constant expression}}
84+ @section ( " mysection " ) let invalidClosure3 = { [ capturedVar] in return capturedVar }
85+ // expected-error@-1{{not supported in a constant expression}}
86+ @section ( " mysection " ) let invalidClosure4 = { [ weak capturedMutableVar] in return capturedMutableVar }
87+ // expected-error@-1{{not supported in a constant expression}}
88+ @section ( " mysection " ) let invalidClosure5 = { [ unowned capturedMutableVar] in return capturedMutableVar }
89+ // expected-error@-1{{not supported in a constant expression}}
90+ @section ( " mysection " ) let invalidClosure6 = { [ capturedVar, capturedMutableVar] in return 42 }
91+ // expected-error@-1{{not supported in a constant expression}}
92+ @section ( " mysection " ) let invalidClosure7 = { [ renamed = capturedVar] in return renamed * 2 }
93+ // expected-error@-1{{not supported in a constant expression}}
94+ @section ( " mysection " ) let invalidClosure8 = { [ computed = capturedVar + 5 ] in return computed }
95+ // expected-error@-1{{not supported in a constant expression}}
6996
7097struct S { }
7198enum E { case a }
0 commit comments