Skip to content

Commit

Permalink
interp: fix return of untyped values for defined types
Browse files Browse the repository at this point in the history
Fixes #1475
  • Loading branch information
mvertes committed Oct 25, 2022
1 parent 4a80936 commit 71112db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
12 changes: 12 additions & 0 deletions _test/issue-1475.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type T uint16

func f() T { return 0 }

func main() {
println(f())
}

// Output:
// 0
35 changes: 16 additions & 19 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2396,35 +2396,32 @@ func _return(n *node) {
switch t := def.typ.ret[i]; t.cat {
case errorT:
values[i] = genInterfaceWrapper(c, t.TypeOf())
case aliasT:
if isInterfaceSrc(t) {
values[i] = genValueInterface(c)
} else {
values[i] = genValue(c)
}
case interfaceT:
if len(t.field) == 0 {
// empty interface case.
// we can't let genValueInterface deal with it, because we call on c,
// not on n, which means that the interfaceT knowledge is lost.
values[i] = genValue(c)
break
}
values[i] = genValueInterface(c)
case funcT:
values[i] = genValue(c)
case valueT:
switch t.rtype.Kind() {
case reflect.Interface:
values[i] = genInterfaceWrapper(c, t.rtype)
values[i] = genInterfaceWrapper(c, t.TypeOf())
continue
case reflect.Func:
values[i] = genFunctionWrapper(c)
continue
}
fallthrough
default:
if c.typ.untyped {
values[i] = genValueAs(c, def.typ.ret[i].TypeOf())
} else {
switch {
case isInterfaceSrc(t):
if len(t.field) == 0 {
// empty interface case.
// we can't let genValueInterface deal with it, because we call on c,
// not on n, which means that the interfaceT knowledge is lost.
values[i] = genValue(c)
break
}
values[i] = genValueInterface(c)
case c.typ.untyped:
values[i] = genValueAs(c, t.TypeOf())
default:
values[i] = genValue(c)
}
}
Expand Down

0 comments on commit 71112db

Please sign in to comment.