mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
SourceKit: Don't call subst() with generic parameters that don't exist in the SubstitutionMap
Hack around this instead by using the two-function form of subst(), and checking if the generic parameter is valid in the signature. This comes up because we're using the generic signature of the nominal type to get a SubstitutionMap, and then applying this map to the types in the generic requirements of a member. If the member introduces its own generic parameters, some of those requirements might not be valid types in the outer generic signature. This can probably use SubstitutionMap::combineSubstitutionMaps() instead, but it would require more refactoring than I'm willing to undertake for now.
This commit is contained in:
@@ -1588,17 +1588,34 @@ void PrintAST::printSingleDepthOfGenericSignature(
|
||||
bool swapSelfAndDependentMemberType =
|
||||
(flags & SwapSelfAndDependentMemberType);
|
||||
|
||||
unsigned typeContextDepth = 0;
|
||||
SubstitutionMap subMap;
|
||||
ModuleDecl *M = nullptr;
|
||||
if (CurrentType && Current) {
|
||||
if (!CurrentType->isExistentialType()) {
|
||||
auto *DC = Current->getInnermostDeclContext()->getInnermostTypeContext();
|
||||
auto *M = DC->getParentModule();
|
||||
M = DC->getParentModule();
|
||||
subMap = CurrentType->getContextSubstitutionMap(M, DC);
|
||||
if (!subMap.empty()) {
|
||||
typeContextDepth = subMap.getGenericSignature()
|
||||
.getGenericParams().back()->getDepth() + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto substParam = [&](Type param) -> Type {
|
||||
return param.subst(subMap);
|
||||
if (subMap.empty())
|
||||
return param;
|
||||
|
||||
return param.subst(
|
||||
[&](SubstitutableType *type) -> Type {
|
||||
if (cast<GenericTypeParamType>(type)->getDepth() < typeContextDepth)
|
||||
return Type(type).subst(subMap);
|
||||
return type;
|
||||
},
|
||||
[&](CanType depType, Type substType, ProtocolDecl *proto) {
|
||||
return M->lookupConformance(substType, proto);
|
||||
});
|
||||
};
|
||||
|
||||
if (printParams) {
|
||||
@@ -1608,10 +1625,7 @@ void PrintAST::printSingleDepthOfGenericSignature(
|
||||
genericParams,
|
||||
[&](GenericTypeParamType *param) {
|
||||
if (!subMap.empty()) {
|
||||
if (auto argTy = substParam(param))
|
||||
printType(argTy);
|
||||
else
|
||||
printType(param);
|
||||
printType(substParam(param));
|
||||
} else if (auto *GP = param->getDecl()) {
|
||||
Printer.callPrintStructurePre(PrintStructureKind::GenericParameter,
|
||||
GP);
|
||||
|
||||
@@ -269,7 +269,9 @@ static void initDocGenericParams(const Decl *D, DocEntityInfo &Info,
|
||||
// synthesized extention itself rather than a member, into its extended
|
||||
// nominal (the extension's own requirements shouldn't be considered in the
|
||||
// substitution).
|
||||
unsigned TypeContextDepth = 0;
|
||||
SubstitutionMap SubMap;
|
||||
ModuleDecl *M = nullptr;
|
||||
Type BaseType;
|
||||
if (SynthesizedTarget) {
|
||||
BaseType = SynthesizedTarget.getBaseNominal()->getDeclaredInterfaceType();
|
||||
@@ -279,13 +281,29 @@ static void initDocGenericParams(const Decl *D, DocEntityInfo &Info,
|
||||
DC = cast<ExtensionDecl>(D)->getExtendedNominal();
|
||||
else
|
||||
DC = D->getInnermostDeclContext()->getInnermostTypeContext();
|
||||
auto *M = DC->getParentModule();
|
||||
M = DC->getParentModule();
|
||||
SubMap = BaseType->getContextSubstitutionMap(M, DC);
|
||||
if (!SubMap.empty()) {
|
||||
TypeContextDepth = SubMap.getGenericSignature()
|
||||
.getGenericParams().back()->getDepth() + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto SubstTypes = [&](Type Ty) {
|
||||
return Ty.subst(SubMap, SubstFlags::DesugarMemberTypes);
|
||||
if (SubMap.empty())
|
||||
return Ty;
|
||||
|
||||
return Ty.subst(
|
||||
[&](SubstitutableType *type) -> Type {
|
||||
if (cast<GenericTypeParamType>(type)->getDepth() < TypeContextDepth)
|
||||
return Type(type).subst(SubMap);
|
||||
return type;
|
||||
},
|
||||
[&](CanType depType, Type substType, ProtocolDecl *proto) {
|
||||
return M->lookupConformance(substType, proto);
|
||||
},
|
||||
SubstFlags::DesugarMemberTypes);
|
||||
};
|
||||
|
||||
// FIXME: Not right for extensions of nested generic types
|
||||
|
||||
Reference in New Issue
Block a user