Closed theclapp closed 2 months ago
This appears to've broken in v0.14.3. It works in v0.14.2.
I don't know if this is the right fix, but it's a fix:
% git diff interp
diff --git a/interp/cfg.go b/interp/cfg.go
index 39133a4c..c7a00eef 100644
--- a/interp/cfg.go
+++ b/interp/cfg.go
@@ -651,7 +651,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
var sym *symbol
var level int
- if dest.rval.IsValid() && isConstType(dest.typ) {
+ if dest.rval.IsValid() && !dest.rval.CanSet() && isConstType(dest.typ) {
err = n.cfgErrorf("cannot assign to %s (%s constant)", dest.rval, dest.typ.str)
break
}
I confirm the issue and I think that your fix is the right solution. Well done!
Feel free to submit a pull request with your proposed fix and test, so you can be properly credited. Thanks.
Thanks, will do!
Just a heads-up to any interested observers that this problem is not entirely fixed. I'm getting various weird panics in interpreted code when I do it. I'm still trying to nail down a test case. When I figure it out I'll open a new bug.
The way to prevent the panic / allow the update is to either create the variable as a pointer to begin with
// native Go
var V = new(float32)
// interpreted code where V has been imported via `interp.ImportUsed`.
*V += 5
math.Sin(*V)
or do a *&
dance whenever you access it:
// native Go
var V float32
// Interpreted code
*&V += 5
math.Sin(*&V)
As a hint to the dev team, should you be interested in looking into this without a concrete reproducer, when I tried to update a struct I got
panic: reflect: call of reflect.Value.SetFloat on zero Value
This might not actually be related to the problem, but I suspect that it is.
The following program
sample.go
triggers an unexpected resultExpected result
Got
Yaegi Version
3fbebb3
Additional Notes
In test-case form: add this to the end of interp/interp_eval_test.go: