From 0ed4b362dc949817e46227fdd529442176994d84 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 3 Nov 2020 17:48:03 +0100 Subject: [PATCH] interp: implement conversion for interpreter function types Interpreter function types are represented internally by the AST node of their definition. The conversion operation creates a new node with the type field pointing to the target type. Fixes #936. --- _test/convert0.go | 21 +++++++++++++++++++++ interp/run.go | 15 +++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 _test/convert0.go diff --git a/_test/convert0.go b/_test/convert0.go new file mode 100644 index 000000000..acb7f96c6 --- /dev/null +++ b/_test/convert0.go @@ -0,0 +1,21 @@ +package main + +type T struct { + v int +} + +type comparator func(T, T) bool + +func sort(items []T, comp comparator) { + println("in sort") +} + +func compT(t0, t1 T) bool { return t0.v < t1.v } + +func main() { + a := []T{} + sort(a, comparator(compT)) +} + +// Output: +// in sort diff --git a/interp/run.go b/interp/run.go index 5b1e39acb..249ea200a 100644 --- a/interp/run.go +++ b/interp/run.go @@ -390,6 +390,21 @@ func convert(n *node) { return } + if n.child[0].typ.cat == funcT && c.typ.cat == funcT { + value := genValue(c) + n.exec = func(f *frame) bltn { + n, ok := value(f).Interface().(*node) + if !ok || !n.typ.convertibleTo(c.typ) { + panic("cannot convert") + } + n1 := *n + n1.typ = c.typ + dest(f).Set(reflect.ValueOf(&n1)) + return next + } + return + } + var value func(*frame) reflect.Value if c.typ.cat == funcT { value = genFunctionWrapper(c)