Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: fix type switch on arbitrary expressions #1445

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions _test/switch39.go
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions _test/switch40.go
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down