https://github.com/JuliaLang/julia
Revision e50778520b4ec7db350607188dc18bc6c0719b94 authored by Simon Etter on 08 February 2024, 22:30:27 UTC, committed by GitHub on 08 February 2024, 22:30:27 UTC
`@kwdef` currently does not handle escaped type names and default values
correctly. This makes it impossible to write a (correctly escaped) macro
that internally uses `@kwdef`:

```julia
julia> module M
       macro define_struct(name)
           quote
               @kwdef struct $(esc(name)) field::Int end
           end
       end
       end;

julia> @macroexpand M.@define_struct(Foo)
ERROR: Invalid usage of @kwdef
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] var"@kwdef"(__source__::LineNumberNode, __module__::Module, expr::Any)
   @ Base ./util.jl:603
 [3] #macroexpand#66
   @ Base ./expr.jl:122 [inlined]
 [4] top-level scope
   @ REPL[5]:1
```

This PR patches `@kwdef` so various types of escaping are handled
correctly. Specifically, all of the following now works (see new test
case
[here](https://github.com/ettersi/julia/blob/adf7e20378ae7d0c3cf24a2cc3c815e560913b78/test/misc.jl#L1290-L1332)):

```julia
module KwdefWithEsc
    const Int1 = Int
    const val1 = 42
    macro define_struct()
        quote
            @kwdef struct $(esc(:Struct))
                a
                b = val1
                c::Int1
                d::Int1 = val1


                $(esc(quote
                    e
                    f = val2
                    g::Int2
                    h::Int2 = val2
                end))


                $(esc(:(i = val2)))
                $(esc(:(j::Int2)))
                $(esc(:(k::Int2 = val2)))


                l::$(esc(:Int2))
                m::$(esc(:Int2)) = val1


                n = $(esc(:val2))
                o::Int1 = $(esc(:val2))


                $(esc(:p))
                $(esc(:q)) = val1
                $(esc(:s))::Int1
                $(esc(:t))::Int1 = val1
            end
        end
    end
end


module KwdefWithEsc_TestModule
    using ..KwdefWithEsc
    const Int2 = Int
    const val2 = 42
    KwdefWithEsc.@define_struct()
end
```

---------

Co-authored-by: inky <git@wo-class.cn>
1 parent aba8afc
History
Tip revision: e50778520b4ec7db350607188dc18bc6c0719b94 authored by Simon Etter on 08 February 2024, 22:30:27 UTC
Support escape expressions in @kwdef (#53230)
Tip revision: e507785
File Mode Size
.devcontainer
.github
base
cli
contrib
deps
doc
etc
src
stdlib
test
.buildkite-external-version -rw-r--r-- 5 bytes
.clang-format -rw-r--r-- 3.3 KB
.clangd -rw-r--r-- 114 bytes
.codecov.yml -rw-r--r-- 52 bytes
.git-blame-ignore-revs -rw-r--r-- 371 bytes
.gitattributes -rw-r--r-- 65 bytes
.gitignore -rw-r--r-- 571 bytes
.mailmap -rw-r--r-- 12.7 KB
CITATION.bib -rw-r--r-- 513 bytes
CITATION.cff -rw-r--r-- 940 bytes
CONTRIBUTING.md -rw-r--r-- 23.4 KB
HISTORY.md -rw-r--r-- 372.8 KB
LICENSE.md -rw-r--r-- 1.3 KB
Make.inc -rw-r--r-- 55.9 KB
Makefile -rw-r--r-- 30.2 KB
NEWS.md -rw-r--r-- 11.5 KB
README.md -rw-r--r-- 7.4 KB
THIRDPARTY.md -rw-r--r-- 3.9 KB
VERSION -rw-r--r-- 11 bytes
julia.spdx.json -rw-r--r-- 37.8 KB
pkgimage.mk -rw-r--r-- 6.9 KB
sysimage.mk -rw-r--r-- 4.2 KB
typos.toml -rw-r--r-- 78 bytes

README.md

back to top