@@ -433,6 +433,14 @@ function _resolve_scopes(ctx, ex::SyntaxTree)
433433 throw (LoweringError (ex, " type declarations for global variables must be at top level, not inside a function" ))
434434 end
435435 end
436+ id = ex_out[1 ]
437+ if kind (id) != K " Placeholder"
438+ binfo = lookup_binding (ctx, id)
439+ if ! isnothing (binfo. type)
440+ throw (LoweringError (ex, " multiple type declarations found for `$(binfo. name) `" ))
441+ end
442+ update_binding! (ctx, id; type= ex_out[2 ])
443+ end
436444 ex_out
437445 elseif k == K " always_defined"
438446 id = lookup_var (ctx, NameKey (ex[1 ]))
@@ -624,14 +632,22 @@ function analyze_variables!(ctx, ex)
624632 k = kind (ex)
625633 if k == K " BindingId"
626634 if has_lambda_binding (ctx, ex)
627- # FIXME : Move this after closure conversion so that we don't need
635+ # TODO : Move this after closure conversion so that we don't need
628636 # to model the closure conversion transformations here.
629637 update_lambda_binding! (ctx, ex, is_read= true )
638+ else
639+ binfo = lookup_binding (ctx, ex. var_id)
640+ if ! binfo. is_ssa && binfo. kind != :global
641+ # The type of typed locals is invisible in the previous pass,
642+ # but is filled in here.
643+ init_lambda_binding (ctx. lambda_bindings, ex. var_id, is_captured= true , is_read= true )
644+ update_binding! (ctx, ex, is_captured= true )
645+ end
630646 end
631647 elseif is_leaf (ex) || is_quoted (ex)
632648 return
633649 elseif k == K " local" || k == K " global"
634- # Uses of bindings which don't count as uses .
650+ # Presence of BindingId within local/global is ignored .
635651 return
636652 elseif k == K " ="
637653 lhs = ex[1 ]
@@ -640,6 +656,12 @@ function analyze_variables!(ctx, ex)
640656 if has_lambda_binding (ctx, lhs)
641657 update_lambda_binding! (ctx, lhs, is_assigned= true )
642658 end
659+ lhs_binfo = lookup_binding (ctx, lhs)
660+ if ! isnothing (lhs_binfo. type)
661+ # Assignments introduce a variable's type later during closure
662+ # conversion, but we must model that explicitly here.
663+ analyze_variables! (ctx, lhs_binfo. type)
664+ end
643665 end
644666 analyze_variables! (ctx, ex[2 ])
645667 elseif k == K " function_decl"
@@ -655,17 +677,6 @@ function analyze_variables!(ctx, ex)
655677 if kind (ex[1 ]) != K " BindingId" || lookup_binding (ctx, ex[1 ]). kind != = :local
656678 analyze_variables! (ctx, ex[1 ])
657679 end
658- elseif k == K " decl"
659- @chk numchildren (ex) == 2
660- id = ex[1 ]
661- if kind (id) != K " Placeholder"
662- binfo = lookup_binding (ctx, id)
663- if ! isnothing (binfo. type)
664- throw (LoweringError (ex, " multiple type declarations found for `$(binfo. name) `" ))
665- end
666- update_binding! (ctx, id; type= ex[2 ])
667- end
668- analyze_variables! (ctx, ex[2 ])
669680 elseif k == K " const"
670681 id = ex[1 ]
671682 if lookup_binding (ctx, id). kind == :local
@@ -677,7 +688,7 @@ function analyze_variables!(ctx, ex)
677688 if kind (name) == K " BindingId"
678689 id = name. var_id
679690 if has_lambda_binding (ctx, id)
680- # FIXME : Move this after closure conversion so that we don't need
691+ # TODO : Move this after closure conversion so that we don't need
681692 # to model the closure conversion transformations.
682693 update_lambda_binding! (ctx, id, is_called= true )
683694 end
@@ -710,9 +721,10 @@ function analyze_variables!(ctx, ex)
710721 end
711722 ctx2 = VariableAnalysisContext (ctx. graph, ctx. bindings, ctx. mod, lambda_bindings,
712723 ctx. method_def_stack, ctx. closure_bindings)
713- # Add any captured bindings to the enclosing lambda, if necessary.
724+ foreach (e -> analyze_variables! (ctx2, e), ex[ 3 : end ]) # body & return type
714725 for (id,lbinfo) in pairs (lambda_bindings. bindings)
715726 if lbinfo. is_captured
727+ # Add any captured bindings to the enclosing lambda, if necessary.
716728 outer_lbinfo = lookup_lambda_binding (ctx. lambda_bindings, id)
717729 if isnothing (outer_lbinfo)
718730 # Inner lambda captures a variable. If it's not yet present
@@ -723,9 +735,6 @@ function analyze_variables!(ctx, ex)
723735 end
724736 end
725737 end
726-
727- # TODO : Types of any assigned captured vars will also be used and might be captured.
728- foreach (e-> analyze_variables! (ctx2, e), ex[3 : end ])
729738 else
730739 foreach (e-> analyze_variables! (ctx, e), children (ex))
731740 end
0 commit comments