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

Code before:
          precompile(f, (Int, Int))
          end
          """)
    ji, ofile = Base.compilecache(Base.PkgId("NoBackEdges"))
    @eval using NoBackEdges
    invokelatest() do
        @test first(methods(NoBackEdges.f)).specializations.cache.max_world === typemax(UInt)
    end
end

# Test precompilation of generated functions that return opaque closures
# (with constprop marker set to false).
precompile_test_harness("Generated Opaque") do load_path
    write(joinpath(load_path, "GeneratedOpaque.jl"),
        """
        module GeneratedOpaque
        using Base.Experimental: @opaque
        using InteractiveUtils
        const_int_barrier() = Base.inferencebarrier(1)::typeof(1)
        const lno = LineNumberNode(1, :none)

        const ci = @code_lowered const_int_barrier()
        @generated function oc_re_generated_no_partial()
            Expr(:new_opaque_closure, Tuple{}, Any, Any, false,
                Expr(:opaque_closure_method, nothing, 0, false, lno, ci))
        end
        @assert oc_re_generated_no_partial()() === 1
        @generated function oc_re_generated_no_partial_macro()
            AT = nothing
            RT = nothing
            allow_partial = false # makes this legal to generate during pre-compile
            return Expr(:opaque_closure, AT, RT, RT, allow_partial, :(()->const_int_barrier()))
        end
        @assert oc_re_generated_no_partial_macro()() === 1
        end
        """)
    Base.compilecache(Base.PkgId("GeneratedOpaque"))
    @eval using GeneratedOpaque
    let oc = invokelatest(()->GeneratedOpaque.oc_re_generated_no_partial())
        @test oc.source.specializations.cache.max_world === typemax(UInt)
        @test oc() === 1
    end
end

precompile_test_harness("Issue #52063") do load_path
    fname = joinpath(load_path, "i_do_not_exist.jl")
    @test try
        include_dependency(fname); false
    catch e
        @test e isa SystemError
        @test e.prefix == "opening file or folder $(repr(fname))"

Review comment (suggestion):
```suggestion
            mi = first(Base.specializations(match.method))
```
to be certain that you're not *generating* that `mi` with this call.

Code after:
          precompile(f, (Int, Int))
          end
          """)
    ji, ofile = Base.compilecache(Base.PkgId("NoBackEdges"))
    @eval using NoBackEdges
    invokelatest() do
        @test first(methods(NoBackEdges.f)).specializations.cache.max_world === typemax(UInt)
    end
end

precompile_test_harness("Pre-compile Core methods") do load_path
    # Core methods should support pre-compilation as external CI's like anything else
    # https://github.com/JuliaLang/julia/issues/58497
    write(joinpath(load_path, "CorePrecompilation.jl"),
          """
          module CorePrecompilation
          struct Foo end
          precompile(Tuple{Type{Vector{Foo}}, UndefInitializer, Tuple{Int}})
          end
          """)
    ji, ofile = Base.compilecache(Base.PkgId("CorePrecompilation"))
    @eval using CorePrecompilation
    invokelatest() do
        let tt = Tuple{Type{Vector{CorePrecompilation.Foo}}, UndefInitializer, Tuple{Int}},
            match = first(Base._methods_by_ftype(tt, -1, Base.get_world_counter())),
            mi = Base.specialize_method(match)
            @test isdefined(mi, :cache)
            @test mi.cache.max_world === typemax(UInt)
            @test mi.cache.invoke != C_NULL
        end
    end
end

# Test precompilation of generated functions that return opaque closures
# (with constprop marker set to false).
precompile_test_harness("Generated Opaque") do load_path
    write(joinpath(load_path, "GeneratedOpaque.jl"),
        """
        module GeneratedOpaque
        using Base.Experimental: @opaque
        using InteractiveUtils
        const_int_barrier() = Base.inferencebarrier(1)::typeof(1)
        const lno = LineNumberNode(1, :none)

        const ci = @code_lowered const_int_barrier()
        @generated function oc_re_generated_no_partial()
            Expr(:new_opaque_closure, Tuple{}, Any, Any, false,
                Expr(:opaque_closure_method, nothing, 0, false, lno, ci))
        end
        @assert oc_re_generated_no_partial()() === 1
        @generated function oc_re_generated_no_partial_macro()