Skip to content

Commit

Permalink
interp: fix case behavior for values converted to empty interface
Browse files Browse the repository at this point in the history
Fixes #1465.
  • Loading branch information
mvertes authored Oct 25, 2022
1 parent e4e3d11 commit 7865c90
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
24 changes: 24 additions & 0 deletions _test/issue-1465.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
)

func SomeFunc[T int | string](defaultValue T) T {
switch v := any(&defaultValue).(type) {
case *string:
*v = *v + " abc"
case *int:
*v -= 234
}
return defaultValue
}

func main() {
fmt.Println(SomeFunc("test"))
fmt.Println(SomeFunc(1234))
}

// Output:
// test abc
// 1000
34 changes: 23 additions & 11 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3010,15 +3010,17 @@ func _case(n *node) {
}
return fnext
}
vi := v.Interface().(valueInterface)
if vi.node == nil {
if typ.cat == nilT {
return tnext
if vi, ok := v.Interface().(valueInterface); ok {
if vi.node != nil {
if vi.node.typ.id() == typ.id() {
destValue(f).Set(vi.value)
return tnext
}
}
return fnext
}
if vi.node.typ.id() == typ.id() {
destValue(f).Set(vi.value)
if v.Type() == typ.TypeOf() {
destValue(f).Set(v)
return tnext
}
return fnext
Expand Down Expand Up @@ -3053,13 +3055,23 @@ func _case(n *node) {
}
return fnext
}
if v := val.Interface().(valueInterface).node; v != nil {
for _, typ := range types {
if v.typ.id() == typ.id() {
destValue(f).Set(val)
return tnext
if vi, ok := val.Interface().(valueInterface); ok {
if v := vi.node; v != nil {
for _, typ := range types {
if v.typ.id() == typ.id() {
destValue(f).Set(val)
return tnext
}
}
}
return fnext
}
vt := val.Type()
for _, typ := range types {
if vt == typ.TypeOf() {
destValue(f).Set(val)
return tnext
}
}
return fnext
}
Expand Down

0 comments on commit 7865c90

Please sign in to comment.