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

UndefVarError line number wrong [0.4 only] #13851

Closed
physicsd00d opened this issue Nov 2, 2015 · 5 comments
Closed

UndefVarError line number wrong [0.4 only] #13851

physicsd00d opened this issue Nov 2, 2015 · 5 comments

Comments

@physicsd00d
Copy link

I'm using v0.4.0.
If I put the following code in a file (scopeError.jl):

for ix in 1:3
    if ix == 2
        y = 3
        print("y = $y\n")
    else
        print("$ix else\n")
        # print("y = $y\n")
    end
end

then run it, I get the expected output:

$ julia scopeError.jl 
1 else
y = 3
3 else

If I uncomment line 7 with the print("y = $y\n") and run it again, I get an error.

$ julia scopeError.jl 
1 else
ERROR: LoadError: UndefVarError: y not defined
 [inlined code] from /Volumes/Storage/Research/Help/scopeError.jl:4
 in anonymous at no file:3
 in include at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
 in include_from_node1 at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
 in process_options at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
 in _start at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
while loading /Volumes/Storage/Research/Help/scopeError.jl, in expression starting on line 1

You'll notice that it's telling me the error is on line 4, inside the if-clause immediately after y's initialization, and not on line 7 inside the else-clause where y is actualy undefined (until ix == 2). At the least, the error message should point to line 7 I think. For the equivalent code, Python puts the error at line 7.

What REALLY confuses me is that the documentation specifically states that if-blocks "do not introduce new scope blocks." In that case, it seems like y should be scoped inside the entire for-loop after it gets initialized at ix==2. Perhaps I'm too used to Python and that's not how it actually works in julia, but I would have expected the following code to run without any errors:

for ix in 1:3
    if ix == 1     # Notice that I changed this from 2 to 1, so y should be initialized for every run through the loop
        y = 3
        print("y = $y\n")
    else
        print("$ix else\n")
        print("y = $y\n")
    end
end

Yet it returns:

y = 3
2 else
ERROR: LoadError: UndefVarError: y not defined
 [inlined code] from /Volumes/Storage/Research/Help/scopeError.jl:4
 in anonymous at no file:3
 in include at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
 in include_from_node1 at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
 in process_options at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
 in _start at /Applications/Julia-0.4.0.app/Contents/Resources/julia/lib/julia/sys.dylib
while loading /Volumes/Storage/Research/Help/scopeError.jl, in expression starting on line 1

The equivalent code works without error in Python. Notice that the above error is again pointing to the wrong place.

@yuyichao
Copy link
Contributor

yuyichao commented Nov 2, 2015

if does not introduce new scope but for does. The y in each loop are independent with each other.

I don't see the line number issue on 0.5.0-1108 with LLVM 3.7.

@vtjnash
Copy link
Member

vtjnash commented Nov 3, 2015

see #13491 for the line number fix

@vtjnash vtjnash changed the title UndefVarError in wrong place, possible scoping issue UndefVarError line number wrong [0.4 only] Nov 3, 2015
@tkelman
Copy link
Contributor

tkelman commented Jan 8, 2016

Same opinion here as #13818 (comment)

This might be a wontfix unless someone can come up with a less-breaking-than-#13491 fix specifically for release-0.4.

@vtjnash
Copy link
Member

vtjnash commented Jan 9, 2016

the line that fixes the bug is https://github.com/JuliaLang/julia/pull/13491/files#diff-6d4d21428a67320600faf5a1a9f3a16aR5911, the rest of the PR was removing old workarounds that were no longer necessary

@tkelman
Copy link
Contributor

tkelman commented Mar 15, 2016

should be fixed by #15385

@tkelman tkelman closed this as completed Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants