From ad557d0a8e2253fb720be88819b6273acce69b32 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Thu, 27 Feb 2025 21:51:44 +0000 Subject: [PATCH] Make no-body `function` declaration implicitly `global` These were the intended semantics of #57311 (and matches what it used to do in 1.11). Note however that this differs from the body-ful form, which now always tries to extend. Fixes #57546. --- src/julia-syntax.scm | 10 ++++++++-- test/syntax.jl | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 6beff02cb1a0b..194df7a52f700 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -1181,7 +1181,9 @@ (cond ((and (length= e 2) (or (symbol? name) (globalref? name))) (if (not (valid-name? name)) (error (string "invalid function name \"" name "\""))) - `(method ,name)) + (if (globalref? name) + `(block (global ,name) (method ,name)) + `(block (global-if-global ,name) (method ,name)))) ((not (pair? name)) e) ((eq? (car name) 'call) (let* ((raw-typevars (or where '())) @@ -3127,6 +3129,10 @@ (if (eq? (var-kind (cadr e) scope) 'local) (if (length= e 2) (null) `(= ,@(cdr e))) `(const ,@(cdr e)))) + ((eq? (car e) 'global-if-global) + (if (eq? (var-kind (cadr e) scope) 'local) + '(null) + `(global ,@(cdr e)))) ((memq (car e) '(local local-def)) (check-valid-name (cadr e)) ;; remove local decls @@ -3759,7 +3765,7 @@ f(x) = yt(x) (Set '(quote top core lineinfo line inert local-def unnecessary copyast meta inbounds boundscheck loopinfo decl aliasscope popaliasscope thunk with-static-parameters toplevel-only - global globalref assign-const-if-global isglobal thismodule + global globalref global-if-global assign-const-if-global isglobal thismodule const atomic null true false ssavalue isdefined toplevel module lambda error gc_preserve_begin gc_preserve_end import using export public inline noinline purity))) diff --git a/test/syntax.jl b/test/syntax.jl index a9bb19c128d81..97b2b8c70f061 100644 --- a/test/syntax.jl +++ b/test/syntax.jl @@ -4110,3 +4110,11 @@ end # Issue #56904 - lambda linearized twice @test (let; try 3; finally try 1; f(() -> x); catch x; end; end; x = 7; end) === 7 @test (let; try 3; finally try 4; finally try 1; f(() -> x); catch x; end; end; end; x = 7; end) === 7 + +# Issue #57546 - explicit function declaration should create new global +module FuncDecl57546 + using Test + @test_nowarn @eval function Any end + @test isa(Any, Function) + @test isempty(methods(Any)) +end