Skip to content

[BUG] Compiler crash on binary operators in constants #3918

@petrochenkov

Description

@petrochenkov

Describe the bug
The next cython compiler crash is observed:

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from *:
    const int A = 0
    const int B = A + 1
                   ^
------------------------------------------------------------

my.pyx:3:20: Compiler crash in OptimizeBuiltinCalls

ModuleNode.body = StatListNode(my.pyx:1:5)
StatListNode.stats[0] = CDefExternNode(my.pyx:1:5)
CDefExternNode.body = StatListNode(my.pyx:2:4)
StatListNode.stats[1] = SingleAssignmentNode(my.pyx:3:4)
SingleAssignmentNode.rhs = AddNode(my.pyx:3:20,
    infix = True,
    operator = '+',
    result_is_used = True,
    use_managed_ref = True)

Compiler crash traceback from this point on:
  File "c:\python39\lib\site-packages\Cython\Compiler\Visitor.py", line 180, in _visit
    return handler_method(obj)
  File "c:\python39\lib\site-packages\Cython\Compiler\Visitor.py", line 531, in visit_BinopNode
    return self._visit_binop_node(node)
  File "c:\python39\lib\site-packages\Cython\Compiler\Visitor.py", line 545, in _visit_binop_node
    if obj_type.is_builtin_type:
AttributeError: 'NoneType' object has no attribute 'is_builtin_type'

To Reproduce
Code to reproduce the behaviour:

cdef extern from *:
    const int A = 0
    const int B = A + 1

Expected behavior
Successful compilation.

Environment

  • OS: Windows
  • Python 3.9.0
  • Cython 0.29.21

Additional context
Found when testing auto-generation of .pxd files from Rust libraries (mozilla/cbindgen#590).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions