JuliaServices / Match.jl

Advanced Pattern Matching for Julia
https://juliaservices.github.io/Match.jl/latest/
Other
240 stars 22 forks source link

Duplicate goto labels generated when using `@match_return` #102

Closed nystrom closed 4 days ago

nystrom commented 5 months ago

The following generates duplicate goto labels:

julia> struct Foo x; y end

julia> foo(x) = x == 9
foo (generic function with 1 method)

julia> @match Foo(1,2) begin
               Foo(_, _) where (foo(1) && foo(2)) => 15
               Foo(_, _) where foo(7) =>
                   begin
                       if foo(9)
                           @match_return 16
                       end
                       17
                   end
               Foo(_, _) where (foo(1) && foo(3)) => 18
               _ => 43
        end
ERROR: syntax: label "##label#236" defined multiple times

The cause is duplicated code for where clauses in the automaton. See nodes 9 and 16 below:

Decision Automaton: (19 nodes) input «input_value»
Node 1 TEST «input_value» isa Foo ELSE: Node 19
Node 2 FETCH «where_0» := foo(1)
Node 3 TEST «where_0» ELSE: Node 14
Node 4 FETCH «where_1» := foo(2)
Node 5 TEST «where_1» ELSE: Node 7
Node 6 MATCH 1 with value 15
Node 7 FETCH «where_2» := foo(7)
Node 8 TEST «where_2» ELSE: Node 11
Node 9 FETCH «where_3» := begin
    var"«value»" = begin
            if foo(9)
                var"«value»" = 16
                $(Expr(:symbolicgoto, Symbol("«label»")))
            end
            17
        end
    @label var"«label»"
    var"«value»" !== MatchFailure
end
Node 10 TEST «where_3» THEN: Node 18 ELSE: Node 11
Node 11 FETCH «where_4» := foo(3)
Node 12 TEST «where_4» ELSE: Node 19
Node 13 MATCH 3 with value 18
Node 14 FETCH «where_2» := foo(7)
Node 15 TEST «where_2» ELSE: Node 19
Node 16 FETCH «where_3» := begin
    var"«value»" = begin
            if foo(9)
                var"«value»" = 16
                $(Expr(:symbolicgoto, Symbol("«label»")))
            end
            17
        end
    @label var"«label»"
    var"«value»" !== MatchFailure
end
Node 17 TEST «where_3» ELSE: Node 19
Node 18 MATCH 2 with value «value»
Node 19 MATCH 4 with value 43
end # of automaton
gafter commented 3 months ago

@nystrom I'm sorry I didn't notice this issue until today. I'll have a look.