Skip to content

Commit 937e584

Browse files
committed
[cxx-interop] Fix infinite recursion of PackExpansion type.
Fix infinite recursion issue in the SwiftTypeConverter. This patch is purely to prevent crashes.
1 parent f72afc8 commit 937e584

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -830,23 +830,32 @@ namespace {
830830
return { mappedType, underlyingResult.Hint };
831831
}
832832

833-
#define SUGAR_TYPE(KIND) \
834-
ImportResult Visit##KIND##Type(const clang::KIND##Type *type) { \
835-
return Visit(type->desugar()); \
836-
}
837-
SUGAR_TYPE(TypeOfExpr)
838-
SUGAR_TYPE(TypeOf)
839-
SUGAR_TYPE(Decltype)
840-
SUGAR_TYPE(UnaryTransform)
841-
SUGAR_TYPE(Elaborated)
842-
SUGAR_TYPE(SubstTemplateTypeParm)
843-
SUGAR_TYPE(TemplateSpecialization)
844-
SUGAR_TYPE(Auto)
845-
SUGAR_TYPE(DeducedTemplateSpecialization)
846-
SUGAR_TYPE(Adjusted)
847-
SUGAR_TYPE(PackExpansion)
848-
SUGAR_TYPE(Attributed)
833+
// TODO: add custom visitors for these types.
834+
#define MAYBE_SUGAR_TYPE(KIND) \
835+
ImportResult Visit##KIND##Type(const clang::KIND##Type *type) { \
836+
if (type->isSugared()) \
837+
return Visit(type->desugar()); \
838+
return Type(); \
839+
}
840+
MAYBE_SUGAR_TYPE(TypeOfExpr)
841+
MAYBE_SUGAR_TYPE(TypeOf)
842+
MAYBE_SUGAR_TYPE(Decltype)
843+
MAYBE_SUGAR_TYPE(UnaryTransform)
844+
MAYBE_SUGAR_TYPE(TemplateSpecialization)
845+
MAYBE_SUGAR_TYPE(Auto)
846+
MAYBE_SUGAR_TYPE(DeducedTemplateSpecialization)
847+
MAYBE_SUGAR_TYPE(PackExpansion)
848+
849+
// These types are ALWAYS sugared.
850+
#define SUGAR_TYPE(KIND) \
851+
ImportResult Visit##KIND##Type(const clang::KIND##Type *type) { \
852+
return Visit(type->desugar()); \
853+
}
849854
SUGAR_TYPE(MacroQualified)
855+
SUGAR_TYPE(Attributed)
856+
SUGAR_TYPE(Adjusted)
857+
SUGAR_TYPE(SubstTemplateTypeParm)
858+
SUGAR_TYPE(Elaborated)
850859

851860
ImportResult VisitDecayedType(const clang::DecayedType *type) {
852861
clang::ASTContext &clangCtx = Impl.getClangASTContext();

test/Interop/Cxx/templates/Inputs/function-templates.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,22 @@ template <class R, class T, class U> R returns_template(T a, U b) {
2020

2121
// Same here:
2222
template <class T> void cannot_infer_template() {}
23+
24+
// TODO: We should support these types. Until then, make sure we don't crash when importing.
25+
template<class... Ts>
26+
void testPackExpansion(Ts...) { }
27+
28+
template<class T>
29+
void testTypeOfExpr(T a, typeof(a + 1) b) { }
30+
31+
template<class T>
32+
void testTypeOf(T a, typeof a b) { }
33+
34+
template<class T>
35+
decltype(auto) testAuto(T arg) {
36+
return arg;
37+
}
38+
39+
// TODO: Add tests for Decltype, UnaryTransform, and TemplateSpecialization with a dependent type once those are supported.
40+
41+
// TODO: Add test for DeducedTemplateSpecializationType once we support class templates.

0 commit comments

Comments
 (0)