Skip to content

Commit

Permalink
Merge pull request #17498 from bjarthur/static
Browse files Browse the repository at this point in the history
@static && || syntax
  • Loading branch information
vtjnash authored Dec 22, 2016
2 parents 0d84051 + a55988c commit d47f24b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
9 changes: 6 additions & 3 deletions base/osutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ Partially evaluates an expression at parse time.
For example, `@static is_windows() ? foo : bar` will evaluate `is_windows()` and insert either `foo` or `bar` into the expression.
This is useful in cases where a construct would be invalid on other platforms,
such as a `ccall` to a non-existent function.
`@static if is_apple() foo end` and `@static foo <&&,||> bar` are also valid syntax.
"""
macro static(ex)
if isa(ex, Expr)
if ex.head === :if
if ex.head === :if || ex.head === :&& || ex.head === :||
cond = eval(current_module(), ex.args[1])
if cond
if xor(cond, ex.head === :||)
return esc(ex.args[2])
elseif length(ex.args) == 3
return esc(ex.args[3])
else
elseif ex.head === :if
return nothing
else
return cond
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function choosetests(choices = [])
"markdown", "base64", "serialize", "misc", "threads",
"enums", "cmdlineargs", "i18n", "workspace", "libdl", "int",
"checked", "intset", "floatfuncs", "compile", "parallel", "inline",
"boundscheck", "error", "ambiguous", "cartesian", "asmvariant",
"boundscheck", "error", "ambiguous", "cartesian", "asmvariant", "osutils",
"channels"
]
profile_skipped = false
Expand Down
18 changes: 0 additions & 18 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,6 @@ end
@test isa(ex, ErrorException) && ex.msg == "cannot assign variables in other modules"
end

@test !Base.is_unix(:Windows)
@test !Base.is_linux(:Windows)
@test Base.is_linux(:Linux)
@test Base.is_windows(:Windows)
@test Base.is_windows(:NT)
@test !Base.is_windows(:Darwin)
@test Base.is_apple(:Darwin)
@test Base.is_apple(:Apple)
@test !Base.is_apple(:Windows)
@test Base.is_unix(:Darwin)
@test Base.is_unix(:FreeBSD)
@test_throws ArgumentError Base.is_unix(:BeOS)
if !is_windows()
@test Sys.windows_version() === (0, 0)
else
@test (Sys.windows_version()::Tuple{Int,Int})[1] > 0
end

# Issue 14173
module Tmp14173
export A
Expand Down
26 changes: 26 additions & 0 deletions test/osutils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@test !Base.is_unix(:Windows)
@test !Base.is_linux(:Windows)
@test Base.is_linux(:Linux)
@test Base.is_windows(:Windows)
@test Base.is_windows(:NT)
@test !Base.is_windows(:Darwin)
@test Base.is_apple(:Darwin)
@test Base.is_apple(:Apple)
@test !Base.is_apple(:Windows)
@test Base.is_unix(:Darwin)
@test Base.is_unix(:FreeBSD)
@test_throws ArgumentError Base.is_unix(:BeOS)
if !is_windows()
@test Sys.windows_version() === (0, 0)
else
@test (Sys.windows_version()::Tuple{Int,Int})[1] > 0
end

@test (@static true ? 1 : 2) === 1
@test (@static false ? 1 : 2) === 2
@test (@static if true 1 end) === 1
@test (@static if false 1 end) === nothing
@test (@static true && 1) === 1
@test (@static false && 1) === false
@test (@static true || 1) === true
@test (@static false || 1) === 1

0 comments on commit d47f24b

Please sign in to comment.