Skip to content

v1.12 throws UndefVarError: not defined in local scope for variable renamed in function before being captured by a closure #57141

@nickrobinson251

Description

@nickrobinson251

I've not yet been able to create a minimal reproduction of this that doesn't depend on a private package (sorry), but i can at least start a bisect to try to find the offending commit.

[Edit: better repro in message below]

In a private package, which has a function like

function assign_node_ids(node, starting_id::Int)
    ii = starting_id

    function _traverse::BackIRType)
        new_φ = copy_and_assign_id(φ, ii)
        if new_φ !== φ
            ii += 1
        end
        return new_φ
    end

    _traverse(n) = n

    return (_traverse(node), ii)
end
function assign_node_ids(node)
    return assign_node_ids(node, 0)[1]
end

Calling the function like

node = make_node(...)
assign_node_ids(node)

throws

ERROR: UndefVarError: `ii` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
Stacktrace:
 [1] (::RAI_BackIR.Utilities.var"#_traverse#assign_node_ids##0")(φ::RAI_BackIR.Atom)
   @ RAI_BackIR.Utilities ~/repos/raicode/packages/RAI_BackIR/src/Utilities/traversal.jl:104
 [2] assign_node_ids
   @ ~/repos/raicode/packages/RAI_BackIR/src/Utilities/traversal.jl:113 [inlined]
 [3] assign_node_ids(node::RAI_BackIR.Atom)
   @ RAI_BackIR.Utilities ~/repos/raicode/packages/RAI_BackIR/src/Utilities/traversal.jl:117
 [4] top-level scope
   @ REPL[1]:1

The error disappears if the function is marked @noinline or the variable isn't renamed in the function, i.e.

@noinline function assign_node_ids(node, starting_id::Int)
    ii = starting_id
    ...
end

or

function assign_node_ids(node, ii::Int) 
    ...    

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorcompiler:loweringSyntax lowering (compiler front end, 2nd stage)regression 1.12Regression in the 1.12 release

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions