Skip to content

Disallow non-lhs all-underscore variable names #57626

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

Merged
merged 1 commit into from
Mar 25, 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
6 changes: 3 additions & 3 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4616,9 +4616,9 @@ f(x) = yt(x)
(let ((e1 (if (and arg-map (symbol? e))
(get arg-map e e)
e)))
(if (and value (or (underscore-symbol? e)
(and (pair? e) (eq? (car e) 'globalref)
(underscore-symbol? (cadr e)))))
(if (or (underscore-symbol? e)
(and (pair? e) (eq? (car e) 'globalref)
(underscore-symbol? (cadr e))))
(error (string "all-underscore identifiers are write-only and their values cannot be used in expressions" (format-loc current-loc))))
(cond (tail (emit-return tail e1))
(value e1)
Expand Down
3 changes: 2 additions & 1 deletion src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ static jl_value_t *resolve_definition_effects(jl_value_t *expr, jl_module_t *mod
int binding_effects, int eager_resolve)
{
if (jl_is_symbol(expr)) {
jl_error("Found raw symbol in code returned from lowering. Expected all symbols to have been resolved to GlobalRef or slots.");
jl_errorf("Found raw symbol %s in code returned from lowering. Expected all symbols to have been resolved to GlobalRef or slots.",
jl_symbol_name((jl_sym_t*)expr));
}
if (jl_is_globalref(expr)) {
jl_maybe_add_binding_backedge((jl_globalref_t*)expr, module, binding_edge);
Expand Down
14 changes: 14 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2819,6 +2819,20 @@ end
@m38386
@test isempty(methods(f38386))

@testset "non-lhs all-underscore vars should fail in lowering" begin
# OK
@test (_ = 1) === 1
@test ((_, _) = (1, 2)) == (1, 2)
@test Meta.isexpr(Meta.lower(Main, :(for _ in 1:2; 1; end)), :thunk)
@test (try; throw(1); catch _; 2; end) === 2
@test (let _ = 1; 2; end) === 2
# ERROR: syntax: all-underscore identifiers are write-only and their values cannot be used in expressions
@test Meta.isexpr(Meta.lower(Main, :(_ = 1; a = _)), :error)
@test Meta.isexpr(Meta.lower(Main, :(let; function f(); _; end; end)), :error)
@test Meta.isexpr(Meta.lower(Main, :(let; function f(); _; 1; end; end)), :error)
@test Meta.isexpr(Meta.lower(Main, :(begin; _; 1; end)), :error)
end

@testset "all-underscore varargs on the rhs" begin
@test ncalls_in_lowered(quote _..., = a end, GlobalRef(Base, :rest)) == 0
@test ncalls_in_lowered(quote ___..., = a end, GlobalRef(Base, :rest)) == 0
Expand Down