Skip to content

Commit f8dace5

Browse files
committed
[Typechecker] Fix a crash related to use of invalid @autoclosure parameter
1 parent 66c2429 commit f8dace5

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,23 +2265,31 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
22652265

22662266
// Validate use of @autoclosure
22672267
if (attrs.has(TAK_autoclosure)) {
2268+
bool didDiagnose = false;
22682269
if (attrs.hasConvention()) {
22692270
if (attrs.getConventionName() == "c" ||
22702271
attrs.getConventionName() == "block") {
22712272
diagnose(attrs.getLoc(TAK_convention),
22722273
diag::invalid_autoclosure_and_convention_attributes,
22732274
attrs.getConventionName());
22742275
attrs.clearAttribute(TAK_convention);
2276+
didDiagnose = true;
22752277
}
22762278
} else if (options.is(TypeResolverContext::VariadicFunctionInput) &&
22772279
!options.hasBase(TypeResolverContext::EnumElementDecl)) {
22782280
diagnose(attrs.getLoc(TAK_autoclosure),
22792281
diag::attr_not_on_variadic_parameters, "@autoclosure");
22802282
attrs.clearAttribute(TAK_autoclosure);
2283+
didDiagnose = true;
22812284
} else if (!options.is(TypeResolverContext::FunctionInput)) {
22822285
diagnose(attrs.getLoc(TAK_autoclosure), diag::attr_only_on_parameters,
22832286
"@autoclosure");
22842287
attrs.clearAttribute(TAK_autoclosure);
2288+
didDiagnose = true;
2289+
}
2290+
2291+
if (didDiagnose) {
2292+
ty = ErrorType::get(Context);
22852293
}
22862294
}
22872295

test/attr/attr_autoclosure.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-typecheck-verify-swift -swift-version 5
22

33
// Simple case.
4-
var fn : @autoclosure () -> Int = 4 // expected-error {{'@autoclosure' may only be used on parameters}} expected-error {{cannot convert value of type 'Int' to specified type '() -> Int'}}
4+
var fn : @autoclosure () -> Int = 4 // expected-error {{'@autoclosure' may only be used on parameters}}
55

66
@autoclosure func func1() {} // expected-error {{attribute can only be applied to types, not declarations}}
77

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %target-swift-frontend %s -typecheck -verify
2+
3+
func sr_11939(_ closure: @autoclosure () -> String...) {} // expected-error {{'@autoclosure' must not be used on variadic parameters}}
4+
sr_11939("A") // No crash

0 commit comments

Comments
 (0)