diff --git a/_test/switch39.go b/_test/switch39.go new file mode 100644 index 000000000..881a1d3ac --- /dev/null +++ b/_test/switch39.go @@ -0,0 +1,17 @@ +package main + +func f(params ...interface{}) { + switch p0 := params[0].(type) { + case string: + println("string:", p0) + default: + println("not a string") + } +} + +func main() { + f("Hello") +} + +// Output: +// string: Hello diff --git a/_test/switch40.go b/_test/switch40.go new file mode 100644 index 000000000..348e6ddc6 --- /dev/null +++ b/_test/switch40.go @@ -0,0 +1,17 @@ +package main + +func f(params ...interface{}) { + switch params[0].(type) { + case string: + println("a string") + default: + println("not a string") + } +} + +func main() { + f("Hello") +} + +// Output: +// a string diff --git a/interp/cfg.go b/interp/cfg.go index 34c1df0c9..bd579662e 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -2051,7 +2051,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string } sbn.start = clauses[0].start n.start = n.child[0].start - n.child[0].tnext = sbn.start + if n.kind == typeSwitch { + // Handle the typeSwitch init (the type assert expression). + init := n.child[1].lastChild().child[0] + init.tnext = sbn.start + n.child[0].tnext = init.start + } else { + n.child[0].tnext = sbn.start + } case switchIfStmt: // like an if-else chain sc = sc.pop()