python / python/mypy

Mypyc gcc compilation fails with specific duplicate variable definition

Aperta
#19,557 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

crash topic-mypyc
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l’esempio in simple.py con mypyc e analizza build/package_name/__native_simple.c nelle vicinanze di CPyDef_next_down. Leggi la logica di conversione correlata in CPy.h, in particolare CPyLong_AsUInt8, e confronta il modo in cui l’assegnazione ripetuta produce il codice generato che fallisce. Il lavoro è concluso quando questo input non causa più un errore di compilazione con GCC e sono presenti un’adeguata copertura di regressione o diagnostica.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.