Skip to content

[cxx-interop] Avoid importing too complex specializations #59906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsClangImporter.def
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ WARNING(libstdcxx_not_found, none,
"libstdc++ not found for '%0'; C++ stdlib may be unavailable",
(StringRef))

WARNING(too_many_class_template_instantiations, none,
"template instantiation for '%0' not imported: too many instantiations",
(StringRef))

NOTE(macro_not_imported_unsupported_operator, none, "operator not supported in macro arithmetic", ())
NOTE(macro_not_imported_unsupported_named_operator, none, "operator '%0' not supported in macro arithmetic", (StringRef))
NOTE(macro_not_imported_invalid_string_literal, none, "invalid string literal", ())
Expand Down
10 changes: 9 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2486,8 +2486,16 @@ namespace {
// deep/complex template, or we've run into an infinite loop. In either
// case, its not worth the compile time, so bail.
// TODO: this could be configurable at some point.
if (llvm::size(decl->getSpecializedTemplate()->specializations()) > 10000)
if (llvm::size(decl->getSpecializedTemplate()->specializations()) >
1000) {
std::string name;
llvm::raw_string_ostream os(name);
decl->printQualifiedName(os);
// Emit a warning if we haven't warned about this decl yet.
if (Impl.tooDeepTemplateSpecializations.insert(name).second)
Impl.diagnose({}, diag::too_many_class_template_instantiations, name);
return nullptr;
}

// `Sema::isCompleteType` will try to instantiate the class template as a
// side-effect and we rely on this here. `decl->getDefinition()` can
Expand Down
4 changes: 4 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
llvm::DenseMap<clang::FunctionDecl *, ValueDecl *>
specializedFunctionTemplates;

/// Stores qualified names of C++ template specializations that were too deep
/// to import into Swift.
llvm::StringSet<> tooDeepTemplateSpecializations;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing the following:

Assertion failed: (isHandleInSync() && "invalid iterator access!"), function operator++, file DenseMap.h, line 1263.

Pretty sure this is the cause.


/// Keeps track of the Clang functions that have been turned into
/// properties.
llvm::DenseMap<const clang::FunctionDecl *, VarDecl *> FunctionsAsProperties;
Expand Down