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

Strange error when sum_type macro is used inside a begin block #67

Closed
Tortar opened this issue Jan 22, 2024 · 2 comments · Fixed by #69
Closed

Strange error when sum_type macro is used inside a begin block #67

Tortar opened this issue Jan 22, 2024 · 2 comments · Fixed by #69

Comments

@Tortar
Copy link
Contributor

Tortar commented Jan 22, 2024

For non-parametric sum types this works fine

julia> using SumTypes

julia> begin
           mutable struct A
               x::Int
           end
           @sum_type B begin
               C(a::A)
           end
       end

but when you try with a parametric one a strange error with no informative stacktrace appears:

julia> using SumTypes

julia> begin
           mutable struct A{X}
               x::X
           end
           @sum_type B{X} begin
               C{X}(a::A{X})
           end
       end
ERROR: syntax: SlotNumber objects should not occur in an AST
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1
 [2] top-level scope
   @ REPL[2]:5
@Tortar
Copy link
Contributor Author

Tortar commented Jan 22, 2024

This works fine instead

julia> using SumTypes

julia> begin
           mutable struct A{Y}
               x::Y
           end
           @sum_type B{X} begin
               C{X}(a::A{X})
           end
       end

so the workaround is simple, but I'm puzzled by what Julia gets confused

@MasonProtter
Copy link
Owner

MasonProtter commented Jan 22, 2024

I'm guessing this is because SumTypes emits an Expr(:toplevel, ...) object instead of an Expr(:block, ...) for the struct definition. There's some kinda scary edge cases with macros defining structs, but since the implementation of sumtypes has changed a lot, maybe we no longer need to use Expr(:toplevel) and can go back to Expr(:block).

Here's a MWE:

julia> macro foo()
           ex = :(struct Foo{X}; x::X end)
           esc(Expr(:toplevel, ex))
       end
@foo (macro with 1 method)

julia> begin
           mutable struct A{X}
               x::X
           end
           @foo
       end
ERROR: syntax: SlotNumber objects should not occur in an AST
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1
 [2] top-level scope
   @ REPL[7]:5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants