Skip to content

Commit

Permalink
interp: error instead of panic when assigning to a constant
Browse files Browse the repository at this point in the history
Add early detection of assigning to a constant during compiling instead of panicking at runtime.
  • Loading branch information
mvertes authored Oct 25, 2022
1 parent 71112db commit 9f43170
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
var sym *symbol
var level int

if dest.rval.IsValid() && isConstType(dest.typ) {
err = n.cfgErrorf("cannot assign to %s (%s constant)", dest.rval, dest.typ.str)
break
}
if isBlank(src) {
err = n.cfgErrorf("cannot use _ as value")
break
Expand Down
1 change: 1 addition & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func TestEvalAssign(t *testing.T) {
{src: "j := interface{}(int(1)); j.(_)", err: "1:54: cannot use _ as value"},
{src: "ff := func() (a, b, c int) {return 1, 2, 3}; x, y, x := ff()", err: "1:73: x repeated on left side of :="},
{src: "xx := 1; xx, _ := 2, 3", err: "1:37: no new variables on left side of :="},
{src: "1 = 2", err: "1:28: cannot assign to 1 (untyped int constant)"},
})
}

Expand Down

0 comments on commit 9f43170

Please sign in to comment.