@@ -155,15 +155,24 @@ static Type getTypeFromSubstitutionSchema(Type schema,
155155 return schema.transformRec ([&](Type t) -> Optional<Type> {
156156 if (t->is <GenericTypeParamType>()) {
157157 auto index = getGenericParamIndex (t);
158+ auto substitution = substitutions[index];
158159
159- // Prepend the prefix of the lookup key to the substitution, skipping
160- // creation of a new MutableTerm in the case where the prefix is empty.
160+ // Prepend the prefix of the lookup key to the substitution.
161161 if (prefix.empty ()) {
162- return ctx.getTypeForTerm (substitutions[index], genericParams, protos);
163- } else {
164- MutableTerm substitution (prefix);
165- substitution.append (substitutions[index]);
162+ // Skip creation of a new MutableTerm in the case where the
163+ // prefix is empty.
166164 return ctx.getTypeForTerm (substitution, genericParams, protos);
165+ } else if (substitution.size () == 1 &&
166+ substitution[0 ].getKind () == Symbol::Kind::Protocol) {
167+ // If the prefix is non-empty and the substitution is the
168+ // protocol 'Self' type for some protocol, just use the prefix.
169+ return ctx.getTypeForTerm (prefix, genericParams, protos);
170+ } else {
171+ // Otherwise build a new term by appending the substitution
172+ // to the prefix.
173+ MutableTerm result (prefix);
174+ result.append (substitution);
175+ return ctx.getTypeForTerm (result, genericParams, protos);
167176 }
168177 }
169178
0 commit comments