Skip to content

Commit

Permalink
interp: treat var declaration within a block as a define statement
Browse files Browse the repository at this point in the history
This PR:

- Treats a `varDecl` within a block as a `defineStmt`
  - More specifically, any `varDecl` with a grandparent that is *not* a `fileStmt`
- Adds an extra condition to the handler for implicit const assignment
- Adds a tests to cover the changes
- Closes #1071
  • Loading branch information
firelizzard18 authored Apr 1, 2021
1 parent aa2621f commit d92051d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/go-cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ jobs:
strategy:
matrix:
go-version: [ 1.15, 1.16 ]
os: [ubuntu-latest, macos-latest, windows-latest]
# os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, windows-latest]

include:
- os: ubuntu-latest
go-path-suffix: /go
- os: macos-latest
go-path-suffix: /go
# - os: macos-latest
# go-path-suffix: /go
- os: windows-latest
go-path-suffix: \go

Expand Down
9 changes: 6 additions & 3 deletions interp/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
n := addChild(&root, anc, pos, identExpr, aNop)
n.ident = a.Name
st.push(n, nod)
if n.anc.kind == defineStmt && n.anc.nright == 0 {
if n.anc.kind == defineStmt && n.anc.anc.kind == constDecl && n.anc.nright == 0 {
// Implicit assign expression (in a ConstDecl block).
// Clone assign source and type from previous
a := n.anc
Expand Down Expand Up @@ -858,7 +858,8 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
case *ast.ValueSpec:
kind := valueSpec
act := aNop
if a.Values != nil {
switch {
case a.Values != nil:
if len(a.Names) > 1 && len(a.Values) == 1 {
if anc.node.kind == constDecl || anc.node.kind == varDecl {
kind = defineXStmt
Expand All @@ -874,7 +875,9 @@ func (interp *Interpreter) ast(src, name string, inc bool) (string, *node, error
}
act = aAssign
}
} else if anc.node.kind == constDecl {
case anc.node.kind == constDecl:
kind, act = defineStmt, aAssign
case anc.node.kind == varDecl && anc.node.anc.kind != fileStmt:
kind, act = defineStmt, aAssign
}
n := addChild(&root, anc, pos, kind, act)
Expand Down
2 changes: 2 additions & 0 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ func (interp *Interpreter) cfg(root *node, importPath string) ([]*node, error) {
case src.kind == basicLit:
// Assign to nil.
src.rval = reflect.New(dest.typ.TypeOf()).Elem()
case n.nright == 0:
n.gen = reset
}

n.typ = dest.typ
Expand Down
9 changes: 9 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ func TestEvalDecl(t *testing.T) {
})
}

func TestEvalDeclWithExpr(t *testing.T) {
i := interp.New(interp.Options{})
runTests(t, i, []testCase{
{src: `a1 := ""; var a2 int; a2 = 2`, res: "2"},
{src: `b1 := ""; const b2 = 2; b2`, res: "2"},
{src: `c1 := ""; var c2, c3 [8]byte; c3[3]`, res: "0"},
})
}

func TestEvalFunc(t *testing.T) {
i := interp.New(interp.Options{})
runTests(t, i, []testCase{
Expand Down

0 comments on commit d92051d

Please sign in to comment.