python / python/mypy

Mypyc gcc compilation fails with specific duplicate variable definition

Offen
#19,557 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

crash topic-mypyc
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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
...

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Beispiel in simple.py mit mypyc zu reproduzieren und build/package_name/__native_simple.c rund um CPyDef_next_down zu untersuchen. Lies die zugehörige Konvertierungslogik in CPy.h, insbesondere CPyLong_AsUInt8, und vergleiche, wie die wiederholte Zuweisung den fehlschlagenden generierten Code erzeugt. Erledigt ist die Aufgabe, wenn diese Eingabe nicht mehr bei der GCC-Kompilierung fehlschlägt und eine angemessene Regressionstestabdeckung oder Diagnosen vorhanden sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.