tshort / StaticCompiler.jl

Compiles Julia code to a standalone library (experimental)
Other
499 stars 30 forks source link

Rebase of the error handling PR. Have fun! #97

Closed gbaraldi closed 1 year ago

gbaraldi commented 1 year ago

Some errors are still not good enough, and it needs some tests.

brenhinkeller commented 1 year ago

OMG, all tests have passed! If no objections I'll merge tonight!

brenhinkeller commented 1 year ago

(I can try to add some tests for the new parts but if this fixes the breakage on nightly then that's already awesome)

tshort commented 1 year ago

Can the user use @device_override to add to the method table, or is that only supported internally within StaticCompiler?

gbaraldi commented 1 year ago

I haven't tried it out, and it's not currently exported, but I don't see why not.

brenhinkeller commented 1 year ago

Can confirm!

julia> using StaticCompiler, StaticTools

julia> oops() = Int64(0.1)
oops (generic function with 1 method)

julia> compile_executable(oops, (), "./")
ERROR: oops() did not infer to a concrete type. Got Union{}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] compile_executable(f::Function, types::Tuple{}, path::String, name::String; filename::String, cflags::Cmd, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ StaticCompiler ~/.julia/packages/StaticCompiler/7kg6k/src/StaticCompiler.jl:242
 [3] compile_executable
   @ ~/.julia/packages/StaticCompiler/7kg6k/src/StaticCompiler.jl:232 [inlined]
 [4] compile_executable(f::Function, types::Tuple{}, path::String)
   @ StaticCompiler ~/.julia/packages/StaticCompiler/7kg6k/src/StaticCompiler.jl:232
 [5] top-level scope
   @ REPL[3]:1

julia> StaticCompiler.@device_override @noinline Base.Int64(::Float64) =
           StaticCompiler.@print_and_throw c"Surprise!"

julia> oops() = Int64(0.1) # this redefinition is apparently necessary, but only because of our previous failed compilation attempt
oops (generic function with 1 method)

julia> compile_executable(oops, (), "./")
"/Users/cbkeller/oops"

shell> ./oops
Surprise!
brenhinkeller commented 1 year ago

Also perhaps worth noting that AFAICT one can override any method, not just errors (which could also be handy! I was going to override the error here instead until it turned out that it was tricky to find the right method,)