Skip to content

Commit d0798e2

Browse files
authored
Update TypeCheckConstraints.cpp
1 parent e8748a0 commit d0798e2

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -616,20 +616,6 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
616616
});
617617
};
618618

619-
auto containsTypesButDependant = [&](Type type) -> bool {
620-
DependentMemberType *cachedDP = nullptr;
621-
return type.findIf([&](Type type) -> bool {
622-
if (auto *GP = type->getAs<GenericTypeParamType>()) {
623-
if (cachedDP && !cachedDP->getBase()->isEqual(GP))
624-
return findParam(GP);
625-
return cachedDP == nullptr && findParam(GP);
626-
}
627-
if (auto *DP = type->getAs<DependentMemberType>())
628-
cachedDP = DP;
629-
return false;
630-
});
631-
};
632-
633619
// Anchor of this default expression i.e. function, subscript
634620
// or enum case.
635621
auto *anchor = cast<ValueDecl>(DC->getParent()->getAsDecl());
@@ -641,14 +627,42 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
641627

642628
if (anchor->hasCurriedSelf())
643629
anchorTy = anchorTy->getResult()->castTo<AnyFunctionType>();
630+
631+
auto containsTypesButDependent = [&](Type type) -> bool {
632+
633+
class Walker: public TypeWalker {
634+
llvm::function_ref<TypeVariableType *(GenericTypeParamType *)> findParamFn;
635+
public:
636+
explicit Walker(llvm::function_ref<TypeVariableType *(GenericTypeParamType *)> findParam) : findParamFn(findParam) {}
637+
638+
Action walkToTypePre(Type ty) override {
639+
if (auto *GP = ty->getAs<GenericTypeParamType>()) {
640+
if (findParamFn(GP)) {
641+
return Action::Stop;
642+
}
643+
}
644+
if (auto *DMT = ty->getAs<DependentMemberType>()) {
645+
if (auto *baseGP = DMT->getBase()->getAs<GenericTypeParamType>()) {
646+
if (findParamFn(baseGP)) {
647+
return Action::SkipNode;
648+
}
649+
}
650+
}
651+
return Action::Continue;
652+
}
653+
};
654+
655+
return type.walk(Walker(findParam));
656+
};
657+
644658
// Reject if generic parameters are used in multiple different positions
645659
// in the parameter list.
646660

647661
llvm::SmallVector<unsigned, 2> affectedParams;
648662
for (unsigned i : indices(anchorTy->getParams())) {
649663
const auto &param = anchorTy->getParams()[i];
650664

651-
if (containsTypesButDependant(param.getPlainType()))
665+
if (containsTypesButDependent(param.getPlainType()))
652666
affectedParams.push_back(i);
653667
}
654668

0 commit comments

Comments
 (0)