Skip to content

Commit 06576e5

Browse files
authored
Merge pull request #27078 from LucianoPAlmeida/SR11027-disallow-convention-c-autoclosure-parameters
SR-11027 [TypeChecker][CompilerCrash] Disallow convention c/block and autoclosure
2 parents 9748a5a + 940b0ba commit 06576e5

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,11 @@ ERROR(rethrows_without_throwing_parameter,none,
26802680
ERROR(autoclosure_function_type,none,
26812681
"@autoclosure attribute only applies to function types",
26822682
())
2683+
2684+
ERROR(invalid_autoclosure_and_convention_attributes,none,
2685+
"'@convention(%0)' attribute is not allowed on '@autoclosure' types",
2686+
(StringRef))
2687+
26832688
ERROR(autoclosure_function_input_nonunit,none,
26842689
"argument type of @autoclosure parameter must be '()'", ())
26852690

lib/Sema/TypeCheckType.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,17 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
22572257
rep = FunctionType::Representation::Swift;
22582258
} else {
22592259
rep = *parsedRep;
2260+
2261+
if (attrs.has(TAK_autoclosure)) {
2262+
// @convention(c) and @convention(block) are not allowed with an @autoclosure type.
2263+
if (rep == FunctionType::Representation::CFunctionPointer ||
2264+
rep == FunctionType::Representation::Block) {
2265+
diagnose(attrs.getLoc(TAK_convention),
2266+
diag::invalid_autoclosure_and_convention_attributes,
2267+
attrs.getConvention());
2268+
attrs.clearAttribute(TAK_convention);
2269+
}
2270+
}
22602271
}
22612272
}
22622273

test/attr/attr_convention.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,28 @@ let f4: @convention(c) (Int) -> Int = { $0 }
77

88
let f5: @convention(INTERCAL) (Int) -> Int = { $0 } // expected-error{{convention 'INTERCAL' not supported}}
99

10+
// SR-11027
11+
12+
func sr11027(_ f: @convention(block) @autoclosure () -> Int) -> Void {} // expected-error {{'@convention(block)' attribute is not allowed on '@autoclosure' types}}
13+
sr11027(1)
14+
15+
func sr11027_c(_ f: @convention(c) @autoclosure () -> Int) -> Void {} // expected-error{{'@convention(c)' attribute is not allowed on '@autoclosure' types}}
16+
sr11027_c(1)
17+
18+
func sr11027_swift(_ f: @convention(swift) @autoclosure () -> Int) -> Void {} // OK
19+
sr11027_swift(1)
20+
21+
func sr11027_thin(_ f: @convention(thin) @autoclosure () -> Int) -> Void {} // OK
22+
sr11027_thin(1)
23+
24+
func sr11027_2(_ f: @autoclosure @convention(block) () -> Int) -> Void {} // expected-error {{'@convention(block)' attribute is not allowed on '@autoclosure' types}}
25+
sr11027_2(1)
26+
27+
func sr11027_c_2(_ f: @autoclosure @convention(c) () -> Int) -> Void {} // expected-error {{'@convention(c)' attribute is not allowed on '@autoclosure' types}}
28+
sr11027_c_2(1)
29+
30+
func sr11027_swift_2(_ f: @autoclosure @convention(swift) () -> Int) -> Void {} // OK
31+
sr11027_swift_2(1)
32+
33+
func sr11027_thin_2(_ f: @autoclosure @convention(thin) () -> Int) -> Void {} // OK
34+
sr11027_thin_2(1)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This source file is part of the Swift.org open source project
2+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
3+
// Licensed under Apache License v2.0 with Runtime Library Exception
4+
//
5+
// See https://swift.org/LICENSE.txt for license information
6+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
7+
8+
// RUN: not %target-swift-frontend %s -emit-silgen
9+
10+
func sr11027(_ f: @convention(block) @autoclosure () -> Int) -> Void {}
11+
sr11027(1)
12+
13+
func sr11027_c(_ f: @convention(c) @autoclosure () -> Int) -> Void {}
14+
sr11027_c(1)
15+
16+
func sr11027_swift(_ f: @convention(swift) @autoclosure () -> Int) -> Void {} // OK
17+
sr11027_swift(1)
18+
19+
func sr11027_thin(_ f: @convention(thin) @autoclosure () -> Int) -> Void {} // OK
20+
sr11027_thin(1)
21+
22+
func sr11027_2(_ f: @autoclosure @convention(block) () -> Int) -> Void {}
23+
sr11027_2(1)
24+
25+
func sr11027_c_2(_ f: @autoclosure @convention(c) () -> Int) -> Void {}
26+
sr11027_c_2(1)
27+
28+
func sr11027_swift_2(_ f: @autoclosure @convention(swift) () -> Int) -> Void {} // OK
29+
sr11027_swift_2(1)
30+
31+
func sr11027_thin_2(_ f: @autoclosure @convention(thin) () -> Int) -> Void {} // OK
32+
sr11027_thin_2(1)

0 commit comments

Comments
 (0)