@@ -480,8 +480,6 @@ func (c *compiler) VisitVarDecl(d *ast.VarDecl) ast.VisitResult {
480480 varLocation = c .NewAlloca (Typ .IrType ())
481481 }
482482
483- Var := c .scp .addVar (d .Name (), varLocation , Typ , false )
484-
485483 // adds the variable initializer to the function fun
486484 addInitializer := func () {
487485 initVal , initTyp , isTemp := c .evaluate (d .InitVal ) // evaluate the initial value
@@ -496,7 +494,7 @@ func (c *compiler) VisitVarDecl(d *ast.VarDecl) ast.VisitResult {
496494 initVal , initTyp , isTemp = c .castNonAnyToAny (initVal , initTyp , isTemp , vtable )
497495 }
498496
499- c .claimOrCopy (Var , initVal , Typ , isTemp )
497+ c .claimOrCopy (varLocation , initVal , Typ , isTemp )
500498 }
501499
502500 if c .scp .enclosing == nil { // module_init
@@ -512,7 +510,7 @@ func (c *compiler) VisitVarDecl(d *ast.VarDecl) ast.VisitResult {
512510 c .moduleInitFunc , c .moduleInitCbb = c .cf , c .cbb
513511
514512 c .cf , c .cbb = c .moduleDisposeFunc , c .moduleDisposeFunc .Blocks [0 ]
515- c .freeNonPrimitive (Var , Typ ) // free the variable in module_dispose
513+ c .freeNonPrimitive (varLocation , Typ ) // free the variable in module_dispose
516514
517515 c .cf , c .cbb = cf , cbb
518516 }
@@ -522,6 +520,8 @@ func (c *compiler) VisitVarDecl(d *ast.VarDecl) ast.VisitResult {
522520 if c .cf != nil && c .cbb != nil { // ddp_main
523521 addInitializer ()
524522 }
523+
524+ c .scp .addVar (d .Name (), varLocation , Typ , false )
525525 return ast .VisitRecurse
526526}
527527
0 commit comments