Skip to content

Commit

Permalink
interp: allow assignment to exported variables
Browse files Browse the repository at this point in the history
If you `(*interp.Interpreter).Use` a variable, you should be able to assign to it.

Fixes #1623
  • Loading branch information
theclapp authored Apr 30, 2024
1 parent c828692 commit 381e045
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,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
}
Expand Down
24 changes: 24 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,3 +1906,27 @@ func TestIssue1383(t *testing.T) {
t.Fatal(err)
}
}

func TestIssue1623(t *testing.T) {
var f float64
var j int
var s string = "foo"

i := interp.New(interp.Options{})
if err := i.Use(interp.Exports{
"pkg/pkg": map[string]reflect.Value{
"F": reflect.ValueOf(&f).Elem(),
"J": reflect.ValueOf(&j).Elem(),
"S": reflect.ValueOf(&s).Elem(),
},
}); err != nil {
t.Fatal(err)
}
i.ImportUsed()

runTests(t, i, []testCase{
{desc: "pkg.F = 2.0", src: "pkg.F = 2.0; pkg.F", res: "2"},
{desc: "pkg.J = 3", src: "pkg.J = 3; pkg.J", res: "3"},
{desc: `pkg.S = "bar"`, src: `pkg.S = "bar"; pkg.S`, res: "bar"},
})
}

0 comments on commit 381e045

Please sign in to comment.