Skip to content

Commit 2021b0c

Browse files
committed
AST: Clean up getMemberForBaseType() with an early return
1 parent 54918cb commit 2021b0c

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

lib/AST/Type.cpp

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4390,6 +4390,19 @@ static Type getMemberForBaseType(LookupConformanceFn lookupConformances,
43904390
if (auto *selfType = substBase->getAs<DynamicSelfType>())
43914391
substBase = selfType->getSelfType();
43924392

4393+
// If the parent is a type variable or a member rooted in a type variable,
4394+
// or if the parent is a type parameter, we're done. Also handle
4395+
// UnresolvedType here, which can come up in diagnostics.
4396+
if (substBase->isTypeVariableOrMember() ||
4397+
substBase->isTypeParameter() ||
4398+
substBase->is<UnresolvedType>())
4399+
return getDependentMemberType(substBase);
4400+
4401+
// All remaining cases require an associated type declaration and not just
4402+
// the name of a member type.
4403+
if (!assocType)
4404+
return failed();
4405+
43934406
// If the parent is an archetype, extract the child archetype with the
43944407
// given name.
43954408
if (auto archetypeParent = substBase->getAs<ArchetypeType>()) {
@@ -4402,61 +4415,46 @@ static Type getMemberForBaseType(LookupConformanceFn lookupConformances,
44024415
return failed();
44034416
}
44044417

4405-
// If the parent is a type variable or a member rooted in a type variable,
4406-
// or if the parent is a type parameter, we're done. Also handle
4407-
// UnresolvedType here, which can come up in diagnostics.
4408-
if (substBase->isTypeVariableOrMember() ||
4409-
substBase->isTypeParameter() ||
4410-
substBase->is<UnresolvedType>())
4411-
return getDependentMemberType(substBase);
4418+
auto proto = assocType->getProtocol();
4419+
ProtocolConformanceRef conformance =
4420+
lookupConformances(origBase->getCanonicalType(), substBase, proto);
44124421

4413-
// Retrieve the member type with the given name.
4414-
4415-
// If we know the associated type, look in the witness table.
4416-
if (assocType) {
4417-
auto proto = assocType->getProtocol();
4418-
ProtocolConformanceRef conformance =
4419-
lookupConformances(origBase->getCanonicalType(), substBase, proto);
4420-
4421-
if (conformance.isInvalid())
4422-
return failed();
4422+
if (conformance.isInvalid())
4423+
return failed();
44234424

4424-
Type witnessTy;
4425+
Type witnessTy;
44254426

4426-
// Retrieve the type witness.
4427-
if (conformance.isPack()) {
4428-
auto *packConformance = conformance.getPack();
4427+
// Retrieve the type witness.
4428+
if (conformance.isPack()) {
4429+
auto *packConformance = conformance.getPack();
44294430

4430-
witnessTy = packConformance->getAssociatedType(
4431-
assocType->getDeclaredInterfaceType());
4432-
} else if (conformance.isConcrete()) {
4433-
auto witness =
4434-
conformance.getConcrete()->getTypeWitnessAndDecl(assocType, options);
4431+
witnessTy = packConformance->getAssociatedType(
4432+
assocType->getDeclaredInterfaceType());
4433+
} else if (conformance.isConcrete()) {
4434+
auto witness =
4435+
conformance.getConcrete()->getTypeWitnessAndDecl(assocType, options);
44354436

4436-
witnessTy = witness.getWitnessType();
4437-
if (!witnessTy || witnessTy->hasError())
4438-
return failed();
4437+
witnessTy = witness.getWitnessType();
4438+
if (!witnessTy || witnessTy->hasError())
4439+
return failed();
44394440

4440-
// This is a hacky feature allowing code completion to migrate to
4441-
// using Type::subst() without changing output.
4442-
if (options & SubstFlags::DesugarMemberTypes) {
4443-
if (auto *aliasType = dyn_cast<TypeAliasType>(witnessTy.getPointer()))
4444-
witnessTy = aliasType->getSinglyDesugaredType();
4441+
// This is a hacky feature allowing code completion to migrate to
4442+
// using Type::subst() without changing output.
4443+
if (options & SubstFlags::DesugarMemberTypes) {
4444+
if (auto *aliasType = dyn_cast<TypeAliasType>(witnessTy.getPointer()))
4445+
witnessTy = aliasType->getSinglyDesugaredType();
44454446

4446-
// Another hack. If the type witness is a opaque result type. They can
4447-
// only be referred using the name of the associated type.
4448-
if (witnessTy->is<OpaqueTypeArchetypeType>())
4449-
witnessTy = witness.getWitnessDecl()->getDeclaredInterfaceType();
4450-
}
4447+
// Another hack. If the type witness is a opaque result type. They can
4448+
// only be referred using the name of the associated type.
4449+
if (witnessTy->is<OpaqueTypeArchetypeType>())
4450+
witnessTy = witness.getWitnessDecl()->getDeclaredInterfaceType();
44514451
}
4452-
4453-
if (!witnessTy || witnessTy->is<ErrorType>())
4454-
return failed();
4455-
4456-
return witnessTy;
44574452
}
44584453

4459-
return failed();
4454+
if (!witnessTy || witnessTy->is<ErrorType>())
4455+
return failed();
4456+
4457+
return witnessTy;
44604458
}
44614459

44624460
ProtocolConformanceRef LookUpConformanceInModule::

0 commit comments

Comments
 (0)