diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -785,8 +785,7 @@ materializedNotes = py::tuple(numNotes); for (intptr_t i = 0; i < numNotes; ++i) { MlirDiagnostic noteDiag = mlirDiagnosticGetNote(diagnostic, i); - py::object pyNoteDiag = py::cast(PyDiagnostic(noteDiag)); - PyTuple_SET_ITEM(materializedNotes->ptr(), i, pyNoteDiag.ptr()); + materializedNotes.value()[i] = PyDiagnostic(noteDiag); } return *materializedNotes; } diff --git a/mlir/test/python/ir/diagnostic_handler.py b/mlir/test/python/ir/diagnostic_handler.py --- a/mlir/test/python/ir/diagnostic_handler.py +++ b/mlir/test/python/ir/diagnostic_handler.py @@ -85,6 +85,23 @@ assert not handler.had_error +# CHECK-LABEL: TEST: testDiagnosticNonEmptyNotes +@run +def testDiagnosticNonEmptyNotes(): + ctx = Context() + def callback(d): + # CHECK: DIAGNOSTIC: + # CHECK: message='arith.addi' op requires one result + # CHECK: notes=['see current operation: "arith.addi"() : () -> ()'] + print(f"DIAGNOSTIC:") + print(f" message={d.message}") + print(f" notes={list(map(str, d.notes))}") + return True + handler = ctx.attach_diagnostic_handler(callback) + loc = Location.unknown(ctx) + Operation.create('arith.addi', loc=loc).verify() + assert not handler.had_error + # CHECK-LABEL: TEST: testDiagnosticCallbackException @run def testDiagnosticCallbackException():