Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always disallow assignments to constants #57567

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,19 @@ JL_DLLEXPORT void jl_check_binding_currently_writable(jl_binding_t *b, jl_module
{
jl_binding_partition_t *bpart = jl_get_binding_partition(b, jl_current_task->world_age);
enum jl_partition_kind kind = jl_binding_kind(bpart);
if (kind != BINDING_KIND_GLOBAL && kind != BINDING_KIND_DECLARED && !jl_bkind_is_some_constant(kind)) {
if (kind != BINDING_KIND_GLOBAL && kind != BINDING_KIND_DECLARED) {
if (jl_bkind_is_some_guard(kind)) {
jl_errorf("Global %s.%s does not exist and cannot be assigned.\n"
"Note: Julia 1.9 and 1.10 inadvertently omitted this error check (#56933).\n"
"Hint: Declare it using `global %s` inside `%s` before attempting assignment.",
jl_symbol_name(m->name), jl_symbol_name(s),
jl_symbol_name(s), jl_symbol_name(m->name));
} else {
}
else if (jl_bkind_is_some_constant(kind)) {
jl_errorf("invalid assignment to constant %s.%s. This redefinition may be permitted using the `const` keyword.",
jl_symbol_name(m->name), jl_symbol_name(s));
}
else {
jl_module_t *from = jl_binding_dbgmodule(b, m, s);
if (from == m)
jl_errorf("cannot assign a value to imported variable %s.%s",
Expand Down Expand Up @@ -1449,16 +1454,6 @@ jl_value_t *jl_check_binding_assign_value(jl_binding_t *b JL_PROPAGATES_ROOT, jl
JL_GC_PUSH1(&rhs); // callee-rooted
jl_binding_partition_t *bpart = jl_get_binding_partition(b, jl_current_task->world_age);
enum jl_partition_kind kind = jl_binding_kind(bpart);
if (jl_bkind_is_some_constant(kind)) {
jl_value_t *old = bpart->restriction;
JL_GC_PROMISE_ROOTED(old);
if (jl_egal(rhs, old)) {
JL_GC_POP();
return NULL;
}
jl_errorf("invalid assignment to constant %s.%s. This redefinition may be permitted using the `const` keyword.",
jl_symbol_name(mod->name), jl_symbol_name(var));
}
assert(kind == BINDING_KIND_DECLARED || kind == BINDING_KIND_GLOBAL);
jl_value_t *old_ty = kind == BINDING_KIND_DECLARED ? (jl_value_t*)jl_any_type : bpart->restriction;
JL_GC_PROMISE_ROOTED(old_ty);
Expand Down
7 changes: 6 additions & 1 deletion test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3969,8 +3969,13 @@ end

# Test trying to define a constant and then trying to assign to the same value
module AssignConstValueTest
using Test
const x = 1
x = 1
@test_throws ErrorException @eval x = 1
@test_throws ErrorException @eval begin
@Base.Experimental.force_compile
global x = 1
end
end
@test isconst(AssignConstValueTest, :x)

Expand Down