Skip to content

Commit c997f14

Browse files
committed
remove implicitly inherited globals
1 parent a26b7d0 commit c997f14

File tree

2 files changed

+75
-56
lines changed

2 files changed

+75
-56
lines changed

src/julia-syntax.scm

+19-13
Original file line numberDiff line numberDiff line change
@@ -1718,9 +1718,9 @@
17181718
(define (expand-for while lhs X body)
17191719
;; (for (= lhs X) body)
17201720
(let* ((coll (make-ssavalue))
1721-
(state (gensy))
1722-
(outer? (and (pair? lhs) (eq? (car lhs) 'outer)))
1723-
(lhs (if outer? (cadr lhs) lhs)))
1721+
(state (gensy))
1722+
(outer? (and (pair? lhs) (eq? (car lhs) 'outer)))
1723+
(lhs (if outer? (cadr lhs) lhs)))
17241724
`(block (= ,coll ,(expand-forms X))
17251725
(= ,state (call (top start) ,coll))
17261726
;; TODO avoid `local declared twice` error from this
@@ -2284,15 +2284,15 @@
22842284
'while
22852285
(lambda (e)
22862286
`(break-block loop-exit
2287-
(_while ,(expand-forms (cadr e))
2288-
(break-block loop-cont
2289-
(scope-block ,(blockify (expand-forms (caddr e))))))))
2287+
(_while ,(expand-forms (cadr e))
2288+
(break-block loop-cont
2289+
(scope-block ,(blockify (expand-forms (caddr e))))))))
22902290

22912291
'inner-while
22922292
(lambda (e)
22932293
`(_while ,(expand-forms (cadr e))
2294-
(break-block loop-cont
2295-
(scope-block ,(blockify (expand-forms (caddr e)))))))
2294+
(break-block loop-cont
2295+
(scope-block ,(blockify (expand-forms (caddr e)))))))
22962296

22972297
'break
22982298
(lambda (e)
@@ -2571,10 +2571,15 @@
25712571
(define (find-local-def-decls e) (find-decls 'local-def e))
25722572
(define (find-global-decls e) (find-decls 'global e))
25732573

2574-
(define (implicit-locals e env glob)
2574+
(define (implicit-locals e env deprecated-env glob)
25752575
;; const decls on non-globals introduce locals
25762576
(append! (diff (find-decls 'const e) glob)
2577-
(find-assigned-vars e env)))
2577+
(filter
2578+
(lambda (v)
2579+
(if (memq v deprecated-env)
2580+
(begin (syntax-deprecation #f (string "implicit assignment to global variable `" v "`") (string "global " v)) #f)
2581+
#t))
2582+
(find-assigned-vars e env))))
25782583

25792584
(define (unbound-vars e bound tab)
25802585
(cond ((or (eq? e 'true) (eq? e 'false) (eq? e UNUSED)) tab)
@@ -2624,15 +2629,16 @@
26242629
((eq? (car e) 'scope-block)
26252630
(let* ((blok (cadr e)) ;; body of scope-block expression
26262631
(other-locals (if lam (caddr lam) '())) ;; locals that are explicitly part of containing lambda expression
2627-
(iglo (find-decls 'implicit-global blok)) ;; globals defined implicitly outside blok
2632+
(iglo (find-decls 'implicit-global blok)) ;; globals possibly defined implicitly outside blok
26282633
(glob (diff (find-global-decls blok) iglo)) ;; all globals declared in blok
26292634
(vars-def (check-dups (find-local-def-decls blok) '()))
26302635
(locals-declared (check-dups (find-local-decls blok) vars-def))
26312636
(locals-implicit (implicit-locals
26322637
blok
26332638
;; being declared global prevents a variable
26342639
;; assignment from introducing a local
2635-
(append env glob iglo outerglobals locals-declared vars-def)
2640+
(append env glob iglo locals-declared vars-def)
2641+
outerglobals
26362642
(append glob iglo)))
26372643
(vars (delete-duplicates (append! locals-declared locals-implicit)))
26382644
(all-vars (append vars vars-def))
@@ -2669,7 +2675,7 @@
26692675
(memq var implicitglobals) ;; remove anything only added implicitly in the last scope block
26702676
(memq var glob))))) ;; remove anything that's now global
26712677
renames)))
2672-
(new-oglo (append iglo outerglobals)) ;; list of all outer-globals from outside blok
2678+
(new-oglo (append iglo outerglobals)) ;; list of all implicit-globals from outside blok
26732679
(body (resolve-scopes- blok new-env new-oglo new-iglo lam new-renames #f))
26742680
(real-new-vars (append (diff vars need-rename) renamed))
26752681
(real-new-vars-def (append (diff vars-def need-rename-def) renamed-def)))

test/core.jl

+56-43
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ begin
371371
global f7234_cnt += -10000
372372
end
373373
end
374-
@test_throws UndefVarError f7234_a()
374+
@test_throws UndefVarError(:glob_x2) f7234_a()
375375
@test f7234_cnt == 1
376376
begin
377377
global glob_x2 = 24
@@ -381,11 +381,11 @@ begin
381381
global f7234_cnt += -10000
382382
end
383383
end
384-
@test_throws UndefVarError f7234_b()
384+
@test_throws UndefVarError(:glob_x2) f7234_b()
385385
@test f7234_cnt == 2
386-
# existing globals can be inherited by non-function blocks
386+
# globals can accessed if declared
387387
for i = 1:2
388-
glob_x2 += 1
388+
global glob_x2 += 1
389389
end
390390
@test glob_x2 == 26
391391
# globals declared as such in a non-global scope are inherited
@@ -454,15 +454,15 @@ function const_implies_local()
454454
end
455455
@test const_implies_local() === (1, 0)
456456

457-
a = Vector{Any}(3)
458-
for i=1:3
457+
a_global_closure_vector = Vector{Any}(3)
458+
for i = 1:3
459459
let ii = i
460-
a[i] = x->x+ii
460+
a_global_closure_vector[i] = x -> x + ii
461461
end
462462
end
463-
@test a[1](10) == 11
464-
@test a[2](10) == 12
465-
@test a[3](10) == 13
463+
@test a_global_closure_vector[1](10) == 11
464+
@test a_global_closure_vector[2](10) == 12
465+
@test a_global_closure_vector[3](10) == 13
466466

467467
# issue #22032
468468
let a = [], fs = []
@@ -499,8 +499,10 @@ end
499499
@test_throws UndefVarError f21900()
500500
@test f21900_cnt == 1
501501

502-
@test_throws UndefVarError @eval begin
502+
# use @eval so this runs as a toplevel scope block
503+
@test_throws UndefVarError(:foo21900) @eval begin
503504
for i21900 = 1:10
505+
local bar21900
504506
for j21900 = 1:10
505507
foo21900 = 10
506508
end
@@ -511,8 +513,9 @@ end
511513
@test !@isdefined(foo21900)
512514
@test !@isdefined(bar21900)
513515
bar21900 = 0
514-
@test_throws UndefVarError @eval begin
516+
@test_throws UndefVarError(:foo21900) @eval begin
515517
for i21900 = 1:10
518+
global bar21900
516519
for j21900 = 1:10
517520
foo21900 = 10
518521
end
@@ -523,8 +526,9 @@ end
523526
@test bar21900 == -1
524527
@test !@isdefined foo21900
525528
foo21900 = 0
526-
@test nothing === @eval begin
529+
@test nothing === begin
527530
for i21900 = 1:10
531+
global bar21900, foo21900
528532
for j21900 = 1:10
529533
foo21900 = 10
530534
end
@@ -682,40 +686,47 @@ end
682686

683687
# try/finally
684688
begin
685-
after = 0
686-
b = try
689+
try_finally_glo_after = 0
690+
try_finally_loc_after = 0
691+
try_finally_glo_b = try
687692
1+2
688693
finally
689-
after = 1
694+
# try_finally_loc_after = 1 # enable with #19324
695+
global try_finally_glo_after = 1
690696
end
691-
@test b == 3
692-
@test after == 1
697+
@test try_finally_loc_after == 0
698+
@test try_finally_glo_b == 3
699+
@test try_finally_glo_after == 1
693700

694-
after = 0
701+
try_finally_glo_after = 0
695702
gothere = 0
696703
try
697704
try
698705
error(" ")
699706
finally
700-
after = 1
707+
# try_finally_loc_after = 1 # enable with #19324
708+
global try_finally_glo_after = 1
701709
end
702-
gothere = 1
710+
global gothere = 1
703711
end
704-
@test after == 1
712+
@test try_finally_loc_after == 0
713+
@test try_finally_glo_after == 1
705714
@test gothere == 0
706715

707-
after = 0
708-
b = try
716+
try_finally_glo_after = 0
717+
try_finally_glo_b = try
709718
error(" ")
710719
catch
711720
42
712721
finally
713-
after = 1
722+
# try_finally_loc_after = 1 # enable with #19324
723+
global try_finally_glo_after = 1
714724
end
715-
@test b == 42
716-
@test after == 1
725+
@test try_finally_loc_after == 0
726+
@test try_finally_glo_b == 42
727+
@test try_finally_glo_after == 1
717728

718-
glo = 0
729+
global glo = 0
719730
function retfinally()
720731
try
721732
return 5
@@ -1223,7 +1234,7 @@ C3729{D} = Vector{Vector{D}}
12231234
# issue #3789
12241235
x3789 = 0
12251236
while(all([false for idx in 1:10]))
1226-
x3789 = 1
1237+
global x3789 = 1
12271238
end
12281239
@test x3789 == 0
12291240

@@ -1446,7 +1457,7 @@ b4688(y) = "not an Int"
14461457
begin
14471458
a4688(y::Int) = "an Int"
14481459
let x = true
1449-
b4688(y::Int) = x == true ? a4688(y) : a4688(y)
1460+
global b4688(y::Int) = x == true ? a4688(y) : a4688(y)
14501461
end
14511462
end
14521463
@test b4688(1) == "an Int"
@@ -1540,9 +1551,8 @@ function tupledispatch(a::TupleParam{(1,:a)})
15401551
a.x
15411552
end
15421553

1543-
let
1544-
# tuples can be used as type params
1545-
t1 = TupleParam{(1,:a)}(true)
1554+
# tuples can be used as type params
1555+
let t1 = TupleParam{(1,:a)}(true),
15461556
t2 = TupleParam{(1,:b)}(true)
15471557

15481558
# tuple type params can't contain invalid type params
@@ -2003,11 +2013,13 @@ function issue7897!(data, arr)
20032013
a = arr[1]
20042014
end
20052015

2006-
a = ones(UInt8, 10)
2007-
sa = view(a,4:6)
2008-
# This can throw an error, but shouldn't segfault
2009-
try
2010-
issue7897!(sa, zeros(10))
2016+
let
2017+
a = ones(UInt8, 10)
2018+
sa = view(a, 4:6)
2019+
# This can throw an error, but shouldn't segfault
2020+
try
2021+
issue7897!(sa, zeros(10))
2022+
end
20112023
end
20122024

20132025
# issue #7582
@@ -3813,7 +3825,8 @@ end
38133825
# issue #15283
38143826
j15283 = 0
38153827
let
3816-
k15283 = j15283+=1
3828+
global j15283
3829+
k15283 = (j15283 += 1)
38173830
end
38183831
@test j15283 == 1
38193832
@test !@isdefined k15283
@@ -4467,12 +4480,12 @@ end
44674480
@test_throws ErrorException main18986()
44684481

44694482
# issue #18085
4470-
f18085(a,x...) = (0,)
4471-
for (f,g) in ((:asin,:sin), (:acos,:cos))
4483+
f18085(a, x...) = (0, )
4484+
for (f, g) in ((:asin, :sin), (:acos, :cos))
44724485
gx = eval(g)
4473-
f18085(::Type{Val{f}},x...) = map(x->2gx(x), f18085(Val{g},x...))
4486+
global f18085(::Type{Val{f}}, x...) = map(x -> 2gx(x), f18085(Val{g}, x...))
44744487
end
4475-
@test f18085(Val{:asin},3) === (0.0,)
4488+
@test f18085(Val{:asin}, 3) === (0.0,)
44764489

44774490
# issue #18236 constant VecElement in ast triggers codegen assertion/undef
44784491
# VecElement of scalar

0 commit comments

Comments
 (0)