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

Inconsistency in 1-arg and 3-arg names in :method Exprs #38451

Closed
timholy opened this issue Nov 15, 2020 · 4 comments · Fixed by #38496
Closed

Inconsistency in 1-arg and 3-arg names in :method Exprs #38451

timholy opened this issue Nov 15, 2020 · 4 comments · Fixed by #38496
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@timholy
Copy link
Member

timholy commented Nov 15, 2020

This is a re-post of timholy/Revise.jl#568:

module MyModule

function my_fun end

macro some_macro(value)
    return esc(:($MyModule.my_fun() = $value))
end

end

And then:

julia> Base.Meta.lower(MyModule, :(@some_macro 20))
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.getproperty(Main.MyModule, :my_fun)
│   %2 = Core.Typeof(%1)
│   %3 = Core.svec(%2)
│   %4 = Core.svec()
│   %5 = Core.svec(%3, %4, $(QuoteNode(:(#= REPL[1]:6 =#))))
$(Expr(:method, nothing, :(%5), CodeInfo(
    @ REPL[1]:6 within `none'
1 ─     return 20
)))
└──      return nothing
))))

I was surprised by the nothing used for argument 1 of the 3-arg :method Expr; shouldn't it be :my_fun?

@timholy timholy added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Nov 15, 2020
@cstjean
Copy link
Contributor

cstjean commented Nov 15, 2020

Perhaps that's obvious from context, but FWIW this is on 1.6. 1.5 yields

%5 = Core.svec(%3, %4, $(QuoteNode(:(#= REPL[2]:8 =#))))
│        $(Expr(:method, :(Base.getproperty(Main.MyModule, :my_fun)), :(%5), CodeInfo(quote
    return 20
end)))

@Keno
Copy link
Member

Keno commented Nov 16, 2020

This was changed in #37287. The difference is whether or not the method can create an entirely new method or not (i.e. whether it's interpreted as an identifier or an expression).

@timholy
Copy link
Member Author

timholy commented Nov 16, 2020

To make sure I understand, you're saying this is how it should work and not a bug? If so, perhaps we should update https://docs.julialang.org/en/v1.6-dev/devdocs/ast/#Expr-types. I can do that, but I'd need to know a bit more about the conditions under which it uses nothing. I always assumed that was for anonymous functions.

@JeffBezanson
Copy link
Member

Yes, this is how it should work. As Keno said, the difference is that f(x) = x can start with f being undefined, and assign a value to it. But M.f(x) = x requires M.f to be defined already, evaluates that expression, and uses it to determine the type to add the method to, so there is no assigned name.

KristofferC pushed a commit that referenced this issue Dec 27, 2020
staticfloat pushed a commit that referenced this issue Jan 15, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this issue May 4, 2021
staticfloat pushed a commit that referenced this issue Dec 23, 2022
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)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants