Skip to content

Commit 594f52d

Browse files
committed
Prewalk the syntax tree in at-static evaluation
Post-walking the syntax tree meant the `if-else` chains were evaluated in opposite order from how they are normally processed, and this has consequences for things like checks on the existence of symbols: ```julia @static if VERSION >= v"1.1" && (Sys.isfreebsd() || Sys.isdragonfly()) ... @static if Sys.freebsd() ... end end ``` Under normal evaluation rules, this is valid — we have previously checked that the version is great enough for the `Sys.isfreebsd()` and `Sys.isdragonfly()` functions to exist, so the inner case doesn't need to check the version again. With the post-walk evaluation order, though, the inner `if` is evaluated first, and this threw errors as we tried to actually run the conditional. By switching to a pre-walk order, the outer `if` will kill the block of code on pre-v1.1 versions of Julia, and the inner case will never be evaluated.
1 parent 3ccb9e3 commit 594f52d

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/mmap.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BitFlags
2-
using MacroTools: @capture, flatten, isexpr, postwalk
32

43
import Base: INVALID_OS_HANDLE
54
import Mmap
@@ -104,7 +103,7 @@ Base.cconvert(::Type{T}, mf::MmapFlags) where {T <: Integer} = T(mf)
104103
MADV_AUTOSYNC = 7
105104
MADV_NOCORE = 8
106105
MADV_CORE = 9
107-
@static if VERSION >= v"1.1" && Sys.isfreebsd()
106+
@static if Sys.isfreebsd()
108107
MADV_PROTECT = 10
109108
else
110109
MADV_INVAL = 10

src/staticexpand.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using MacroTools: flatten, isexpr, prewalk, postwalk
2+
13
"""
24
@staticexpand @a_macro begin
35
# some code...
@@ -16,7 +18,7 @@ macro staticexpand(expr::Expr)
1618
sentinel = gensym("sentinel")
1719
isexpr(expr, :macrocall) || error("Expected a macro to expand")
1820
# Evaluate the static statements
19-
expr′ = postwalk(expr) do ex
21+
expr′ = prewalk(expr) do ex
2022
# Search for @static macro
2123
isexpr(ex, :macrocall) || return ex
2224
ex = ex::Expr

0 commit comments

Comments
 (0)