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

TypingError when conditionally shadowing variable #9587

Open
ricardoV94 opened this issue May 24, 2024 · 2 comments
Open

TypingError when conditionally shadowing variable #9587

ricardoV94 opened this issue May 24, 2024 · 2 comments
Labels
Blocked awaiting long term feature For PRs/Issues that require the implementation of a long term plan feature bug Effort - long Long size effort needed

Comments

@ricardoV94
Copy link

ricardoV94 commented May 24, 2024

I'm not sure if this is expected/documented behavior, but I see a typing failure when conditionally shadowing an existing name:

import numba
import numpy as np

@numba.njit
def inc_array(x, val):
    # No shadowing
    if x.ndim == val.ndim:
        new_val = val[0]
    else:
        new_val = val
    x[0] += new_val

inc_array(np.zeros((5, 3)), np.array([[2.]]))  # Fine

@numba.njit
def inc_array(x, val):
    # Unconditional shadowing
    val = val[0]
    x[0] += val
    
inc_array(np.zeros((5, 3)), np.array([[2.]]))  # Fine 

@numba.njit
def inc_array(x, val):
    # Conditional shadowing
    if x.ndim == val.ndim:
        val = val[0]
    x[0] += val

inc_array(np.zeros((5, 3)), np.array([[2.]]))  # Fails: Cannot unify array(float64, 1d, C) and float64 for 'val.2'
@ricardoV94 ricardoV94 changed the title TypingError when conditionally reassigning variable TypingError when conditionally shadowing variable May 24, 2024
@sklam
Copy link
Member

sklam commented May 24, 2024

That is interesting. I'll need to take a deeper look. So far, it seems to be a problem for running SSA after deadbranch pruning. The first version work because deadbranch pruning eliminated the else branch. But I think the third version reuse of val is somehow disabling the pruning.

@sklam sklam added needtriage bug - typing Bugs: occuring at typing time labels May 24, 2024
@sklam
Copy link
Member

sklam commented May 28, 2024

Conclusion from triage meeting:

This happens because deadbranch pruning is running before SSA. To resolve this bug, we need to make deadbranch pruning SSA aware and then move SSA above it.

@sklam sklam added Blocked awaiting long term feature For PRs/Issues that require the implementation of a long term plan feature Effort - long Long size effort needed bug and removed needtriage bug - typing Bugs: occuring at typing time labels May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked awaiting long term feature For PRs/Issues that require the implementation of a long term plan feature bug Effort - long Long size effort needed
Projects
None yet
Development

No branches or pull requests

2 participants