|
| 1 | +using MacroTools: flatten, striplines |
| 2 | + |
| 3 | + |
| 4 | +@testset "flatten try" begin # see julia#50710 and MacroTools#194 |
| 5 | + # These tests were prompted due to the following two issues: |
| 6 | + # 1. on all Julia versions `flatten(striplines(:(try catch; false finally false end)))` |
| 7 | + # was completely breaking the try block due to turning `begin false end` into `false` |
| 8 | + # which have drastically different interpretations as 3rd or 4th argument of a try block. |
| 9 | + # 2. only on Julia 1.10, due to the new parser generating slightly more annotations (line number nodes) |
| 10 | + # `flatten(:(try f() catch end))` was turning into `Expr(:try, call, false, linenumbernode)` |
| 11 | + # instead of `Expr(:try, call, false, empty_block)`. Downstream consumers of the AST |
| 12 | + # were not expecting the line number node and were breaking, which is how this issue was |
| 13 | + # discovered. |
| 14 | + # The two issues have the same underlying cause: `begin expr end` was being turned into `expr` |
| 15 | + # which is valid everywhere in julia except in try blocks. |
| 16 | + # Notice that issue 1 is triggered only if one uses `striplines`. As such it was not really |
| 17 | + # triggered in the wild. However, issue 2 was seen with the slight modification to |
| 18 | + # parser annotations in Julia 1.10 which led to the discovery of issue 1. |
| 19 | + exs = [ |
| 20 | + quote try; f(); catch; end; end, |
| 21 | + quote try; f(); catch; else; finally; end; end, |
| 22 | + quote try; f(); catch E; else; finally; end; end, |
| 23 | + quote try; f(); catch; finally; end; end, |
| 24 | + quote try; f(); catch E; finally; end; end, |
| 25 | + quote try; f(); catch E; 3+3; finally; 4+4; end; end, |
| 26 | + quote try; f(); catch E; 3+3; else; 2+2; finally; 4+4; end; end, |
| 27 | + quote try; f(); finally; end; end, |
| 28 | + quote try; f(); catch; false; finally; end; end, |
| 29 | + quote try; f(); catch; else; finally; false; end; end, |
| 30 | + quote try; f(); catch; else; end; end, |
| 31 | + quote try; f(); catch; 3+3; else; 2+2; end; end, |
| 32 | + quote try; f(); catch E; else; end; end, |
| 33 | + quote try; f(); catch E; 3+3; else; 2+2; end; end |
| 34 | + ] |
| 35 | + for ex in exs |
| 36 | + #@show ex |
| 37 | + @test flatten(ex) |> striplines == ex |> striplines |
| 38 | + @test flatten(striplines(ex)) == striplines(ex).args[1] |
| 39 | + end |
| 40 | + @test 123 == eval(flatten(striplines(:(try error() catch; 123 finally end)))) |
| 41 | + @test 123 == eval(flatten(:(try error() catch; 123 finally end))) |
| 42 | + @test 234 == eval(flatten(striplines(:(try 1+1 catch; false; else 234; finally end)))) |
| 43 | + @test 234 == eval(flatten(:(try 1+1 catch; false; else 234; finally end))) |
| 44 | + for (exa, exb) in [ |
| 45 | + (quote try; begin f(); g(); end; catch; end; end, quote try; f(); g(); catch; end; end), |
| 46 | + (quote try; catch; begin f(); g(); end; end; end, quote try; catch; f(); g(); end; end), |
| 47 | + (quote try; begin f(); g(); end; catch; finally; begin m(); n(); end; end; end, quote try; f(); g(); catch; finally; m(); n(); end; end) |
| 48 | + ] |
| 49 | + @test exa |> flatten |> striplines == exb |> striplines |
| 50 | + @test exa |> striplines |> flatten == (exb |> striplines).args[1] |
| 51 | + end |
| 52 | + # unnatural expressions that can not be generated by the Julia parser, but still get accepted and we do not want to break |
| 53 | + for ex in [ |
| 54 | + Expr(:try, 1, false, 2) |
| 55 | + Expr(:try, 1, false, false, false) |
| 56 | + ] |
| 57 | + @test flatten(ex)==ex |
| 58 | + end |
| 59 | +end |
0 commit comments