Skip to content

invalid cast of an rvalue expression of type #3754

@cmpute

Description

@cmpute

Tested using latest cython, minimal code to reproduce:

# distutils: language = c++
from libcpp.vector cimport vector

a = {1:2, 2:3}
cdef vector[int] b = [1,2,3]
print(a[b[0]])

The compiler will report

/home/jacobz/.conda/envs/lidar/include/python3.7m/pyport.h:109:54: error: invalid cast of an rvalue expression of type ‘Py_ssize_t {aka long int}’ to type ‘int&’
 #define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
                                                      ^
/home/jacobz/PointCloud/detection3/test.cpp:827:43: note: in definition of macro ‘likely’
   #define likely(x)   __builtin_expect(!!(x), 1)
                                           ^
/home/jacobz/PointCloud/detection3/test.cpp:668:42: note: in expansion of macro ‘PY_SSIZE_T_MAX’
           (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||\
                                          ^~~~~~~~~~~~~~
/home/jacobz/PointCloud/detection3/test.cpp:978:6: note: in expansion of macro ‘__Pyx_fits_Py_ssize_t’
     (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\
      ^~~~~~~~~~~~~~~~~~~~~
/home/jacobz/PointCloud/detection3/test.cpp:1644:15: note: in expansion of macro ‘__Pyx_GetItemInt’
   __pyx_t_4 = __Pyx_GetItemInt(__pyx_t_1, __pyx_t_3, int &, 1, __Pyx_PyInt_From_int, 0, 1, 1); if (unlikely(!__pyx_t_4)) __PYX_ERR(1, 6, __pyx_L1_error)

It seems that the type of b[0] is reduced as int&, which is not acceptable by __Pyx_GetItemInt. And if change the cython code to

c = b[0]
print(a[c])

it will work fine

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions