New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'final' subclasses can produce uncompilable code #2747

Open
QuLogic opened this Issue Dec 6, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@QuLogic

QuLogic commented Dec 6, 2018

Starting from:

# subclass.pyx
cdef class A:
    cdef int foo(self):
        return -1

    cdef int bar(self, int x):
        return -1

cdef class Sub(A):
    cdef int foo(self):
        return self.bar(0)

    cdef int bar(self, int x):
        return x + 1

compiles fine with cythonize -b subclass.pyx. If you add @cython.final onto Sub, then it produces a non-fatal warning:

gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -m64 -fPIC -m64 -fPIC -fPIC -Iconda/envs/cp36/include/python3.6m -c subclass.c -o build/temp.linux-x86_64-3.6/subclass.o
subclass.c: In function ‘__pyx_f_8subclass_3Sub_foo’:
subclass.c:1667:41: warning: passing argument 1 of ‘__pyx_f_8subclass_3Sub_bar’ from incompatible pointer type [-Wincompatible-pointer-types]
   __pyx_r = __pyx_f_8subclass_3Sub_bar(((struct __pyx_obj_8subclass_A *)__pyx_v_self), 0);
                                        ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subclass.c:1205:85: note: expected ‘struct __pyx_obj_8subclass_Sub *’ but argument is of type ‘struct __pyx_obj_8subclass_A *’
 static int __pyx_f_8subclass_3Sub_bar(CYTHON_UNUSED struct __pyx_obj_8subclass_Sub *__pyx_v_self, int __pyx_v_x); /* proto*/
                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~

But if you also add # distutils: language = c++ to the file to compile as C++, then it fails to compile:

gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -m64 -fPIC -m64 -fPIC -fPIC -Iconda/envs/cp36/include/python3.6m -c subclass.cpp -o build/temp.linux-x86_64-3.6/subclass.o
subclass.cpp: In function ‘int __pyx_f_8subclass_3Sub_foo(__pyx_obj_8subclass_Sub*)’:
subclass.cpp:1682:41: error: cannot convert ‘__pyx_obj_8subclass_A*’ to ‘__pyx_obj_8subclass_Sub*’
   __pyx_r = __pyx_f_8subclass_3Sub_bar(((struct __pyx_obj_8subclass_A *)__pyx_v_self), 0);
                                        ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subclass.cpp:1220:85: note:   initializing argument 1 of ‘int __pyx_f_8subclass_3Sub_bar(__pyx_obj_8subclass_Sub*, int)’
 static int __pyx_f_8subclass_3Sub_bar(CYTHON_UNUSED struct __pyx_obj_8subclass_Sub *__pyx_v_self, int __pyx_v_x); /* proto*/
                                                                                     ^
error: command 'gcc' failed with exit status 1
@QuLogic

This comment has been minimized.

QuLogic commented Dec 6, 2018

Note that in the C case (which produces just a warning), it seems to work correctly. That is, if we add:

def do_foo():
    cdef A a 

    a = Sub()
    return a.foo()

and call it, it returns 1 whether there's the cython.final decorator or not.

robertwb added a commit to robertwb/cython that referenced this issue Dec 10, 2018

@robertwb

This comment has been minimized.

Contributor

robertwb commented Dec 10, 2018

Thanks for the report. Indeed, the code is correct, but the C compiler does not understand the subtype relationship among these pointers (hence the warning and error).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment