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

Callsite @inline doesn't like val argument #48910

Open
maleadt opened this issue Mar 6, 2023 · 8 comments
Open

Callsite @inline doesn't like val argument #48910

maleadt opened this issue Mar 6, 2023 · 8 comments
Labels
bug Indicates an unexpected problem or unintended behavior macros @macros

Comments

@maleadt
Copy link
Member

maleadt commented Mar 6, 2023

MWE:

macro foo()
    quote
        function bar(val)
            @inline identity(nothing)
        end
    end
end
@foo
ERROR: LoadError: syntax: local variable name "#9#val" conflicts with an argument

Using any other variable name than val makes this work. Probably suspect:

julia/base/expr.jl

Lines 846 to 859 in 7eb9615

function annotate_meta_def_or_block(@nospecialize(ex), meta::Symbol)
inner = unwrap_macrocalls(ex)
if is_function_def(inner)
# annotation on a definition
return esc(pushmeta!(ex, meta))
else
# annotation on a block
return Expr(:block,
Expr(meta, true),
Expr(:local, Expr(:(=), :val, esc(ex))),
Expr(meta, false),
:val)
end
end

@maleadt maleadt added bug Indicates an unexpected problem or unintended behavior macros @macros labels Mar 6, 2023
@TakshDhabalia
Copy link
Contributor

anyone working on this issue ? would like to talk about this and make changes
Thanks!

@maleadt
Copy link
Member Author

maleadt commented Mar 10, 2023

The issue isn't assigned, so I don't think anybody is working on this.

@simeonschaub
Copy link
Member

This is probably not a great first issue though, since the source is likely a macro hygiene bug

@xlxs4
Copy link
Contributor

xlxs4 commented Mar 21, 2023

For completeness' sake annotate_meta_def_or_block is also called when @noinline is used, so this error is present if you substitute @inline with @noinline. The problem has to do with annotate_meta_def_or_block itself.
The expanded macro will be

quote
    function var"#<x gensym>#<function name>"(var"#<y gensym>#<argument name>")
        begin
            $(Expr(:<meta>, true))
            local var"#<z gensym>#val" = <escaped expression>
            $(Expr(:<meta>, false))
            var"#<z gensym>#val"
        end
    end
end

The gensym for the argument in the case of val is the same inside the begin block

@xlxs4
Copy link
Contributor

xlxs4 commented Mar 27, 2023

Same issue for @inbounds, defined as:

macro inbounds(blk)
    return Expr(:block,
        Expr(:inbounds, true),
        Expr(:local, Expr(:(=), :val, esc(blk))),
        Expr(:inbounds, :pop),
        :val)
end

@xlxs4
Copy link
Contributor

xlxs4 commented Mar 27, 2023

I wonder, why use true/false for @(no)inline, but true/:pop for @inbounds?

@xlxs4
Copy link
Contributor

xlxs4 commented Mar 27, 2023

I think this is unrelated to hygiene, and is intended behavior, as it has to do with what gets escaped and what doesn't. If that's the case, we can leave everything as-is, or prefix val with an underscore? In case we'd do the latter, we should remember to also prefix the respective variable in @noinline and @inbounds.

@Pangoraw
Copy link
Contributor

Pangoraw commented Apr 4, 2023

Probably related to #43151. @foo and @inline share the same gensym for val.

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 macros @macros
Projects
None yet
Development

No branches or pull requests

5 participants