Skip to content

Commit

Permalink
implement setfield! case correctly of PR to optimize field access whe…
Browse files Browse the repository at this point in the history
…n field types don't depend on parameters

fixes the bug in PR #21620 correctly
  • Loading branch information
vtjnash committed Jun 9, 2017
1 parent f541d29 commit 6ab0f0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3050,10 +3050,10 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
}

else if (f==jl_builtin_setfield && nargs==3) {
jl_datatype_t *sty = (jl_datatype_t*)expr_type(args[1], ctx);
rt1 = (jl_value_t*)sty;
jl_datatype_t *uty = (jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)sty);
if (jl_is_structtype(uty) && uty != jl_module_type && ((jl_datatype_t*)uty)->layout) {
jl_value_t *sty = expr_type(args[1], ctx);
rt1 = sty;
jl_datatype_t *uty = (jl_datatype_t*)jl_unwrap_unionall(sty);
if (jl_is_structtype(uty) && uty != jl_module_type && uty->layout) {
size_t idx = (size_t)-1;
if (jl_is_quotenode(args[2]) && jl_is_symbol(jl_fieldref(args[2],0))) {
idx = jl_field_index(uty, (jl_sym_t*)jl_fieldref(args[2],0), 0);
Expand All @@ -3075,7 +3075,7 @@ static bool emit_builtin_call(jl_cgval_t *ret, jl_value_t *f, jl_value_t **args,
// TODO: attempt better codegen for approximate types
jl_cgval_t strct = emit_expr(args[1], ctx); // emit lhs
*ret = emit_expr(args[3], ctx);
emit_setfield(sty, strct, idx, *ret, ctx, true, true);
emit_setfield(uty, strct, idx, *ret, ctx, true, true);
JL_GC_POP();
return true;
}
Expand Down
14 changes: 14 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4973,6 +4973,20 @@ let
end
@test f22122(1) === Int

# issue #22256
mutable struct Bar22256{AParameter}
inner::Int
end
mutable struct Foo22256
bar::Bar22256
end
setbar22256_inner(a) = (a.bar.inner = 3; nothing)
let a_foo = Foo22256(Bar22256{true}(2))
@test a_foo.bar.inner == 2
setbar22256_inner(a_foo)
@test a_foo.bar.inner == 3
end

# issue #22026
module M22026

Expand Down

0 comments on commit 6ab0f0a

Please sign in to comment.