Skip to content

Commit 7dcb14f

Browse files
Suyash SrijanSuyash Srijan
authored andcommitted
[typechecker] allow @autoclosure closure to be a typealias
1 parent c86351d commit 7dcb14f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,27 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
19961996
hasFunctionAttr = true;
19971997
break;
19981998
}
1999-
1999+
2000+
// If we have an @autoclosure then try resolving the top level type repr
2001+
// first as it may be pointing to a typealias
2002+
if (attrs.has(TAK_autoclosure)) {
2003+
if (auto CITR = dyn_cast<ComponentIdentTypeRepr>(repr)) {
2004+
auto typeAliasResolver = TypeResolverContext::TypeAliasDecl;
2005+
if (auto type = resolveTopLevelIdentTypeComponent(resolution, CITR,
2006+
typeAliasResolver)) {
2007+
if (auto TAT = dyn_cast<TypeAliasType>(type.getPointer())) {
2008+
if (auto underlyingRepr = TAT->getDecl()
2009+
->getUnderlyingTypeLoc()
2010+
.getTypeRepr()) {
2011+
if (!underlyingRepr->isInvalid()) {
2012+
repr = underlyingRepr;
2013+
}
2014+
}
2015+
}
2016+
}
2017+
}
2018+
}
2019+
20002020
// Function attributes require a syntactic function type.
20012021
auto *fnRepr = dyn_cast<FunctionTypeRepr>(repr);
20022022

test/attr/attr_autoclosure.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,14 @@ func rdar_30906031(in arr: [Int], fn: @autoclosure () -> Int) -> Bool {
234234
arr.lazy.filter { $0 >= escapableF() }.isEmpty
235235
}
236236
}
237+
238+
// SR-2688
239+
class Foo {
240+
typealias FooClosure = () -> String
241+
func fooFunction(closure: @autoclosure FooClosure) {} // ok
242+
}
243+
244+
class Bar {
245+
typealias BarClosure = (String) -> String
246+
func barFunction(closure: @autoclosure BarClosure) {} // expected-error {{argument type of @autoclosure parameter must be '()'}}
247+
}

0 commit comments

Comments
 (0)