Skip to content

Commit

Permalink
interp: don't panic for undefined types
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak authored and traefiker committed Sep 25, 2019
1 parent 424e7ac commit 8db7a81
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) {
} else if n.anc.typ != nil {
n.typ = n.anc.typ.val
}
// FIXME n.typ can be nil.
if n.typ == nil {
err = n.cfgErrorf("undefined type")
return false
}
n.typ.untyped = true
}
// Propagate type to children, to handle implicit types
Expand Down
26 changes: 26 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,32 @@ func TestEvalChan(t *testing.T) {
})
}

func TestEvalMissingSymbol(t *testing.T) {
defer func() {
r := recover()
if r != nil {
t.Errorf("unexpected panic: %v", r)
}
}()

type S2 struct{}
type S1 struct {
F S2
}
i := interp.New(interp.Options{})
i.Use(interp.Exports{"p": map[string]reflect.Value{
"S1": reflect.Zero(reflect.TypeOf(&S1{})),
}})
_, err := i.Eval(`import "p"`)
if err != nil {
t.Fatalf("failed to import package: %v", err)
}
_, err = i.Eval(`p.S1{F: p.S2{}}`)
if err == nil {
t.Error("unexpected nil error for expression with undefined type")
}
}

func runTests(t *testing.T, i *interp.Interpreter, tests []testCase) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
Expand Down

0 comments on commit 8db7a81

Please sign in to comment.