Skip to content

Commit 3dc7a2d

Browse files
committed
Sema: Fix substMemberTypeWithBase() for non-generic typealias with 'where' clause
getContextSubstitutionMap() builds a substitution map for the generic signature of the parent context, which is wrong if the typealias has its own 'where' clause.
1 parent 1457f8d commit 3dc7a2d

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

lib/Sema/TypeCheckType.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4623,7 +4623,7 @@ Type TypeChecker::substMemberTypeWithBase(ModuleDecl *module,
46234623

46244624
auto *aliasDecl = dyn_cast<TypeAliasDecl>(member);
46254625
if (aliasDecl) {
4626-
if (aliasDecl->getGenericParams()) {
4626+
if (aliasDecl->isGeneric()) {
46274627
return UnboundGenericType::get(
46284628
aliasDecl, baseTy,
46294629
aliasDecl->getASTContext());
@@ -4647,7 +4647,7 @@ Type TypeChecker::substMemberTypeWithBase(ModuleDecl *module,
46474647
if (baseTy->is<ErrorType>())
46484648
return ErrorType::get(memberType);
46494649

4650-
subs = baseTy->getContextSubstitutionMap(module, member->getDeclContext());
4650+
subs = baseTy->getMemberSubstitutionMap(module, member);
46514651
resultType = memberType.subst(subs);
46524652
} else {
46534653
resultType = memberType;

test/Generics/where_clause_contextually_generic_decls.swift

+11
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ struct Container<T> {
127127
// expected-error@-1 {{invalid redeclaration of 'NestedAlias}}
128128
typealias NestedAlias2 = T.Magnitude where T: FixedWidthInteger
129129

130+
typealias NestedAlias3 = T.Element where T: Sequence
131+
130132
class NestedClass where T: Equatable {}
131133
}
132134

@@ -154,3 +156,12 @@ _ = Container<Bool>.NestedClass.self
154156
_ = Container<String>.NestedStruct.self
155157
_ = Container<Array<UInt8>>.NestedStruct2.self
156158
_ = Container<Array<Double>>.NestedStruct2.NestedEnum.self
159+
160+
// Make sure the substitution here actually succeeds instead of producing an ErrorType
161+
func sameType<T>(_: T.Type, _: T.Type) {}
162+
sameType(Container<Array<Int>>.NestedAlias3.self, Int.self)
163+
sameType(Container<Array<Bool>>.NestedAlias3.self, Int.self)
164+
// expected-error@-1 {{cannot convert value of type 'Int.Type' to expected argument type 'Container<Array<Bool>>.NestedAlias3.Type' (aka 'Bool.Type')}}
165+
166+
sameType(Container<Array<Int>>.NestedAlias3.self, Bool.self)
167+
// expected-error@-1 {{cannot convert value of type 'Bool.Type' to expected argument type 'Container<Array<Int>>.NestedAlias3.Type' (aka 'Int.Type')}}

validation-test/compiler_crashers_2_fixed/0161-issue-49119.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ protocol P {
88

99
struct Type<Param> {}
1010
extension Type: P where Param: P, Param.A == Type<Param> {
11-
// expected-error@-1 6{{extension of generic struct 'Type' has self-referential generic requirements}}
12-
// expected-note@-2 6{{through reference here}}
11+
// expected-error@-1 5{{extension of generic struct 'Type' has self-referential generic requirements}}
12+
// expected-note@-2 5{{through reference here}}
1313
// expected-error@-3 {{type 'Type<Param>' does not conform to protocol 'P'}}
1414
typealias A = Param
1515
// expected-note@-1 2{{through reference here}}

0 commit comments

Comments
 (0)