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

Variable incorrectly perisists in loop when I redefine a print method #52664

Open
DarkClaw1 opened this issue Dec 29, 2023 · 0 comments
Open
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) kind:bug Indicates an unexpected problem or unintended behavior parser Language parsing and surface syntax

Comments

@DarkClaw1
Copy link

DarkClaw1 commented Dec 29, 2023

I asked about this on the discourse and it was recommended I open an issue:
https://discourse.julialang.org/t/redefining-print-method-changes-variable-scoping-in-loop/108010

Essentially the idx_last variable persists when it should not.

using Printf
for n in 1:10
    if n == 1
        flag     = true
        idx_last = 1
    else
        flag     = true
        idx_last = ifelse(flag, n, idx_last)
    end
    Base.show(io::IO, f::Float64) = @printf(io, "%.2f", f)
end

If I comment out the Base.show() line then I correctly see UndefVarError: idx_last not defined.

This happened for me using versions 10.10.0-rc2 and 1.10.0. Apparently the expected behavior was seen using v1.9.4 on windows. Also, from the discourse you can see we verified idx_last is never in the global scope. My versioninfo:

Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 64 × AMD Ryzen Threadripper 2990WX 32-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver1)
  Threads: 1 on 64 virtual cores

@vchuravy vchuravy added compiler:lowering Syntax lowering (compiler front end, 2nd stage) kind:bug Indicates an unexpected problem or unintended behavior parser Language parsing and surface syntax labels Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage) kind:bug Indicates an unexpected problem or unintended behavior parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

2 participants