[Mlir-commits] [mlir] 06488f6 - [mlir] Make the Python binding type casters well-formed under C++23 (#208093)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jul 8 20:50:54 PDT 2026


Author: Nikhil Kalra
Date: 2026-07-08T20:50:49-07:00
New Revision: 06488f6c8d8941cda4973df32d420c71877be3e6

URL: https://github.com/llvm/llvm-project/commit/06488f6c8d8941cda4973df32d420c71877be3e6
DIFF: https://github.com/llvm/llvm-project/commit/06488f6c8d8941cda4973df32d420c71877be3e6.diff

LOG: [mlir] Make the Python binding type casters well-formed under C++23 (#208093)

The nanobind type/attr/loc/value casters in IRCore.h return a by-value
PyType/PyAttribute/PyLocation/PyValue as their DerivedTy result.

Under pre-C++23 rules, this was a rvalue that would decay to a lvalue
when passed to the DerivedTy constructor.

Under C++23's P2266 (implicit move on return), that return operand is an
xvalue, which cannot bind the PyConcrete* constructors that take a
non-const lvalue reference.

This patch constructs the derived type explicitly (return
DerivedTy(arg);) using the lvalue constructor; this preserves the
pre-C++23 behavior on C++23 builds.

Added: 
    

Modified: 
    mlir/include/mlir/Bindings/Python/IRCore.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Bindings/Python/IRCore.h b/mlir/include/mlir/Bindings/Python/IRCore.h
index 1b8c232b009c0..43a751bc9cd9f 100644
--- a/mlir/include/mlir/Bindings/Python/IRCore.h
+++ b/mlir/include/mlir/Bindings/Python/IRCore.h
@@ -996,7 +996,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteType : public BaseTy {
       PyGlobals::get().registerTypeCaster(
           DerivedTy::getTypeIdFunction(),
           nanobind::cast<nanobind::callable>(nanobind::cpp_function(
-              [](PyType pyType) -> DerivedTy { return pyType; })),
+              [](PyType pyType) -> DerivedTy { return DerivedTy(pyType); })),
           /*replace*/ true);
     }
 
@@ -1137,7 +1137,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteAttribute : public BaseTy {
           DerivedTy::getTypeIdFunction(),
           nanobind::cast<nanobind::callable>(
               nanobind::cpp_function([](PyAttribute pyAttribute) -> DerivedTy {
-                return pyAttribute;
+                return DerivedTy(pyAttribute);
               })),
           /*replace*/ true);
     }
@@ -1223,7 +1223,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteLocation : public BaseTy {
       PyGlobals::get().registerTypeCaster(
           DerivedTy::getTypeIdFunction(),
           nanobind::cast<nanobind::callable>(nanobind::cpp_function(
-              [](PyLocation pyLoc) -> DerivedTy { return pyLoc; })),
+              [](PyLocation pyLoc) -> DerivedTy { return DerivedTy(pyLoc); })),
           /*replace*/ true);
     }
     DerivedTy::bindDerived(cls);
@@ -1700,7 +1700,7 @@ class MLIR_PYTHON_API_EXPORTED PyConcreteValue : public PyValue {
       PyGlobals::get().registerValueCaster(
           DerivedTy::getTypeIdFunction(),
           nanobind::cast<nanobind::callable>(nanobind::cpp_function(
-              [](PyValue pyValue) -> DerivedTy { return pyValue; })),
+              [](PyValue pyValue) -> DerivedTy { return DerivedTy(pyValue); })),
           /*replace*/ true);
     }
 


        


More information about the Mlir-commits mailing list