Repository: JuliaLang/julia
File: JuliaLowering/src/syntax_macros.jl (Julia)

Code before:
        # Desugaring uses this helper internally, so we may see K"kw" too.
        if k == K"=" && numchildren(ex) === 2
            ex = @ast ctx ex [K"kw" ex[1] ex[2]]
        end
        mapchildren(c->_apply_nospecialize(ctx, c), ctx, ex, 1:1)
    else
        throw(LoweringError(ex, "Invalid function argument"))
    end
end

function Base.var"@nospecialize"(__context__::MacroContext)
    @ast __context__ __context__.macrocall [K"meta" "nospecialize"::K"Symbol"]
end

function Base.var"@nospecialize"(__context__::MacroContext, ex::SyntaxTree)
    _apply_nospecialize(__context__, ex)
end

function Base.var"@nospecialize"(
        __context__::MacroContext,
        ex1::SyntaxTree, ex2::SyntaxTree, exs::SyntaxTree...
    )
    to_nospecialize = SyntaxTree[ex1, ex2, exs...]
    @ast __context__ __context__.macrocall [K"block"
        map(st->_apply_nospecialize(__context__, st), to_nospecialize)...]
end

# TODO: support all forms that the original supports
# function Base.var"@atomic"(__context__::MacroContext, ex)
#     @chk kind(ex) == K"Identifier" || kind(ex) == K"::" (ex, "Expected identifier or declaration")
#     @ast __context__ __context__.macrocall [K"atomic" ex]
# end

function Base.var"@label"(__context__::MacroContext, ex)
    @chk kind(ex) == K"Identifier"
    @ast __context__ ex [K"symboliclabel" ex]
end

function Base.var"@label"(__context__::MacroContext, name, body)
    # Handle `@label _ body` (anonymous) or `@label name body` (named)
    k = kind(name)
    if k == K"Identifier"
        # `@label _ body` or `@label name body` - plain identifier
    elseif is_contextual_keyword(k)
        # Contextual keyword used as label name (e.g., `@label outer body`)
    else
        throw(MacroExpansionError(name, "Expected identifier for block label"))
    end
    # If body is a syntactic loop, wrap its body in a continue block
    # This allows `continue name` to work by breaking to `name#cont`
    body_kind = kind(body)

Review comment (suggestion):
```suggestion
function Base.var"@nospecialize"(__context__::MacroContext, exs::SyntaxTree...)
    if length(exs) == 0
        @ast __context__ __context__.macrocall [K"meta" "nospecialize"::K"Symbol"]
    elseif length(exs) == 1
        _apply_nospecialize(__context__, only(exs))
    else
        @ast __context__ __context__.macrocall [K"block"
            map(ex->_apply_nospecialize(__context__, ex), exs)...
        ]
     end
end
```

just a suggestion - the dispatch-based logic is somewhat harder to read imo

Code after:
        # Desugaring uses this helper internally, so we may see K"kw" too.
        if k == K"=" && numchildren(ex) === 2
            ex = @ast ctx ex [K"kw" ex[1] ex[2]]
        end
        mapchildren(c->_apply_nospecialize(ctx, c), ctx, ex, 1:1)
    else
        throw(LoweringError(ex, "Invalid function argument"))
    end
end

function Base.var"@nospecialize"(__context__::MacroContext, exs::SyntaxTree...)
    if length(exs) == 0
        @ast __context__ __context__.macrocall [K"meta" "nospecialize"::K"Symbol"]
    elseif length(exs) == 1
        _apply_nospecialize(__context__, only(exs))
    else
        @ast __context__ __context__.macrocall [K"block"
            map(ex->_apply_nospecialize(__context__, ex), exs)...
        ]
     end
end

# TODO: support all forms that the original supports
# function Base.var"@atomic"(__context__::MacroContext, ex)
#     @chk kind(ex) == K"Identifier" || kind(ex) == K"::" (ex, "Expected identifier or declaration")
#     @ast __context__ __context__.macrocall [K"atomic" ex]
# end

function Base.var"@label"(__context__::MacroContext, ex)
    @chk kind(ex) == K"Identifier"
    @ast __context__ ex [K"symboliclabel" ex]
end

function Base.var"@label"(__context__::MacroContext, name, body)
    # Handle `@label _ body` (anonymous) or `@label name body` (named)
    k = kind(name)
    if k == K"Identifier"
        # `@label _ body` or `@label name body` - plain identifier
    elseif is_contextual_keyword(k)
        # Contextual keyword used as label name (e.g., `@label outer body`)
    else
        throw(MacroExpansionError(name, "Expected identifier for block label"))
    end
    # If body is a syntactic loop, wrap its body in a continue block
    # This allows `continue name` to work by breaking to `name#cont`
    body_kind = kind(body)
    if body_kind == K"for" || body_kind == K"while"
        cont_name = mkleaf(name) # use name's scope and attrs
        setattr!(name, :kind, K"Identifier")
        setattr!(name, :name_val, string(name.name_val, "#cont"))
        loop_body = body[2]