Skip to content

Commit c3ac890

Browse files
committed
Sema: Fix findDeclContextForType() for types nested inside closures
Fixes <rdar://problem/31803589>.
1 parent 2fe8632 commit c3ac890

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -259,33 +259,22 @@ findDeclContextForType(TypeChecker &TC,
259259

260260
auto ownerDC = typeDecl->getDeclContext();
261261

262-
// If the type is declared at the top level, there's nothing we can learn from
263-
// walking our parent contexts.
264-
if (ownerDC->isModuleScopeContext())
262+
// If the type is not nested in another type, there's nothing we can
263+
// learn from walking parent contexts.
264+
if (!ownerDC->isTypeContext() ||
265+
isa<GenericTypeParamDecl>(typeDecl))
265266
return std::make_tuple(Type(), true);
266267

267-
// Workaround for issue where generic typealias generic parameters are
268-
// looked up with the wrong 'fromDC'.
269-
if (isa<TypeAliasDecl>(ownerDC)) {
270-
assert(isa<GenericTypeParamDecl>(typeDecl));
271-
return std::make_tuple(Type(), true);
272-
}
273-
274-
bool needsBaseType = (ownerDC->isTypeContext() &&
275-
!isa<GenericTypeParamDecl>(typeDecl));
276268
NominalTypeDecl *ownerNominal =
277269
ownerDC->getAsNominalTypeOrNominalTypeExtensionContext();
278270

279271
// We might have an invalid extension that didn't resolve.
280272
//
281273
// FIXME: How did UnqualifiedLookup find the decl then?
282-
if (needsBaseType && ownerNominal == nullptr)
274+
if (ownerNominal == nullptr)
283275
return std::make_tuple(Type(), false);
284276

285277
auto getSelfType = [&](DeclContext *DC) -> Type {
286-
if (!needsBaseType)
287-
return Type();
288-
289278
// When looking up a nominal type declaration inside of a
290279
// protocol extension, always use the nominal type and
291280
// not the protocol 'Self' type.
@@ -332,13 +321,6 @@ findDeclContextForType(TypeChecker &TC,
332321
// We're going to check the next parent context.
333322
}
334323

335-
// If we didn't find the member in an immediate parent context and
336-
// there is no base type, something went wrong.
337-
if (!needsBaseType) {
338-
assert(false && "Should have found non-type context by now");
339-
return std::make_tuple(Type(), false);
340-
}
341-
342324
// Now, search the supertypes or refined protocols of each parent
343325
// context.
344326
for (auto parentDC = fromDC; !parentDC->isModuleContext();

test/decl/nested/type_in_function.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
// Generic class locally defined in non-generic function (rdar://problem/20116710)
44
func f3() {
55
class B<T> {}
6+
7+
class C : B<Int> {}
8+
9+
_ = B<Int>()
10+
_ = C()
11+
}
12+
13+
// Type defined inside a closure (rdar://problem/31803589)
14+
func hasAClosure() {
15+
_ = {
16+
enum E<T> { case a(T) }
17+
18+
let _ = E.a("hi")
19+
let _ = E<String>.a("hi")
20+
let _: E = .a("hi")
21+
let _: E<String> = .a("hi")
22+
}
623
}
724

825
protocol Racoon {

0 commit comments

Comments
 (0)