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

Segfault when manipulating a recursive union #15192

Closed
jonathan-laurent opened this issue May 5, 2023 · 1 comment · Fixed by #15128
Closed

Segfault when manipulating a recursive union #15192

jonathan-laurent opened this issue May 5, 2023 · 1 comment · Fixed by #15128
Labels
bug mypy got something wrong crash

Comments

@jonathan-laurent
Copy link

Bug Report

Running mypy on the following program produces a segfault.

To Reproduce

from typing import Literal, TypeAlias, Union


Expr: TypeAlias = Union[
    tuple[Literal["const"], int],
    tuple[Literal["neg"], "Expr"],
]


def eval(e: Expr) -> int:
    if e[0] == "const":
        return e[1]
    elif e[0] == "neg":
        return -eval(e[1])

Expected Behavior

Mypy returns successfully.

Actual Behavior

Mypy segfaults:

fish: Job 1, 'mypy ...' terminated by signal SIGSEGV (Address boundary error)

The problem can be replicated in the mypy playground.

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11
@hauntsaninja
Copy link
Collaborator

Thanks for the report! I think #15128 will fix this

hauntsaninja added a commit that referenced this issue May 6, 2023
Fixes #15192

The following code optimises make_simplified_union in the common case
that there are exact duplicates in the union. In this regard, this is
similar to #15104

There's a behaviour change in one unit test. I think it's good? We'll
see what mypy_primer has to say.

To get this to work, I needed to use partial tuple fallbacks in a couple
places. These could cause crashes anyway.
There were some interesting things going on with recursive type aliases
and type state assumptions

This is about a 25% speedup on the pydantic codebase and about a 2%
speedup on self check (measured with uncompiled mypy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants