Skip to content

Commit 66c2429

Browse files
authored
[Typechecker] Fix a few regressions with @autoclosure (#28677)
* [Typechecker] Introduce a new request to check the structure of @autoclosure * Revert "[Typechecker] Introduce a new request to check the structure of @autoclosure" This reverts commit 3b1d4308bd3444230bfc7d031f7ccfa3b366a038. * [Typechecker] Fix a few regressions related to use of @autoclosure
1 parent 8b45b5a commit 66c2429

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,33 +2241,9 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
22412241
rep = FunctionType::Representation::Swift;
22422242
} else {
22432243
rep = *parsedRep;
2244-
2245-
if (attrs.has(TAK_autoclosure)) {
2246-
// @convention(c) and @convention(block) are not allowed with an @autoclosure type.
2247-
if (rep == FunctionType::Representation::CFunctionPointer ||
2248-
rep == FunctionType::Representation::Block) {
2249-
diagnose(attrs.getLoc(TAK_convention),
2250-
diag::invalid_autoclosure_and_convention_attributes,
2251-
attrs.getConventionName());
2252-
attrs.clearAttribute(TAK_convention);
2253-
}
2254-
}
22552244
}
22562245
}
22572246

2258-
// @autoclosure is only valid on parameters.
2259-
if (!isParam && attrs.has(TAK_autoclosure)) {
2260-
bool isVariadicFunctionParam =
2261-
options.is(TypeResolverContext::VariadicFunctionInput) &&
2262-
!options.hasBase(TypeResolverContext::EnumElementDecl);
2263-
2264-
diagnose(attrs.getLoc(TAK_autoclosure),
2265-
isVariadicFunctionParam ? diag::attr_not_on_variadic_parameters
2266-
: diag::attr_only_on_parameters,
2267-
"@autoclosure");
2268-
attrs.clearAttribute(TAK_autoclosure);
2269-
}
2270-
22712247
if (attrs.has(TAK_differentiable) &&
22722248
!Context.LangOpts.EnableExperimentalDifferentiableProgramming) {
22732249
diagnose(attrs.getLoc(TAK_differentiable),
@@ -2287,6 +2263,28 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
22872263
}
22882264
}
22892265

2266+
// Validate use of @autoclosure
2267+
if (attrs.has(TAK_autoclosure)) {
2268+
if (attrs.hasConvention()) {
2269+
if (attrs.getConventionName() == "c" ||
2270+
attrs.getConventionName() == "block") {
2271+
diagnose(attrs.getLoc(TAK_convention),
2272+
diag::invalid_autoclosure_and_convention_attributes,
2273+
attrs.getConventionName());
2274+
attrs.clearAttribute(TAK_convention);
2275+
}
2276+
} else if (options.is(TypeResolverContext::VariadicFunctionInput) &&
2277+
!options.hasBase(TypeResolverContext::EnumElementDecl)) {
2278+
diagnose(attrs.getLoc(TAK_autoclosure),
2279+
diag::attr_not_on_variadic_parameters, "@autoclosure");
2280+
attrs.clearAttribute(TAK_autoclosure);
2281+
} else if (!options.is(TypeResolverContext::FunctionInput)) {
2282+
diagnose(attrs.getLoc(TAK_autoclosure), diag::attr_only_on_parameters,
2283+
"@autoclosure");
2284+
attrs.clearAttribute(TAK_autoclosure);
2285+
}
2286+
}
2287+
22902288
auto instanceOptions = options;
22912289
instanceOptions.setContext(None);
22922290

test/attr/attr_autoclosure.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,14 @@ func test_autoclosure_with_generic_argument_mismatch() {
281281

282282
foo(S<String>()) // expected-error {{cannot convert value of type 'S<String>' to expected argument type 'S<Int>'}}
283283
}
284+
285+
// SR-11934
286+
func sr_11934(_ x: @autoclosure String...) {} // expected-error {{'@autoclosure' must not be used on variadic parameters}}
287+
288+
// SR-11938
289+
let sr_11938_1: Array<@autoclosure String> = [] // expected-error {{'@autoclosure' may only be used on parameters}}
290+
func sr_11938_2() -> @autoclosure String { "" } // expected-error {{'@autoclosure' may only be used on parameters}}
291+
func sr_11938_3(_ x: [@autoclosure String]) {} // expected-error {{'@autoclosure' may only be used on parameters}}
292+
293+
protocol SR_11938_P {}
294+
struct SR_11938_S : @autoclosure SR_11938_P {} // expected-error {{'@autoclosure' may only be used on parameters}}

0 commit comments

Comments
 (0)