Repository: JuliaLang/julia
File: test/misc.jl (Julia)

Code before:
    """
    cmd = if Sys.isunix()
        # Set the maximum core dump size to 0 to keep this expected crash from
        # producing a (and potentially overwriting an existing) core dump file
        `sh -c "ulimit -c 0; $(Base.shell_escape(Base.julia_cmd())) -e $(Base.shell_escape(script))"`
    else
        `$(Base.julia_cmd()) -e $script`
    end
    p = run(ignorestatus(cmd), devnull, stdout, devnull)
    if p.termsignal == 0
        Sys.isunix() ? @test(p.exitcode ∈ (128+7, 128+10, 128+11)) : @test(p.exitcode != 0) # expect SIGBUS (7 on BSDs or 10 on Linux) or SIGSEGV (11)
    else
        @test(p.termsignal ∈ (7, 10, 11))
    end
end

# issue #41656
run(`$(Base.julia_cmd()) -e 'isempty(x) = true'`)

function treshape59278(X::AbstractArray, n, m)
    Y = reshape(X, n, m)
    Y .= 1.0
    return X
end

# a funcion that allocates iff no constprop
@inline maybealloc59278(n, _) = ntuple(i->rand(), n)

@testset "Base/timing.jl" begin
    @test Base.jit_total_bytes() >= 0

    # sanity check `@allocations` returns what we expect in some very simple cases.
    @test (() -> @allocations "a")() == 0
    @test (() -> @allocations "a" * Base.inferencebarrier("b"))() == 1
    # test that you can grab the value from @allocated
    @allocated _x = 1+2
    @test _x === 3

    n, m = 10, 20
    X = rand(n, m)
    treshape59278(X, n, m)
    # test that @allocated and @allocations are consistent about whether anything was
    # allocated in a case where the compiler can sometimes remove an allocation
    # https://github.com/JuliaLang/julia/issues/58634#issuecomment-2940840651
    @test ((@allocated treshape59278(X, n, m))==0) == ((@allocations treshape59278(X, n, m))==0)
    @test ((@allocated begin treshape59278(X, n, m) end)==0) == ((@allocations begin treshape59278(X, n, m) end)==0)

    # test that all wrapped allocations are counted and constprop is not done, regardless
    # of expression structure
    @test (@allocated maybealloc59278(10, [])) > (@allocated maybealloc59278(10, 0)) > 0
    # but if you wrap it in another function it can be constprop'd

Review comment (suggestion):
```suggestion
# a function that allocates iff no constprop
```

Code after:
    """
    cmd = if Sys.isunix()
        # Set the maximum core dump size to 0 to keep this expected crash from
        # producing a (and potentially overwriting an existing) core dump file
        `sh -c "ulimit -c 0; $(Base.shell_escape(Base.julia_cmd())) -e $(Base.shell_escape(script))"`
    else
        `$(Base.julia_cmd()) -e $script`
    end
    p = run(ignorestatus(cmd), devnull, stdout, devnull)
    if p.termsignal == 0
        Sys.isunix() ? @test(p.exitcode ∈ (128+7, 128+10, 128+11)) : @test(p.exitcode != 0) # expect SIGBUS (7 on BSDs or 10 on Linux) or SIGSEGV (11)
    else
        @test(p.termsignal ∈ (7, 10, 11))
    end
end

# issue #41656
run(`$(Base.julia_cmd()) -e 'isempty(x) = true'`)

function treshape59278(X::AbstractArray, n, m)
    Y = reshape(X, n, m)
    Y .= 1.0
    return X
end

# a function that allocates iff no constprop
@inline maybealloc59278(n, _) = ntuple(i->rand(), n)

@testset "Base/timing.jl" begin
    @test Base.jit_total_bytes() >= 0

    # sanity check `@allocations` returns what we expect in some very simple cases.
    @test (() -> @allocations "a")() == 0
    "a" * Base.inferencebarrier("b")
    @test (() -> @allocations "a" * Base.inferencebarrier("b"))() == 1
    # test that you can grab the value from @allocated
    @allocated _x = 1+2
    @test _x === 3

    n, m = 10, 20
    X = rand(n, m)
    treshape59278(X, n, m)
    # test that @allocated and @allocations are consistent about whether anything was
    # allocated in a case where the compiler can sometimes remove an allocation
    # https://github.com/JuliaLang/julia/issues/58634#issuecomment-2940840651
    @test ((@allocated treshape59278(X, n, m))==0) == ((@allocations treshape59278(X, n, m))==0)
    # TODO: would be nice to have but not yet reliable
    #@test ((@allocated begin treshape59278(X, n, m) end)==0) == ((@allocations begin treshape59278(X, n, m) end)==0)

    # test that all wrapped allocations are counted and constprop is not done
    @test (@allocated @noinline maybealloc59278(10, [])) > (@allocated maybealloc59278(10, 0)) > 0