Skip to content

Commit 5554676

Browse files
authored
Fix codegen error path for imported non-owned bindings (JuliaLang#45351)
The code assumed that a null return from `global_binding_pointer` was impossible. However, it happens in the error path, causing a bad memory reference later when the lhs is treated as a slot. Fixes JuliaLang#45350
1 parent a37dd16 commit 5554676

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/codegen.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4608,27 +4608,27 @@ static void emit_assignment(jl_codectx_t &ctx, jl_value_t *l, jl_value_t *r, ssi
46084608
assert(!jl_is_ssavalue(l));
46094609
jl_cgval_t rval_info = emit_expr(ctx, r, ssaval);
46104610

4611+
if (jl_is_slot(l)) {
4612+
int sl = jl_slot_number(l) - 1;
4613+
// it's a local variable
4614+
jl_varinfo_t &vi = ctx.slots[sl];
4615+
return emit_varinfo_assign(ctx, vi, rval_info, l);
4616+
}
4617+
46114618
jl_binding_t *bnd = NULL;
46124619
Value *bp = NULL;
46134620
if (jl_is_symbol(l))
4614-
bp = global_binding_pointer(ctx, ctx.module, (jl_sym_t*)l, &bnd, true); // now bp != NULL or bnd != NULL
4615-
else if (jl_is_globalref(l))
4616-
bp = global_binding_pointer(ctx, jl_globalref_mod(l), jl_globalref_name(l), &bnd, true); // now bp != NULL or bnd != NULL
4617-
else
4618-
assert(jl_is_slot(l));
4619-
if (bp != NULL || bnd != NULL) { // it is a global
4620-
if (bp != NULL) {
4621-
emit_globalset(ctx, bnd, bp, rval_info, AtomicOrdering::Unordered);
4622-
// Global variable. Does not need debug info because the debugger knows about
4623-
// its memory location.
4624-
}
4625-
return;
4621+
bp = global_binding_pointer(ctx, ctx.module, (jl_sym_t*)l, &bnd, true);
4622+
else {
4623+
assert(jl_is_globalref(l));
4624+
bp = global_binding_pointer(ctx, jl_globalref_mod(l), jl_globalref_name(l), &bnd, true);
46264625
}
4627-
4628-
int sl = jl_slot_number(l) - 1;
4629-
// it's a local variable
4630-
jl_varinfo_t &vi = ctx.slots[sl];
4631-
emit_varinfo_assign(ctx, vi, rval_info, l);
4626+
if (bp != NULL) {
4627+
emit_globalset(ctx, bnd, bp, rval_info, AtomicOrdering::Unordered);
4628+
// Global variable. Does not need debug info because the debugger knows about
4629+
// its memory location.
4630+
}
4631+
return;
46324632
}
46334633

46344634
static void emit_upsilonnode(jl_codectx_t &ctx, ssize_t phic, jl_value_t *val)

test/core.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7800,3 +7800,11 @@ end
78007800
m.x = 4.
78017801
@test m.x === 4
78027802
end
7803+
7804+
# #45350 - Codegen for assignment to binding imported from module
7805+
module Foo45350
7806+
global x45350::Int = 1
7807+
end
7808+
import .Foo45350: x45350
7809+
f45350() = (global x45350 = 2)
7810+
@test_throws ErrorException f45350()

0 commit comments

Comments
 (0)