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

interp: allow assignment to exported variables #1628

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"},
})
}
Loading