Skip to content

False positive for local variable in multiple if statements with the same condition #801

@lkdvos

Description

@lkdvos

JET v0.11 seems to generate false positive warnings for the following setup, where multiple branches with a shared condition are used:

using JET

module MWE

    function f(m, n)

        condition = m < n
        if condition
            some_variable = 3
        end

        mn = m + n

        if condition
            mn += some_variable
        end

        return mn
    end

end

using .MWE

@report_call target_modules = (MWE,) MWE.f(1, 2)

While I agree that this might be a hard issue to solve in general, would there be a way to rewrite this such that JET can recognize that this is never an issue, or is there a fix for JET that can detect this?
I'm struggling to come up with a code pattern that can avoid this in a satisfactory manner.
The simplest approach seems to be to just define some_variable = nothing in the else branches, ensuring that the variable is always defined.
However, I don't think this guarantees that the compiler is always able to optimize away the resulting (union-split) type instability, and while in this specific case it is easy to create a dummy variable of the same type, this is not always easy or cheap (e.g. might require allocations).

This is a duplicate of #795 but with a simpler MWE, I wasn't sure if this needs its own issue or should be attached there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions