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

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

Open
nickrobinson251 opened this issue Jan 23, 2025 · 3 comments
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage) regression 1.12 Regression in the 1.12 release

Comments

@nickrobinson251
Copy link
Contributor

nickrobinson251 commented Jan 23, 2025

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) 
    ...    
@nsajko nsajko added the bug Indicates an unexpected problem or unintended behavior label Jan 23, 2025
@nickrobinson251
Copy link
Contributor Author

Reproduction:

julia> module Repro

       struct BackIRType
           value::Int
           id::Int
           BackIRType(value::Int) = new(value)
       end

       function assign_node_ids(node)
           return assign_node_ids(node, 0)[1]
       end
       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 _copy_and_assign_id::BackIRType, id::Int)
           isdefined(φ, :id) && return φ
           return BackIRType.value, id)
       end

       end
Main.Repro

julia> Repro.assign_node_ids(Repro.BackIRType(1))
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] (::Main.Repro.var"#_traverse#assign_node_ids##0")(φ::Main.Repro.BackIRType)
   @ Main.Repro ./REPL[1]:16
 [2] assign_node_ids
   @ ./REPL[1]:25 [inlined]
 [3] assign_node_ids(node::Main.Repro.BackIRType)
   @ Main.Repro ./REPL[1]:10
 [4] top-level scope
   @ REPL[2]:1

julia> VERSION
v"1.12.0-DEV.1903"

@oscardssmith
Copy link
Member

tagging @Keno since this seems probably related to variable world age changes

@nsajko nsajko added the regression 1.12 Regression in the 1.12 release label Jan 23, 2025
@Keno Keno added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Jan 23, 2025
@nickrobinson251
Copy link
Contributor Author

Bisect pointed to 9650510 (#55297)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage) regression 1.12 Regression in the 1.12 release
Projects
None yet
Development

No branches or pull requests

4 participants