Skip to content

[TypeChecker] Drop @autoclosure attribute from invalid parameters #22186

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
Jan 29, 2019
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
4 changes: 4 additions & 0 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ mapParsedParameters(Parser &parser,
// belongs to both type flags and declaration.
if (auto *ATR = dyn_cast<AttributedTypeRepr>(type)) {
auto &attrs = ATR->getAttrs();
// At this point we actually don't know if that's valid to mark
// this parameter declaration as `autoclosure` because type has
// not been resolved yet - it should either be a function type
// or typealias with underlying function type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, does this fix rdar://problem/47550733 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that must be something else because it's related to @escaping attribute.

param->setAutoClosure(attrs.has(TypeAttrKind::TAK_autoclosure));
}
} else if (paramContext != Parser::ParameterContextKind::Closure) {
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,13 @@ static bool validateParameterType(ParamDecl *decl, TypeResolution resolution,
}
}

// If this parameter declaration is marked as `@autoclosure`
// let's make sure that its parameter type is indeed a function,
// this decision couldn't be made based on type representative
// alone because it may be later resolved into an invalid type.
if (decl->isAutoClosure())
hadError |= !(Ty && Ty->is<FunctionType>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pull the diagnostic and the hadError = true; closer together? It feels strange to have them separated like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently diagnostic is produced by type resolver but I can definitely move it to validateParameterType, not sure how much work that would be though.

Maybe it would make sense to revisit diagnostics separately from this PR?


if (hadError)
TL.setInvalidType(TC.Context);

Expand Down
14 changes: 10 additions & 4 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2293,10 +2293,6 @@ bool TypeResolver::resolveASTFunctionTypeParams(
variadic = true;
}

bool autoclosure = false;
if (auto *ATR = dyn_cast<AttributedTypeRepr>(eltTypeRepr))
autoclosure = ATR->getAttrs().has(TAK_autoclosure);

Type ty = resolveType(eltTypeRepr, thisElementOptions);
if (!ty) return true;

Expand All @@ -2310,6 +2306,16 @@ bool TypeResolver::resolveASTFunctionTypeParams(
ty = ty->mapTypeOutOfContext();
}

bool autoclosure = false;
if (auto *ATR = dyn_cast<AttributedTypeRepr>(eltTypeRepr)) {
// Make sure that parameter itself is of a function type, otherwise
// the problem would already be diagnosed by `resolveAttributedType`
// but attributes would stay unchanged. So as a recovery let's drop
// 'autoclosure' attribute from the resolved parameter.
autoclosure =
ty->is<FunctionType>() && ATR->getAttrs().has(TAK_autoclosure);
}

ValueOwnership ownership;

auto *nestedRepr = eltTypeRepr;
Expand Down
13 changes: 13 additions & 0 deletions test/attr/attr_autoclosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,16 @@ class Bar {
typealias BarClosure = (String) -> String
func barFunction(closure: @autoclosure BarClosure) {} // expected-error {{argument type of @autoclosure parameter must be '()'}}
}

func rdar_47586626() {
struct S {}
typealias F = () -> S

func foo(_: @autoclosure S) {} // expected-error {{@autoclosure attribute only applies to function types}}
func bar(_: @autoclosure F) {} // Ok

let s = S()

foo(s) // ok
bar(s) // ok
}