Skip to content

SR-11027 [TypeChecker][CompilerCrash] Disallow convention c/block and autoclosure #27078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,11 @@ ERROR(rethrows_without_throwing_parameter,none,
ERROR(autoclosure_function_type,none,
"@autoclosure attribute only applies to function types",
())

ERROR(invalid_autoclosure_and_convention_attributes,none,
"'@convention(%0)' attribute is not allowed on '@autoclosure' types",
(StringRef))

ERROR(autoclosure_function_input_nonunit,none,
"argument type of @autoclosure parameter must be '()'", ())

Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,17 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
rep = FunctionType::Representation::Swift;
} else {
rep = *parsedRep;

if (attrs.has(TAK_autoclosure)) {
// @convention(c) and @convention(block) are not allowed with an @autoclosure type.
if (rep == FunctionType::Representation::CFunctionPointer ||
rep == FunctionType::Representation::Block) {
diagnose(attrs.getLoc(TAK_convention),
diag::invalid_autoclosure_and_convention_attributes,
attrs.getConvention());
attrs.clearAttribute(TAK_convention);
}
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/attr/attr_convention.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,28 @@ let f4: @convention(c) (Int) -> Int = { $0 }

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

// SR-11027

func sr11027(_ f: @convention(block) @autoclosure () -> Int) -> Void {} // expected-error {{'@convention(block)' attribute is not allowed on '@autoclosure' types}}
sr11027(1)

func sr11027_c(_ f: @convention(c) @autoclosure () -> Int) -> Void {} // expected-error{{'@convention(c)' attribute is not allowed on '@autoclosure' types}}
sr11027_c(1)

func sr11027_swift(_ f: @convention(swift) @autoclosure () -> Int) -> Void {} // OK
sr11027_swift(1)

func sr11027_thin(_ f: @convention(thin) @autoclosure () -> Int) -> Void {} // OK
sr11027_thin(1)

func sr11027_2(_ f: @autoclosure @convention(block) () -> Int) -> Void {} // expected-error {{'@convention(block)' attribute is not allowed on '@autoclosure' types}}
sr11027_2(1)

func sr11027_c_2(_ f: @autoclosure @convention(c) () -> Int) -> Void {} // expected-error {{'@convention(c)' attribute is not allowed on '@autoclosure' types}}
sr11027_c_2(1)

func sr11027_swift_2(_ f: @autoclosure @convention(swift) () -> Int) -> Void {} // OK
sr11027_swift_2(1)

func sr11027_thin_2(_ f: @autoclosure @convention(thin) () -> Int) -> Void {} // OK
sr11027_thin_2(1)
32 changes: 32 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr11027.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This source file is part of the Swift.org open source project
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not %target-swift-frontend %s -emit-silgen

func sr11027(_ f: @convention(block) @autoclosure () -> Int) -> Void {}
sr11027(1)

func sr11027_c(_ f: @convention(c) @autoclosure () -> Int) -> Void {}
sr11027_c(1)

func sr11027_swift(_ f: @convention(swift) @autoclosure () -> Int) -> Void {} // OK
sr11027_swift(1)

func sr11027_thin(_ f: @convention(thin) @autoclosure () -> Int) -> Void {} // OK
sr11027_thin(1)

func sr11027_2(_ f: @autoclosure @convention(block) () -> Int) -> Void {}
sr11027_2(1)

func sr11027_c_2(_ f: @autoclosure @convention(c) () -> Int) -> Void {}
sr11027_c_2(1)

func sr11027_swift_2(_ f: @autoclosure @convention(swift) () -> Int) -> Void {} // OK
sr11027_swift_2(1)

func sr11027_thin_2(_ f: @autoclosure @convention(thin) () -> Int) -> Void {} // OK
sr11027_thin_2(1)