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

[Bug]: Error if a variable inside a @check function has the same name as the function #54

Open
arnaud-ma opened this issue Oct 4, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@arnaud-ma
Copy link

What happened?

From this minimal code:

using Supposition, Test

@check function foo(x = Data.Integers{Int64}())
    foo = x^2
    foo >= 0
end

Everything is working fine, but if i use a @testset:

@testset "Supposition" begin
    @check function foo(x = Data.Integers{Int64}())
        foo = x^2
        foo >= 0
    end
end
Encountered an error
  Context: foo

  Arguments:
      x::Int64 = -9223372036854775808

  Exception:
    Message:
      MethodError: objects of type Int64 are not callable
    Maybe you forgot to use an operator such as *, ^, %, / etc. ?
    Stacktrace:
     [1] (::var"##foo__run#645#54"{var"##foo__geninput#644#53"})(643::Supposition.TestCase{Random.Xoshiro})
       @ Main ~/.julia/packages/Supposition/LzWE1/src/api.jl:240
     [2] macro expansion
       @ ~/.julia/packages/Supposition/LzWE1/src/teststate.jl:38 [inlined]
Test Summary: |Time
Supposition   | None  0.3s

What did you expect to happen?

No error, or at least a more useful information than the current one.

Julia Version

Julia Version 1.10.5

Package Environment

[5a0628fe] Supposition v0.3.5
@arnaud-ma arnaud-ma added the bug Something isn't working label Oct 4, 2024
@Seelengrab
Copy link
Owner

Thank you for the bug report! This is very weird; I'm not sure what's going on, so as a workaround, your best option is to make sure variables in your function don't clash with the function name.

@Seelengrab
Copy link
Owner

Seelengrab commented Oct 5, 2024

Ok wow, this might be related to the weird scoping behavior of closures/let blocks. Placing the @check in an @testset makes the function being generated under the hood into a closure, which ends up capturing itself (?!), which in turn means that binding foo = x^2 inside of that foo function causes the foo referenced in another internal function to be bound to that same number! This is really wild:

julia> @testset "Supposition" begin
           @check function foo(x = Data.Integers{Int64}())
               foo = x^2
               foo >= 0
           end
       end;
input = (x = -9223372036854775808,)
foo = var"#foo#foo##1"(Core.Box(var"#foo#foo##1"(#= circular reference @-2 =#)))
input = (x = -6993590808634186443,)
foo = 0
input = (x = -9223372036854775808,)
foo = 0

(with some internal modifications to get the printing here to show up.)

I don't even know how to tackle that on my end, because it looks like this is a mixup in lowering 🤔

@Seelengrab
Copy link
Owner

Seelengrab commented Oct 5, 2024

Reduced to this MWE, which I'm going to report upstream:

julia> let; 
           function a()
               b()
           end
           function b()
               b = 1
               return b
           end
           @show a()
           @show a()
       end
a() = 1
ERROR: MethodError: objects of type Int64 are not callable
The object of type `Int64` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
Maybe you forgot to use an operator such as *, ^, %, / etc. ?
Stacktrace:
 [1] (::var"#a#a##4")()
   @ Main ./REPL[6]:3
 [2] top-level scope
   @ REPL[6]:10
 [3] macro expansion
   @ show.jl:1229 [inlined]

IMO, the rebinding in b should only shadow the function name, not cause rebinding in a as well. The workaround mentioned above (don't shadow the function name) is still the best thing you can do at the moment, I'm afraid.

@arnaud-ma
Copy link
Author

Yes of course, the workaround is very simple and it only impacts people like me who are bad at naming things 😅.

First time writing a Julia related issue and surprised that the rumor of a very responsive community is 100% true!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants