From e516050f6c517d03198d4a5ea43ab600ab4efad0 Mon Sep 17 00:00:00 2001 From: Josh Bendavid Date: Sat, 7 Nov 2026 21:58:40 -0500 Subject: [PATCH 1/4] [cppinterop] Reject ill-formed template bodies in BestOverloadFunctionMatch and capture their diagnostics [upstream] The body of the best match was instantiated with all diagnostics suppressed, and the match only rejected if the specialization itself was marked invalid. Errors which leave it valid, such as a failing static_assert or an error in a nested instantiation, were therefore ignored: the former resulted in running code which should not have compiled, the latter in a failure at link time for the missing nested definition, without the actual compiler error being reported anywhere. Now the match is rejected if any error occurred during the instantiation (using a DiagnosticErrorTrap), and the specialization marked invalid so that later lookups reject it as well. The diagnostics are rendered into a string as clang would print them, rather than suppressed, and made available through the new GetLastOverloadMatchDiagnostics, so that callers can report them where appropriate while probing for a valid match stays quiet. Upstream: https://github.com/compiler-research/CppInterOp/pull/1179 (the same change on top of the reworked BestOverloadFunctionMatch on CppInterOp main, which also includes the earlier ROOT patch "Reject ill-formed instantiations in BestOverloadFunctionMatch" that this builds on). Co-Authored-By: Claude Opus 5.5 --- .../CppInterOp/lib/CppInterOp/CppInterOp.cpp | 76 +++++++++++++++++-- .../CppInterOp/lib/CppInterOp/CppInterOp.td | 10 +++ 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.cpp b/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.cpp index 7c9736e73e064..6a784f3b8dcc6 100644 --- a/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.cpp +++ b/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.cpp @@ -71,6 +71,7 @@ #include "clang/Basic/Specifiers.h" #include "clang/Basic/Version.h" #include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Interpreter/Interpreter.h" #include "clang/Sema/Lookup.h" #include "clang/Sema/Overload.h" @@ -762,6 +763,46 @@ class clangSilent { clang::IgnoringDiagConsumer fIgnoringClient; #endif }; + +/// Renders the diagnostics emitted during its lifetime into a string, formatted +/// as clang prints them (with source snippets and instantiation notes), instead +/// of passing them to the installed consumer. Like clangSilent this keeps +/// probing operations quiet, but the diagnostics remain available to report +/// where appropriate. +class clangCapture { +public: + clangCapture(clang::DiagnosticsEngine& diag, const clang::LangOptions& LO, + const clang::Preprocessor* PP) + : fDiagEngine(diag), fOldClient(diag.getClient()), + fOldOwnedClient(diag.takeClient()), fStream(fText), + fPrinter(fStream, diag.getDiagnosticOptions()) { + // required to initialize the printer's internal clang::TextDiagnostic + fPrinter.BeginSourceFile(LO, PP); + fDiagEngine.setClient(&fPrinter, /*ShouldOwnClient=*/false); + } + + ~clangCapture() { + fPrinter.EndSourceFile(); + if (fOldOwnedClient) + fDiagEngine.setClient(fOldOwnedClient.release(), + /*ShouldOwnClient=*/true); + else + fDiagEngine.setClient(fOldClient, /*ShouldOwnClient=*/false); + } + + const std::string& str() { + fStream.flush(); + return fText; + } + +private: + clang::DiagnosticsEngine& fDiagEngine; + clang::DiagnosticConsumer* fOldClient; + std::unique_ptr fOldOwnedClient; + std::string fText; + llvm::raw_string_ostream fStream; + clang::TextDiagnosticPrinter fPrinter; +}; } // namespace // See TClingClassInfo::IsLoaded @@ -2999,12 +3040,22 @@ bool GetClassTemplatedMethods(const std::string& name, ConstDeclRef parent, return INTEROP_RETURN(!funcs.empty()); } +// Diagnostics from the instantiation of the body of the best match in the last +// call to BestOverloadFunctionMatch on this thread +static thread_local std::string LastOverloadMatchDiagnostics; + +std::string GetLastOverloadMatchDiagnostics() { + INTEROP_TRACE(); + return INTEROP_RETURN(LastOverloadMatchDiagnostics); +} + // Adapted from inner workings of Sema::BuildCallExpr FuncRef BestOverloadFunctionMatch(const std::vector& candidates, const std::vector& explicit_types, const std::vector& arg_types) { INTEROP_TRACE(candidates, explicit_types, arg_types); + LastOverloadMatchDiagnostics.clear(); auto& S = getSema(); auto& C = S.getASTContext(); @@ -3114,13 +3165,28 @@ BestOverloadFunctionMatch(const std::vector& candidates, // failure in a SFINAE context but only a warning in a function body. // DefinitionRequired stays false: a pattern without a body (defined in a // library) is still a valid match. + // The body is ill-formed if any error occurred during its instantiation + // (including those of the templates it instantiates in turn), not only if + // the specialization itself was marked invalid: e.g. a failing static_assert + // or an error in a nested instantiation leaves the specialization valid, and + // accepting it would either run code that should not have compiled, or fail + // at link time for the missing nested definition. The diagnostics are + // captured rather than printed, and can be retrieved with + // GetLastOverloadMatchDiagnostics. if (Result && Result->isTemplateInstantiation() && !Result->isDefined()) { DiagnosticsEngine& Diags = S.getDiagnostics(); - bool OldSuppress = Diags.getSuppressAllDiagnostics(); - Diags.setSuppressAllDiagnostics(true); - S.InstantiateFunctionDefinition(Loc, Result, /*Recursive=*/true); - Diags.setSuppressAllDiagnostics(OldSuppress); - if (Result->isInvalidDecl()) { + bool Failed; + { + clangCapture Capture(Diags, S.getLangOpts(), &S.getPreprocessor()); + clang::DiagnosticErrorTrap ErrorTrap(Diags); + S.InstantiateFunctionDefinition(Loc, Result, /*Recursive=*/true); + Failed = Result->isInvalidDecl() || ErrorTrap.hasErrorOccurred(); + LastOverloadMatchDiagnostics = Capture.str(); + } + if (Failed) { + // Mark the specialization invalid, so that later lookups reject it as a + // substitution failure instead of finding it already defined. + Result->setInvalidDecl(); Diags.Reset(/*soft=*/true); Diags.getClient()->clear(); Result = nullptr; diff --git a/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.td b/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.td index f7044decabd41..106c793570a5d 100644 --- a/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.td +++ b/interpreter/CppInterOp/lib/CppInterOp/CppInterOp.td @@ -1237,6 +1237,16 @@ def BestOverloadFunctionMatch : CppInterOpAPI { ]; } +def GetLastOverloadMatchDiagnostics : CppInterOpAPI { + let Doc = [{\returns the compiler diagnostics (errors and warnings, formatted as +clang prints them) from the instantiation of the body of the best match in the +last call to BestOverloadFunctionMatch on this thread, or an empty string if no +body was instantiated. They are not printed, since overload resolution may be +used to probe for a valid match, and a match is rejected if its body turns out +to be ill-formed.}]; + let ReturnType = "std::string"; +} + def CodeComplete : CppInterOpAPI { let Doc = [{Append all code completion suggestions to Results. \param[out] Results CC suggestions for code fragment (appended). From a956725e563bcf9d4a2303f69f33a452b6d46d71 Mon Sep 17 00:00:00 2001 From: Josh Bendavid Date: Sat, 7 Nov 2026 21:58:40 -0500 Subject: [PATCH 2/4] [cppjit] report compiler diagnostics from template instantiations Include the compiler errors from the instantiation of a template called from python in the "Failed to instantiate" error, and report compiler warnings from a successful instantiation as a python UserWarning, using the diagnostics captured by CppInterOp's overload resolution. Co-Authored-By: Claude Opus 5.5 --- .../pyroot/cppjit/src/cpyrt/TemplateProxy.cxx | 38 +++++++++++++++++-- .../cppjit/src/interop/cppjit_interop.h | 2 + .../cppjit/src/interop/interop_wrapper.cxx | 6 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/bindings/pyroot/cppjit/src/cpyrt/TemplateProxy.cxx b/bindings/pyroot/cppjit/src/cpyrt/TemplateProxy.cxx index e3b6dddaa16ff..02818bd0907a5 100644 --- a/bindings/pyroot/cppjit/src/cpyrt/TemplateProxy.cxx +++ b/bindings/pyroot/cppjit/src/cpyrt/TemplateProxy.cxx @@ -73,6 +73,23 @@ void TemplateProxy::AdoptTemplate(PyCallable* pc) { fTI->fTemplated->AdoptMethod(pc); } +//---------------------------------------------------------------------------- +static std::string IndentDiagnostics(const std::string& diags) { + // Indent the (multi-line) compiler diagnostics to nest them under the + // message they belong to + std::string res; + size_t start = 0; + while (start < diags.size()) { + size_t end = diags.find('\n', start); + if (end == std::string::npos) + end = diags.size(); + res += "\n "; + res.append(diags, start, end - start); + start = end + 1; + } + return res; +} + //---------------------------------------------------------------------------- PyObject* TemplateProxy::Instantiate(const std::string& fname, cpyrt_PyArgs_t args, size_t nargsf, @@ -244,11 +261,26 @@ PyObject* TemplateProxy::Instantiate(const std::string& fname, interop::TCppScope_t scope = ((CPPClass*)fTI->fPyClass)->fCppType; interop::TCppMethod_t cppmeth = interop::GetMethodTemplate(scope, fname, proto); + // Compiler diagnostics from instantiating the body of the match: they are not + // printed, so report them here, with the error if the instantiation failed, + // or as a warning if it succeeded + std::string diags = interop::GetLastOverloadMatchDiagnostics(); if (!cppmeth && !protoPair.empty() && protoPair != proto) { // retry with 2-tuples typed as std::pair instead of std::initializer_list cppmeth = interop::GetMethodTemplate(scope, fname, protoPair); - if (cppmeth) + const std::string& diagsPair = interop::GetLastOverloadMatchDiagnostics(); + if (cppmeth) { proto = protoPair; + diags = diagsPair; + } else + diags += diagsPair; + } + if (cppmeth && !diags.empty()) { + const std::string& msg = "Compiler warnings during instantiation of \"" + + fname + "(" + proto + ")\"" + + IndentDiagnostics(diags); + if (PyErr_WarnEx(PyExc_UserWarning, msg.c_str(), 1) < 0) + return nullptr; // warnings turned into errors } if (cppmeth) { // overload stops here // A successful instantiation needs to be cached to pre-empt future @@ -392,8 +424,8 @@ PyObject* TemplateProxy::Instantiate(const std::string& fname, return pymeth; } - PyErr_Format(PyExc_TypeError, "Failed to instantiate \"%s(%s)\"", - fname.c_str(), proto.c_str()); + PyErr_Format(PyExc_TypeError, "Failed to instantiate \"%s(%s)\"%s", + fname.c_str(), proto.c_str(), IndentDiagnostics(diags).c_str()); return nullptr; } diff --git a/bindings/pyroot/cppjit/src/interop/cppjit_interop.h b/bindings/pyroot/cppjit/src/interop/cppjit_interop.h index 574bbdca60607..5396ff54258af 100644 --- a/bindings/pyroot/cppjit/src/interop/cppjit_interop.h +++ b/bindings/pyroot/cppjit/src/interop/cppjit_interop.h @@ -336,6 +336,8 @@ RPY_EXPORTED TCppMethod_t GetMethodTemplate(TCppScope_t scope, const std::string& name, const std::string& proto); RPY_EXPORTED +std::string GetLastOverloadMatchDiagnostics(); +RPY_EXPORTED void GetClassOperators(TCppScope_t klass, const std::string& opname, std::vector& operators); RPY_EXPORTED diff --git a/bindings/pyroot/cppjit/src/interop/interop_wrapper.cxx b/bindings/pyroot/cppjit/src/interop/interop_wrapper.cxx index e4674b8e76a1c..bd91ff096735a 100644 --- a/bindings/pyroot/cppjit/src/interop/interop_wrapper.cxx +++ b/bindings/pyroot/cppjit/src/interop/interop_wrapper.cxx @@ -1906,6 +1906,12 @@ interop::TCppMethod_t interop::GetMethodTemplate(TCppScope_t scope, // if it fails, use Sema to propogate info about why it failed (DeductionInfo) } +std::string interop::GetLastOverloadMatchDiagnostics() { + // diagnostics from instantiating the body of the match in the last + // GetMethodTemplate (overload resolution) on this thread + return Cpp::GetLastOverloadMatchDiagnostics(); +} + static inline bool is_basic_string_of(const std::string& n, const char* charT) { // Match "std::basic_string Date: Sat, 7 Nov 2026 21:58:40 -0500 Subject: [PATCH 3/4] [python] add tests for diagnostics from template instantiations, fix test19_templated_operator_add Add tests for the reporting of errors and warnings from template instantiations, including the regression test for #11854, and rejecting instantiations with a failing static_assert. The operator+ in test19_templated_operator_add accesses the private member of other specializations, which is ill-formed (and rejected by gcc and clang) and so is now rejected also when instantiated from python, so add the needed friend declaration. Co-Authored-By: Claude Opus 5.5 --- bindings/pyroot/cppjit/test/test_templates.py | 3 + .../pyroot/pythonizations/test/CMakeLists.txt | 2 + .../template_instantiation_diagnostics.py | 95 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py diff --git a/bindings/pyroot/cppjit/test/test_templates.py b/bindings/pyroot/cppjit/test/test_templates.py index 584c60c0e0c97..f4912d55f5f8c 100644 --- a/bindings/pyroot/cppjit/test/test_templates.py +++ b/bindings/pyroot/cppjit/test/test_templates.py @@ -533,10 +533,13 @@ def test19_templated_operator_add(self): import cppjit import cppjit.gbl as gbl + # the friend declaration is needed for operator+ to access fX of other + # specializations, otherwise its instantiation is (correctly) rejected cppjit.cppdef("""\ namespace OperatorAddTest { template class CustomVec { + template friend class CustomVec; V fX; public: CustomVec() : fX(0) {} diff --git a/bindings/pyroot/pythonizations/test/CMakeLists.txt b/bindings/pyroot/pythonizations/test/CMakeLists.txt index e40a2175e84c4..41d8c8cbabd6b 100644 --- a/bindings/pyroot/pythonizations/test/CMakeLists.txt +++ b/bindings/pyroot/pythonizations/test/CMakeLists.txt @@ -140,3 +140,5 @@ ROOT_ADD_PYUNITTEST(package_metadata package_metadata.py) ROOT_ADD_PYUNITTEST(regression_23728 regression_23728.py) ROOT_ADD_PYUNITTEST(pyroot_array_numpy_views array_conversions.py PYTHON_DEPS numpy) + +ROOT_ADD_PYUNITTEST(pyroot_template_instantiation_diagnostics template_instantiation_diagnostics.py) diff --git a/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py b/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py new file mode 100644 index 0000000000000..6f4e230e03934 --- /dev/null +++ b/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py @@ -0,0 +1,95 @@ +import unittest +import warnings + +import ROOT + + +class TemplateInstantiationDiagnostics(unittest.TestCase): + """Errors and warnings from the instantiation of templates called from Python""" + + def test_gh_11854(self): + """Regression test for https://github.com/root-project/root/issues/11854: the compiler + error in a nested instantiation is reported, rather than a missing symbol""" + + ROOT.gInterpreter.Declare(r""" + template + struct GH11854Helper { + std::size_t operator() () const { + const std::size_t res = 0; + res = T{0, 0}.size(); + return res; + } + }; + + template + std::size_t call_gh11854_helper(const H &helper) { + return helper(); + } + """) + + helper = ROOT.GH11854Helper[ROOT.std.vector["double"]]() + with self.assertRaisesRegex(TypeError, "cannot assign to variable 'res' with const-qualified type"): + ROOT.call_gh11854_helper(helper) + + def test_static_assert_deduced(self): + """An instantiation with a failing static_assert is rejected (rather than run), with its message""" + + ROOT.gInterpreter.Declare(r""" + template + T diag_only_floating(T x) { + static_assert(std::is_floating_point_v, "diag_only_floating requires a floating point type"); + return x; + } + """) + + self.assertEqual(ROOT.diag_only_floating(1.5), 1.5) + with self.assertRaisesRegex(TypeError, "diag_only_floating requires a floating point type"): + ROOT.diag_only_floating(ROOT.std.string("abc")) + + def test_static_assert_explicit(self): + """Same for explicitly given template arguments""" + + ROOT.gInterpreter.Declare(r""" + template + T diag_only_floating_explicit(T x) { + static_assert(std::is_floating_point_v, "diag_only_floating_explicit requires a floating point type"); + return x; + } + """) + + with self.assertRaisesRegex(TypeError, "diag_only_floating_explicit requires a floating point type"): + ROOT.diag_only_floating_explicit["int"](3) + # also when trying again + with self.assertRaises(TypeError): + ROOT.diag_only_floating_explicit["int"](3) + + def test_error_in_body(self): + """The compiler error in the body is reported""" + + ROOT.gInterpreter.Declare(r""" + template + auto diag_call_foo(const T &x) { return x.foo(); } + """) + + with self.assertRaisesRegex(TypeError, "no member named 'foo'"): + ROOT.diag_call_foo(ROOT.std.vector["int"]()) + + def test_warning(self): + """Compiler warnings from a successful instantiation are reported as a python warning""" + + ROOT.gInterpreter.Declare(r""" + template + T diag_shift_big(T x) { return x << 40; } + """) + + with self.assertWarnsRegex(UserWarning, "shift count >= width of type"): + ROOT.diag_shift_big(1) + + # no warning when the cached instantiation is used again, or for an instantiation without + with warnings.catch_warnings(): + warnings.simplefilter("error") + ROOT.diag_shift_big(1) + self.assertEqual(ROOT.diag_shift_big["long"](1), 1 << 40) + +if __name__ == '__main__': + unittest.main() From b08bf0f308156040120799e36f38ffcc872a5d7c Mon Sep 17 00:00:00 2001 From: Josh Bendavid Date: Sun, 8 Nov 2026 10:50:51 -0500 Subject: [PATCH 4/4] [python] format the template instantiation diagnostics test with ruff Co-Authored-By: Claude Opus 5.5 --- .../pythonizations/test/template_instantiation_diagnostics.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py b/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py index 6f4e230e03934..e24f253e53034 100644 --- a/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py +++ b/bindings/pyroot/pythonizations/test/template_instantiation_diagnostics.py @@ -91,5 +91,6 @@ def test_warning(self): ROOT.diag_shift_big(1) self.assertEqual(ROOT.diag_shift_big["long"](1), 1 << 40) -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main()