From 71112dbe871513360dd633640429438c15dc5d1c Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 25 Oct 2022 17:20:06 +0200 Subject: [PATCH] interp: fix return of untyped values for defined types Fixes #1475 --- _test/issue-1475.go | 12 ++++++++++++ interp/run.go | 35 ++++++++++++++++------------------- 2 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 _test/issue-1475.go diff --git a/_test/issue-1475.go b/_test/issue-1475.go new file mode 100644 index 000000000..1c99d6fee --- /dev/null +++ b/_test/issue-1475.go @@ -0,0 +1,12 @@ +package main + +type T uint16 + +func f() T { return 0 } + +func main() { + println(f()) +} + +// Output: +// 0 diff --git a/interp/run.go b/interp/run.go index 1de5b0614..e65efd1ac 100644 --- a/interp/run.go +++ b/interp/run.go @@ -2396,25 +2396,12 @@ 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) @@ -2422,9 +2409,19 @@ func _return(n *node) { } 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) } }