Skip to content

Commit 7fa0c13

Browse files
authored
Make no-body function declaration implicitly global (#57562)
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. Note that this implementation is slightly inefficient since it goes through a binding replacement. However, there's a change coming down the line which optimizes these replacements. I do think it should eventually be refactored to just create the binding directly, but that's a little bit of a larger change.
1 parent 2dd4cdf commit 7fa0c13

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/julia-syntax.scm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,9 @@
11851185
(cond ((and (length= e 2) (or (symbol? name) (globalref? name)))
11861186
(if (not (valid-name? name))
11871187
(error (string "invalid function name \"" name "\"")))
1188-
`(method ,name))
1188+
(if (globalref? name)
1189+
`(block (global ,name) (method ,name))
1190+
`(block (global-if-global ,name) (method ,name))))
11891191
((not (pair? name)) e)
11901192
((eq? (car name) 'call)
11911193
(let* ((raw-typevars (or where '()))
@@ -3131,6 +3133,10 @@
31313133
(if (eq? (var-kind (cadr e) scope) 'local)
31323134
(if (length= e 2) (null) `(= ,@(cdr e)))
31333135
`(const ,@(cdr e))))
3136+
((eq? (car e) 'global-if-global)
3137+
(if (eq? (var-kind (cadr e) scope) 'local)
3138+
'(null)
3139+
`(global ,@(cdr e))))
31343140
((memq (car e) '(local local-def))
31353141
(check-valid-name (cadr e))
31363142
;; remove local decls
@@ -3763,7 +3769,7 @@ f(x) = yt(x)
37633769
(Set '(quote top core lineinfo line inert local-def unnecessary copyast
37643770
meta inbounds boundscheck loopinfo decl aliasscope popaliasscope
37653771
thunk with-static-parameters toplevel-only
3766-
global globalref assign-const-if-global isglobal thismodule
3772+
global globalref global-if-global assign-const-if-global isglobal thismodule
37673773
const atomic null true false ssavalue isdefined toplevel module lambda
37683774
error gc_preserve_begin gc_preserve_end import using export public inline noinline purity)))
37693775

test/syntax.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,3 +4119,11 @@ end
41194119
# Issue #56904 - lambda linearized twice
41204120
@test (let; try 3; finally try 1; f(() -> x); catch x; end; end; x = 7; end) === 7
41214121
@test (let; try 3; finally try 4; finally try 1; f(() -> x); catch x; end; end; end; x = 7; end) === 7
4122+
4123+
# Issue #57546 - explicit function declaration should create new global
4124+
module FuncDecl57546
4125+
using Test
4126+
@test_nowarn @eval function Any end
4127+
@test isa(Any, Function)
4128+
@test isempty(methods(Any))
4129+
end

0 commit comments

Comments
 (0)