Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
We were using the wrong scope variable for else-if statements. This was
only visible when no variables had been declared before the else-if.
  • Loading branch information
jeremyschlatter committed Apr 29, 2015
1 parent 8670426 commit bc74b8d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,13 @@ func (v *visitor) finalizeNode() {

// Handle initializer, if it exists.
if ifstmt.Init != nil {
list = append(list, newCallStmt(idents.godebug, "ElseIfSimpleStmt", ast.NewIdent(idents.ctx), ast.NewIdent(idents.scope), newInt(pos2line(ifstmt.Init.Pos()))))
list = append(list, newCallStmt(idents.godebug, "ElseIfSimpleStmt", ast.NewIdent(idents.ctx), ast.NewIdent(v.scopeVar), newInt(pos2line(ifstmt.Init.Pos()))))
list = append(list, ifstmt.Init)
ifstmt.Init = nil
}

// Handle expression.
list = append(list, newCallStmt(idents.godebug, "ElseIfExpr", ast.NewIdent(idents.ctx), ast.NewIdent(idents.scope), newInt(pos2line(ifstmt.Cond.Pos()))))
list = append(list, newCallStmt(idents.godebug, "ElseIfExpr", ast.NewIdent(idents.ctx), ast.NewIdent(v.scopeVar), newInt(pos2line(ifstmt.Cond.Pos()))))
list = append(list, ifstmt)

// Swap in the new block.
Expand Down
8 changes: 8 additions & 0 deletions testdata/regressions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ invocations:
creates:
- $TMP/1.go
transcript:
---
desc: else-if should work when no local variables have been declared
invocations:
- dir: /
cmd: godebug run regressions/else-if.go
creates:
- $TMP/else-if.go
transcript:
13 changes: 13 additions & 0 deletions testdata/test-filesystem/regressions/else-if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"os"
)

func main() {
if o := os.Getenv("O"); o != "" {
println(o)
} else if s := os.Getenv("S"); s != "" {
println(s)
}
}

0 comments on commit bc74b8d

Please sign in to comment.