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

exporting method as variable #1182

Closed
mvertes opened this issue Jul 12, 2021 · 0 comments · Fixed by #1183
Closed

exporting method as variable #1182

mvertes opened this issue Jul 12, 2021 · 0 comments · Fixed by #1183
Labels
area/core bug Something isn't working
Milestone

Comments

@mvertes
Copy link
Member

mvertes commented Jul 12, 2021

The following program sample.go triggers an unexpected result

package main

import "sample/d2"   // see below for d2 definition

func main() {
    f := d2.F
    f()
}

Expected result

$ GO111MODULE=off go run ./sample.go
test

Got

$ yaegi ./sample.go
/Users/marc/go/src/sample/d1/d1.go:7:27: panic
sample.go:7:2: panic
run: reflect: call of reflect.Value.Field on zero Value
goroutine 1 [running]:
runtime/debug.Stack(0x1, 0x140002f8e00, 0x40)
	runtime/debug/stack.go:24 +0x88
github.com/traefik/yaegi/interp.(*Interpreter).eval.func1(0x140004cfbf0)
	github.com/traefik/yaegi/interp/interp.go:503 +0x94
panic(0x1036a0f60, 0x140001a2e70)
	runtime/panic.go:965 +0x14c
github.com/traefik/yaegi/interp.runCfg.func1(0x140001d6b00, 0x140001cf100, 0x140004cf9c0)
	github.com/traefik/yaegi/interp/run.go:185 +0x1e8
panic(0x1036a0f60, 0x140001a2e70)
	runtime/panic.go:965 +0x14c
github.com/traefik/yaegi/interp.runCfg.func1(0x140001d6bb0, 0x140004c6000, 0x140004cf780)
	github.com/traefik/yaegi/interp/run.go:185 +0x1e8
panic(0x1036a0f60, 0x140001a2e70)
	runtime/panic.go:965 +0x14c
reflect.Value.Field(0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x140004cf748)
	reflect/value.go:850 +0x11c
reflect.Value.FieldByIndex(0x0, 0x0, 0x0, 0x140003769f0, 0x1, 0x1, 0x10, 0x103f10a68, 0x10)
	reflect/value.go:882 +0x1f0
github.com/traefik/yaegi/interp.getPtrIndexSeq.func3(0x140001d6bb0, 0x10366df00)
	github.com/traefik/yaegi/interp/run.go:1917 +0xd0
github.com/traefik/yaegi/interp.runCfg(0x140004c6000, 0x140001d6bb0)
	github.com/traefik/yaegi/interp/run.go:191 +0x8c
github.com/traefik/yaegi/interp.call.func7(0x140001d6b00, 0x14000253f10)
	github.com/traefik/yaegi/interp/run.go:1280 +0xa20
github.com/traefik/yaegi/interp.runCfg(0x140001cf100, 0x140001d6b00)
	github.com/traefik/yaegi/interp/run.go:191 +0x8c
github.com/traefik/yaegi/interp.(*Interpreter).run(0x1400037a000, 0x140001ce900, 0x140001d68f0)
	github.com/traefik/yaegi/interp/run.go:122 +0x244
github.com/traefik/yaegi/interp.(*Interpreter).eval(0x1400037a000, 0x140001838c0, 0x5a, 0x16d1c37fe, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	github.com/traefik/yaegi/interp/interp.go:595 +0x5e8
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0x1400037a000, 0x16d1c37fe, 0xb, 0x14000183800, 0x5a, 0x0, 0x0, 0x0)
	github.com/traefik/yaegi/interp/interp.go:417 +0xc4
main.runFile(0x1400037a000, 0x16d1c37fe, 0xb, 0x0, 0x0, 0x0)
	github.com/traefik/yaegi/cmd/yaegi/run.go:149 +0x84
main.run(0x140001ba010, 0x1, 0x1, 0x14000186180, 0x14000073ea8)
	github.com/traefik/yaegi/cmd/yaegi/run.go:112 +0x830
main.main()
	github.com/traefik/yaegi/cmd/yaegi/yaegi.go:144 +0x324

Yaegi Version

78d7e85 (master at July 12th 2021)

Additional Notes

With the following packages ~/go/src/sample/d2/d2.go:

package d2

import "sample/d1"

var (
     x := d1.NewT("test")
     F := x.F
)

and ~/go/src/sample/d1/d1.go:

package d1

type T struct { Name string }

func (t *T)F { println(t.Name) }

func NewT(s string) *T { return &T{s} }

This issue is related to #1172.

@mvertes mvertes added area/core bug Something isn't working labels Jul 12, 2021
@mvertes mvertes added this to the v0.9.x milestone Jul 12, 2021
mvertes added a commit that referenced this issue Jul 12, 2021
When a function variable is assigned from an exported method,
make sure the receiver is updated from the coresponding symbol.

Fixes #1182.
traefiker pushed a commit that referenced this issue Jul 12, 2021
When a function variable is assigned from an exported method,
make sure the receiver is updated from the coresponding symbol.

Fixes #1182.
traefiker pushed a commit that referenced this issue Jul 13, 2021
When a function variable is assigned from an exported method,
make sure the receiver is updated from the coresponding symbol.

Fixes #1182.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant