Skip to content
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

Catch redeclaration of var by cfunc #4661

Merged

Conversation

0dminnimda
Copy link
Contributor

Fixes #600

To reproduce:

cdef class Bar:
    cdef list _operands

    cdef int _operands(self):
        return -1

This is how the error looks right now:

Traceback (most recent call last):
  File "c:\Python\projects\cython\venv\cyth.py", line 15, in <module>
    ext_modules=cythonize(
  File "c:\python\projects\cython\Cython\Build\Dependencies.py", line 1143, in cythonize
    cythonize_one(*args)
  File "c:\python\projects\cython\Cython\Build\Dependencies.py", line 1289, in cythonize_one
    result = compile_single(pyx_file, options, full_module_name=full_module_name)
  File "c:\python\projects\cython\Cython\Compiler\Main.py", line 576, in compile_single
    return run_pipeline(source, options, full_module_name)
  File "c:\python\projects\cython\Cython\Compiler\Main.py", line 504, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "c:\python\projects\cython\Cython\Compiler\Pipeline.py", line 394, in run_pipeline
    data = run(phase, data)
  File "c:\python\projects\cython\Cython\Compiler\Pipeline.py", line 371, in run
    return phase(data)
  File "c:\python\projects\cython\Cython\Compiler\Pipeline.py", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "c:\python\projects\cython\Cython\Compiler\ModuleNode.py", line 206, in process_implementation
    self.generate_c_code(env, options, result)
  File "c:\python\projects\cython\Cython\Compiler\ModuleNode.py", line 501, in generate_c_code
    self.body.generate_function_definitions(env, code)
  File "c:\python\projects\cython\Cython\Compiler\Nodes.py", line 403, in generate_function_definitions
    stat.generate_function_definitions(env, code)
  File "c:\python\projects\cython\Cython\Compiler\Nodes.py", line 5289, in generate_function_definitions
    self.body.generate_function_definitions(self.scope, code)
  File "c:\python\projects\cython\Cython\Compiler\Nodes.py", line 403, in generate_function_definitions
    stat.generate_function_definitions(env, code)
  File "c:\python\projects\cython\Cython\Compiler\Nodes.py", line 2293, in generate_function_definitions
    err_val = self.error_value()
  File "c:\python\projects\cython\Cython\Compiler\Nodes.py", line 2853, in error_value
    return self.entry.type.exception_value
AttributeError: 'BuiltinObjectType' object has no attribute 'exception_value'

After fix:

Error compiling Cython file:
------------------------------------------------------------
...
cdef class Bar:
    cdef list _operands

    cdef int _operands(self):
        ^
------------------------------------------------------------

test.pyx:4:9: '_operands' redeclared

Error compiling Cython file:
------------------------------------------------------------
...
cdef class Bar:
    cdef list _operands
             ^
------------------------------------------------------------

test.pyx:2:14: Previous declaration is here
Traceback (most recent call last):
  File "c:\Python\projects\cython\venv\cyth.py", line 15, in <module>
    ext_modules=cythonize(
  File "c:\python\projects\cython\Cython\Build\Dependencies.py", line 1143, in cythonize
    cythonize_one(*args)
  File "c:\python\projects\cython\Cython\Build\Dependencies.py", line 1310, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: test.pyx

What's interesting is before the fix this would just compile

cdef class Bar:
    cdef list _operands

    cdef _operands(self):
        pass

@scoder scoder added this to the 3.0 milestone Feb 25, 2022
@scoder
Copy link
Contributor

scoder commented Feb 25, 2022

I'm not 100% sure that there are no cases where a warning would be better than an error here, thinking of function pointers, mostly, but I guess the best way to find out is to merge this change and see if someone reports it for the next alpha. :)

@scoder
Copy link
Contributor

scoder commented Feb 25, 2022

This probably also rejects the case of cpdef methods instead of cdef methods, right?

@scoder scoder merged commit b7ff822 into cython:master Feb 25, 2022
@0dminnimda 0dminnimda deleted the Catch-redeclaration-of-var-by-cfunc branch February 25, 2022 15:45
@0dminnimda
Copy link
Contributor Author

This probably also rejects the case of cpdef methods instead of cdef methods, right?

I'm not sure, there were some simmilar warnings in the methods for other constructs, I'm gonna investigate this.

@0dminnimda
Copy link
Contributor Author

Yep, this change also catches cpdef

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

Successfully merging this pull request may close these issues.

Mysterious error with conflicting types
2 participants