Skip to content

Commit

Permalink
Fail gracefully when attempting pair destructuring (JuliaLang#49368)
Browse files Browse the repository at this point in the history
`a => b = x` was interpreted as a function definition. Now it's an error unless you `import Base: =>`
  • Loading branch information
LilithHafner committed Jun 8, 2023
1 parent c4d162e commit 746a15b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ JL_DLLEXPORT jl_binding_t *jl_get_binding_for_method_def(jl_module_t *m, jl_sym_
}
// TODO: we might want to require explicitly importing types to add constructors
// or we might want to drop this error entirely
if (!b->imported && (!b2->constp || !jl_is_type(f))) {
if (!b->imported && !(b2->constp && jl_is_type(f) && strcmp(jl_symbol_name(var), "=>") != 0)) {
jl_errorf("invalid method definition in %s: function %s.%s must be explicitly imported to be extended",
jl_symbol_name(m->name), jl_symbol_name(from->name), jl_symbol_name(var));
}
Expand Down
6 changes: 6 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3483,3 +3483,9 @@ end
# issue #49984
macro z49984(s); :(let a; $(esc(s)); end); end
@test let a = 1; @z49984(a) === 1; end

# issues #37783, #39929, #42552, #43379, and #48332
let x = 1 => 2
@test_throws ErrorException @eval a => b = 2
@test_throws "function Base.=> must be explicitly imported to be extended" @eval a => b = 2
end

0 comments on commit 746a15b

Please sign in to comment.