Skip to content

Commit 5d439bd

Browse files
committed
Merge pull request #1302 from JaSpa/assoctype
[SR-726][AST] Fix access of associated types through class-constrained generic params
2 parents 8d3ef1a + d0f20fa commit 5d439bd

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

lib/AST/Type.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,15 +2239,22 @@ static Type getMemberForBaseType(Module *module,
22392239
// If the parent is an archetype, extract the child archetype with the
22402240
// given name.
22412241
if (auto archetypeParent = substBase->getAs<ArchetypeType>()) {
2242-
if (!archetypeParent->hasNestedType(name)) {
2243-
const auto parent = archetypeParent->getParent();
2244-
if (!parent)
2245-
return ErrorType::get(module->getASTContext());
2246-
if (parent->isSelfDerived())
2247-
return parent->getNestedTypeValue(name);
2242+
if (archetypeParent->hasNestedType(name))
2243+
return archetypeParent->getNestedTypeValue(name);
2244+
2245+
if (auto parent = archetypeParent->getParent()) {
2246+
// If the archetype doesn't have the requested type and the parent is not
2247+
// self derived, error out
2248+
return parent->isSelfDerived() ? parent->getNestedTypeValue(name)
2249+
: ErrorType::get(module->getASTContext());
2250+
}
2251+
2252+
// If looking for an associated type and the archetype is constrained to a
2253+
// class, continue to the default associated type lookup
2254+
if (!assocType || !archetypeParent->getSuperclass()) {
2255+
// else just error out
2256+
return ErrorType::get(module->getASTContext());
22482257
}
2249-
2250-
return archetypeParent->getNestedTypeValue(name);
22512258
}
22522259

22532260
// If the parent is a type variable, retrieve its member type

test/Constraints/associated_types.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ protocol Runcible {
44
associatedtype Runcee
55
}
66

7-
class Mince {
8-
init() {}
7+
class Mince {
8+
init() {}
99
}
1010

1111
class Spoon : Runcible {
12-
init() {}
12+
init() {}
1313

1414
typealias Runcee = Mince
1515
}
1616

1717
class Owl<T:Runcible> {
18-
init() {}
18+
init() {}
1919

2020
func eat(what: T.Runcee, with: T) { }
2121
}
@@ -31,3 +31,9 @@ func owl2() -> Owl<Spoon> {
3131
func owl3() {
3232
Owl<Spoon>().eat(Mince(), with:Spoon())
3333
}
34+
35+
// "Can't access associated types through class-constrained generic parameters"
36+
// (https://bugs.swift.org/browse/SR-726)
37+
func spoon<S: Spoon>(s: S) {
38+
let _: S.Runcee? = nil
39+
}

0 commit comments

Comments
 (0)