diff --git a/base/essentials.jl b/base/essentials.jl index cf7505bddbb59..297be0e286369 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -499,8 +499,9 @@ end macro inbounds(blk) return Expr(:block, Expr(:inbounds, true), - esc(blk), - Expr(:inbounds, :pop)) + Expr(:local, Expr(:(=), :val, esc(blk))), + Expr(:inbounds, :pop), + :val) end """ diff --git a/test/arrayops.jl b/test/arrayops.jl index 6db4148f13694..cbaddd1cf439e 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2008,6 +2008,18 @@ end @test typeof(.~A) == Vector{Int} end +# @inbounds is expression-like, returning its value; #15558 +@testset "expression-like inbounds" begin + local 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 + g(A, i) = (i == 0 ? (@inbounds return 0) : (@inbounds A[i])) + @test g(A, 0) == 0 + @test g(A, 1) == 1 +end + @testset "issue #16247" begin local A = zeros(3,3) @test size(A[:,0x1:0x2]) == (3, 2)