python / python/mypy

Mypyc gcc compilation fails with specific duplicate variable definition

Open
#19,557 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

crash topic-mypyc
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

Using mypyc to compile simple.py (slimmed down example of real use-case wherein I accidentally set typed variable twice in different ways) leads to compilation failure with GCC instead of mypy noticing something is amiss.

simple.py

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from mypy_extensions import u8


def next_down(
    depth: u8 = 5,
) -> u8:
    next_down = depth - 1

    ##### KEY TO FAILURE #####
    next_down = None if depth is None else depth - 1
    ##########################
    return next_down


if __name__ == "__main__":
    print(next_down())
mypyc simple.py

Traceback

/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/setuptools/config/_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!

        ********************************************************************************
        Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).

        By 2026-Feb-18, you need to update your project and remove deprecated calls
        or your builds will no longer be supported.

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  corresp(dist, value, root_dir)
running build_ext
building 'package_name.simple__mypyc' extension
x86_64-linux-gnu-gcc -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O2 -Wall -fPIC -I/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt -Ibuild -I/home/<username>/Desktop/mypyc-crash/.venv/include -I/usr/include/python3.13 -c build/package_name/__native_simple.c -o build/temp.linux-x86_64-cpython-313/build/package_name/__native_simple.o -O3 -g1 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-but-set-variable -Wno-ignored-optimization-argument -Wno-cpp
In file included from /home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/init.c:2,
                 from build/package_name/__native_simple.c:1:
In function ‘CPyLong_AsUInt8’,
    inlined from ‘CPyDef_next_down’ at build/package_name/__native_simple.c:95:16:
/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/CPy.h:396:16: error: array subscript ‘PyLongObject {aka struct _longobject}[0]’ is partly outside array bounds of ‘PyObject[1]’ {aka ‘struct _object[1]’} [-Werror=array-bounds=]
  396 |         size_t tag = CPY_LONG_TAG(lobj);
      |                ^~~
In file included from /usr/include/python3.13/Python.h:72,
                 from /home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/init.c:1:
/usr/include/python3.13/object.h: In function ‘CPyDef_next_down’:
/usr/include/python3.13/object.h:1101:22: note: object ‘_Py_NoneStruct’ of size 16
 1101 | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
      |                      ^~~~~~~~~~~~~~
In function ‘CPyLong_AsUInt8’,
    inlined from ‘CPyDef_next_down’ at build/package_name/__native_simple.c:95:16:
/home/<username>/Desktop/mypyc-crash/.venv/lib/python3.13/site-packages/mypyc/lib-rt/CPy.h:399:19: error: array subscript ‘PyLongObject {aka struct _longobject}[0]’ is partly outside array bounds of ‘PyObject[1]’ {aka ‘struct _object[1]’} [-Werror=array-bounds=]
  399 |             digit x = CPY_LONG_DIGIT(lobj, 0);
      |                   ^
/usr/include/python3.13/object.h: In function ‘CPyDef_next_down’:
/usr/include/python3.13/object.h:1101:22: note: object ‘_Py_NoneStruct’ of size 16
 1101 | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
      |                      ^~~~~~~~~~~~~~
At top level:
cc1: note: unrecognized command-line option ‘-Wno-ignored-optimization-argument’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-unknown-warning-option’ may have been intended to silence earlier diagnostics
cc1: note: unrecognized command-line option ‘-Wno-unused-command-line-argument’ may have been intended to silence earlier diagnostics
cc1: all warnings being treated as errors
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1

To Reproduce

See above

Your Environment

  • Mypy version used: mypy 1.17.1 (compiled: yes)
  • Mypy command-line flags: See above
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.13.3 (main, Jun 16 2025, 18:15:32) [GCC 14.2.0] on linux
  • Operating system and version:
> uname -a
Linux ideapad 6.14.0-24-generic #24-Ubuntu SMP PREEMPT_DYNAMIC Sun Jun 15 11:18:07 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
  • GCC version
> x86_64-linux-gnu-gcc --version
x86_64-linux-gnu-gcc (Ubuntu 14.2.0-19ubuntu2) 14.2.0
...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in simple.py with mypyc and inspect build/package_name/__native_simple.c around CPyDef_next_down. Read the related conversion logic in CPy.h, especially CPyLong_AsUInt8, and compare how the repeated assignment produces the failing generated code. Done means this input no longer fails GCC compilation and has appropriate regression coverage or diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.