cython / cython

The most widely used Python to C compiler
https://cython.org
Apache License 2.0
9.54k stars 1.49k forks source link

[BUG] Comparing structs causes invalid C code generation #6456

Open bbb23exposed opened 1 month ago

bbb23exposed commented 1 month ago

Describe the bug

Comparing two struct's equality causes invalid C code to be generated.

cython_ctuple_bug.c:2134:69: error: invalid operands to binary expression ('struct __pyx_t_17cython_ctuple_bug_a' and
      'struct __pyx_t_17cython_ctuple_bug_a')
  __pyx_t_3 = __Pyx_PyBool_FromLong((__pyx_v_17cython_ctuple_bug_a1 == __pyx_v_17cython_ctuple_bug_a2)); if (unlikely(!__pyx_t_3)) __PYX_ERR(...
                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

Code to reproduce the behaviour:

cdef struct a:
    int m1
    int m2

cdef a a1 = a(1, 2)
cdef a a2 = a(3, 4)

print(a1 == a2)

Expected behaviour

We should either generate a cython error or implement struct equality/inequality.

OS

No response

Python version

3.8

Cython version

dev

Additional context

No response

da-woods commented 1 month ago

I'd be a little sceptical of turning this into a Cython error - at least with extern structs there's probably some C++ + Cython code that ends up relying on it.

bbb23exposed commented 1 month ago

I'd be a little sceptical of turning this into a Cython error - at least with extern structs there's probably some C++ + Cython code that ends up relying on it.

I didn't end up doing that. My patch does an element-by-element check for all members and substructs.