Skip to content

Commit

Permalink
ensure binding type is set consistently for consts
Browse files Browse the repository at this point in the history
Previously, `get_binding_type` would usually return `Any` for constants.
I think always setting it to the type of the actual value is probably
useful.
  • Loading branch information
simeonschaub committed Feb 18, 2022
1 parent 1ad2396 commit 5db09a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,13 @@ JL_DLLEXPORT void jl_set_const(jl_module_t *m JL_ROOTING_ARGUMENT, jl_sym_t *var
uint8_t constp = 0;
// if (jl_atomic_cmpswap(&bp->constp, &constp, 1)) {
if (constp = bp->constp, bp->constp = 1, constp == 0) {
jl_value_t *old = NULL;
jl_value_t *old_ty = NULL, *old = NULL;
jl_atomic_cmpswap_relaxed(&bp->ty, &old_ty, jl_typeof(val));
if (jl_atomic_cmpswap(&bp->value, &old, val)) {
jl_gc_wb_binding(bp, val);
return;
}
}
jl_value_t *old_ty = NULL;
jl_atomic_cmpswap_relaxed(&bp->ty, &old_ty, (jl_value_t*)jl_any_type);
}
jl_errorf("invalid redefinition of constant %s",
jl_symbol_name(bp->name));
Expand Down Expand Up @@ -807,11 +806,8 @@ void jl_binding_deprecation_warning(jl_module_t *m, jl_binding_t *b)
JL_DLLEXPORT void jl_checked_assignment(jl_binding_t *b, jl_value_t *rhs)
{
jl_value_t *old_ty = NULL;
if (!jl_atomic_cmpswap_relaxed(&b->ty, &old_ty, (jl_value_t*)jl_any_type) && !jl_isa(rhs, old_ty)) {
jl_errorf("cannot assign an incompatible value to the global %s.",
jl_symbol_name(b->name));
}
if (b->constp) {
jl_atomic_cmpswap_relaxed(&b->ty, &old_ty, jl_typeof(rhs));
jl_value_t *old = NULL;
if (jl_atomic_cmpswap(&b->value, &old, rhs)) {
jl_gc_wb_binding(b, rhs);
Expand All @@ -828,6 +824,10 @@ JL_DLLEXPORT void jl_checked_assignment(jl_binding_t *b, jl_value_t *rhs)
jl_safe_printf("WARNING: redefinition of constant %s. This may fail, cause incorrect answers, or produce other errors.\n",
jl_symbol_name(b->name));
}
if (!jl_atomic_cmpswap_relaxed(&b->ty, &old_ty, (jl_value_t*)jl_any_type) && !jl_isa(rhs, old_ty)) {
jl_errorf("cannot assign an incompatible value to the global %s.",
jl_symbol_name(b->name));
}
jl_atomic_store_relaxed(&b->value, rhs);
jl_gc_wb_binding(b, rhs);
}
Expand Down
3 changes: 3 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1109,3 +1109,6 @@ end
@testset "Base/timing.jl" begin
@test Base.jit_total_bytes() >= 0
end

const I_AM_A_CONSTANT_INT = 1
@test Core.get_binding_type(@__MODULE__, :I_AM_A_CONSTANT_INT) == Int

0 comments on commit 5db09a7

Please sign in to comment.