Skip to content

[5.3][AST] Mark repr invalid only if @autoclosure parameter doesn't poin… #33162

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
merged 1 commit into from
Jul 29, 2020
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
14 changes: 11 additions & 3 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2411,15 +2411,23 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
}
}

if (hasFunctionAttr && !fnRepr) {
if (attrs.has(TAK_autoclosure)) {
if (attrs.has(TAK_autoclosure)) {
// If this is a situation where function type is wrapped
// into a number of parens, let's try to look through them,
// because parens are insignificant here e.g.:
//
// let _: (@autoclosure (() -> Void)) -> Void = { _ in }
if (!ty->is<FunctionType>()) {
// @autoclosure is going to be diagnosed when type of
// the parameter is validated, because that attribute
// applies to the declaration now.
repr->setInvalid();
attrs.clearAttribute(TAK_autoclosure);
}

attrs.clearAttribute(TAK_autoclosure);
}

if (hasFunctionAttr && !fnRepr) {
const auto diagnoseInvalidAttr = [&](TypeAttrKind kind) {
if (kind == TAK_escaping) {
Type optionalObjectType = ty->getOptionalObjectType();
Expand Down
33 changes: 33 additions & 0 deletions test/attr/attr_autoclosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,36 @@ struct SR_11938_S : @autoclosure SR_11938_P {} // expected-error {{'@autoclosure

// SR-9178
func bar<T>(_ x: @autoclosure T) {} // expected-error 1{{@autoclosure attribute only applies to function types}}

func test_autoclosure_type_in_parens() {
let _: (@autoclosure (() -> Void)) -> Void = { _ in } // Ok

struct Test {
func bugSingle<T: RawRepresentable>(defaultValue: @autoclosure (() -> T)) -> T { // Ok
defaultValue()
}

func bugMultiple<T: RawRepresentable>(defaultValue: @autoclosure ((() -> T))) -> T { // Ok
defaultValue()
}
}

enum E : String {
case foo = "foo"
case bar = "bar"
}

_ = Test().bugSingle(defaultValue: E.foo) // Ok
_ = Test().bugMultiple(defaultValue: E.bar) // Ok
}

func test_autoclosure_with_typealias() {
typealias ConcreteFunc = () -> Int
typealias GenericFunc<T> = () -> T

func test(cr: @autoclosure ConcreteFunc) -> Int { cr() } // Ok
func test<Q>(gn: @autoclosure GenericFunc<Q>) -> Q { gn() } // Ok

_ = test(cr: 0) // Ok
_ = test(gn: 1) // Ok
}