Skip to content

Commit

Permalink
don't override line info when code generation gives it explicitly (Ju…
Browse files Browse the repository at this point in the history
…liaLang#47750)

* respect given line info when code generation produces `CodeInfo`

This should improve a readability of stacktrace for code generated by
Cassette-like mechanism that produces `CodeInfo` from `@generated`
function.

* add test
  • Loading branch information
aviatesk committed Dec 6, 2022
1 parent a8b3994 commit c3bf180
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,10 @@ void jl_add_function_name_to_lineinfo(jl_code_info_t *ci, jl_value_t *name)
jl_value_t *file = jl_fieldref_noalloc(ln, 2);
lno = jl_fieldref(ln, 3);
inl = jl_fieldref(ln, 4);
jl_value_t *ln_name = (jl_is_int32(inl) && jl_unbox_int32(inl) == 0) ? name : jl_fieldref_noalloc(ln, 1);
// respect a given linetable if available
jl_value_t *ln_name = jl_fieldref_noalloc(ln, 1);
if (jl_is_symbol(ln_name) && (jl_sym_t*)ln_name == jl_symbol("none") && jl_is_int32(inl) && jl_unbox_int32(inl) == 0)
ln_name = name;
rt = jl_new_struct(jl_lineinfonode_type, mod, ln_name, file, lno, inl);
jl_array_ptr_set(li, i, rt);
}
Expand Down Expand Up @@ -587,7 +590,6 @@ JL_DLLEXPORT jl_code_info_t *jl_code_for_staged(jl_method_instance_t *linfo)
else {
// Lower the user's expression and resolve references to the type parameters
func = jl_expand_and_resolve(ex, def->module, linfo->sparam_vals);

if (!jl_is_code_info(func)) {
if (jl_is_expr(func) && ((jl_expr_t*)func)->head == jl_error_sym) {
ct->ptls->in_pure_callback = 0;
Expand Down
12 changes: 12 additions & 0 deletions test/staged.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,15 @@ let tup = g_vararg_generated()
# shifts everything over by 1)
@test only(code_lowered(only(methods(f_vararg_generated)).generator.gen)).slotflags[5] == UInt8(0x00)
end

# respect a given linetable in code generation
# https://github.com/JuliaLang/julia/pull/47750
let match = Base._which(Tuple{typeof(sin),Int})
mi = Core.Compiler.specialize_method(match)
lwr = Core.Compiler.retrieve_code_info(mi)
@test all(lin->lin.method===:sin, lwr.linetable)
@generated sin_generated(a) = lwr
src = only(code_lowered(sin_generated, (Int,)))
@test all(lin->lin.method===:sin, src.linetable)
@test sin_generated(42) == sin(42)
end

0 comments on commit c3bf180

Please sign in to comment.