Skip to content

Commit

Permalink
Make inbounds macros expression-like
Browse files Browse the repository at this point in the history
With #11169 fixed, we can now make at-inbounds return its value.
  • Loading branch information
mbauman committed Mar 18, 2016
1 parent 2a3aada commit d4470f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ esc(e::ANY) = Expr(:escape, e)
macro boundscheck(blk)
# hack: use this syntax since it avoids introducing line numbers
:($(Expr(:boundscheck,true));
$(esc(blk));
$(Expr(:boundscheck,:pop)))
local val = $(esc(blk));
$(Expr(:boundscheck,:pop));
val)
end

macro inbounds(blk)
:($(Expr(:inbounds,true));
$(esc(blk));
$(Expr(:inbounds,:pop)))
local val = $(esc(blk));
$(Expr(:inbounds,:pop));
val)
end

macro label(name::Symbol)
Expand Down
8 changes: 8 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1472,3 +1472,11 @@ type OOB_Functor{T}; a::T; end
let f = OOB_Functor([1,2])
@test_throws BoundsError map(f, [1,2,3,4,5])
end

# @inbounds is expression-like, returning its value
let A = [1,2,3]
@test (@inbounds A[1]) == 1
f(A, i) = @inbounds i == 0 ? (return 0) : A[i]
@test f(A, 0) == 0
@test f(A, 1) == 1
end

0 comments on commit d4470f8

Please sign in to comment.