Skip to content
Draft
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
38 changes: 35 additions & 3 deletions bindings/pyroot/cppjit/src/cpyrt/TemplateProxy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions bindings/pyroot/cppjit/src/interop/cppjit_interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<TCppMethod_t>& operators);
RPY_EXPORTED
Expand Down
6 changes: 6 additions & 0 deletions bindings/pyroot/cppjit/src/interop/interop_wrapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<charT" whether or not the default template
// arguments (char_traits, allocator) are spelled out; the character type
Expand Down
3 changes: 3 additions & 0 deletions bindings/pyroot/cppjit/test/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 V>
class CustomVec {
template <class> friend class CustomVec;
V fX;
public:
CustomVec() : fX(0) {}
Expand Down
2 changes: 2 additions & 0 deletions bindings/pyroot/pythonizations/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 <typename T>
struct GH11854Helper {
std::size_t operator() () const {
const std::size_t res = 0;
res = T{0, 0}.size();
return res;
}
};

template <typename H>
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 <typename T>
T diag_only_floating(T x) {
static_assert(std::is_floating_point_v<T>, "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 <typename T>
T diag_only_floating_explicit(T x) {
static_assert(std::is_floating_point_v<T>, "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 <typename T>
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 <typename T>
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()
76 changes: 71 additions & 5 deletions interpreter/CppInterOp/lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<clang::DiagnosticConsumer> fOldOwnedClient;
std::string fText;
llvm::raw_string_ostream fStream;
clang::TextDiagnosticPrinter fPrinter;
};
} // namespace

// See TClingClassInfo::IsLoaded
Expand Down Expand Up @@ -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<FuncRef>& candidates,
const std::vector<TemplateArgInfo>& explicit_types,
const std::vector<TemplateArgInfo>& arg_types) {
INTEROP_TRACE(candidates, explicit_types, arg_types);
LastOverloadMatchDiagnostics.clear();
auto& S = getSema();
auto& C = S.getASTContext();

Expand Down Expand Up @@ -3114,13 +3165,28 @@ BestOverloadFunctionMatch(const std::vector<FuncRef>& 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;
Expand Down
10 changes: 10 additions & 0 deletions interpreter/CppInterOp/lib/CppInterOp/CppInterOp.td
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down