diff --git a/gnovm/pkg/gnolang/helpers.go b/gnovm/pkg/gnolang/helpers.go index b163b6a52a7..564ac0622c2 100644 --- a/gnovm/pkg/gnolang/helpers.go +++ b/gnovm/pkg/gnolang/helpers.go @@ -91,7 +91,7 @@ func Flds(args ...interface{}) FieldTypeExprs { func Recv(n, t interface{}) FieldTypeExpr { if n == "" { - n = "_" + n = blankIdentifier } return FieldTypeExpr{ Name: N(n), diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 68eb44290e2..2d939246dd9 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -315,7 +315,7 @@ func checkDuplicates(fset *FileSet) bool { name = d.Name case *ValueDecl: for _, nx := range d.NameExprs { - if nx.Name == "_" { + if nx.Name == blankIdentifier { continue } if _, ok := defined[nx.Name]; ok { @@ -327,7 +327,7 @@ func checkDuplicates(fset *FileSet) bool { default: continue } - if name == "_" { + if name == blankIdentifier { continue } if _, ok := defined[name]; ok { diff --git a/gnovm/pkg/gnolang/nodes.go b/gnovm/pkg/gnolang/nodes.go index 6f1584dcc28..482f4850b6e 100644 --- a/gnovm/pkg/gnolang/nodes.go +++ b/gnovm/pkg/gnolang/nodes.go @@ -1014,7 +1014,7 @@ type ValueDecl struct { func (x *ValueDecl) GetDeclNames() []Name { ns := make([]Name, 0, len(x.NameExprs)) for _, nx := range x.NameExprs { - if nx.Name == "_" { + if nx.Name == blankIdentifier { // ignore } else { ns = append(ns, nx.Name) @@ -1031,7 +1031,7 @@ type TypeDecl struct { } func (x *TypeDecl) GetDeclNames() []Name { - if x.NameExpr.Name == "_" { + if x.NameExpr.Name == blankIdentifier { return nil // ignore } else { return []Name{x.NameExpr.Name} @@ -1588,8 +1588,8 @@ func (sb *StaticBlock) GetParentNode(store Store) BlockNode { // Implements BlockNode. // As a side effect, notes externally defined names. func (sb *StaticBlock) GetPathForName(store Store, n Name) ValuePath { - if n == "_" { - return NewValuePathBlock(0, 0, "_") + if n == blankIdentifier { + return NewValuePathBlock(0, 0, blankIdentifier) } // Check local. gen := 1 @@ -1780,7 +1780,7 @@ func (sb *StaticBlock) Define2(isConst bool, n Name, st Type, tv TypedValue) { if tv.T == nil && tv.V != nil { panic("StaticBlock.Define2() requires .T if .V is set") } - if n == "_" { + if n == blankIdentifier { return // ignore } idx, exists := sb.GetLocalIndex(n) diff --git a/gnovm/pkg/gnolang/op_eval.go b/gnovm/pkg/gnolang/op_eval.go index bf4ac88aa25..701615fff13 100644 --- a/gnovm/pkg/gnolang/op_eval.go +++ b/gnovm/pkg/gnolang/op_eval.go @@ -47,7 +47,7 @@ func (m *Machine) doOpEval() { m.PopExpr() switch x.Kind { case INT: - x.Value = strings.ReplaceAll(x.Value, "_", "") + x.Value = strings.ReplaceAll(x.Value, blankIdentifier, "") // temporary optimization bi := big.NewInt(0) // TODO optimize. @@ -84,7 +84,7 @@ func (m *Machine) doOpEval() { V: BigintValue{V: bi}, }) case FLOAT: - x.Value = strings.ReplaceAll(x.Value, "_", "") + x.Value = strings.ReplaceAll(x.Value, blankIdentifier, "") if reFloat.MatchString(x.Value) { value := x.Value diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 4331e2aed9e..60406099e1c 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -9,7 +9,9 @@ import ( "github.com/gnolang/gno/tm2/pkg/errors" ) -const blankIdentifer string = "_" +const ( + blankIdentifier = "_" +) // In the case of a *FileSet, some declaration steps have to happen // in a restricted parallel way across all the files. @@ -191,7 +193,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { var defined bool for _, lx := range n.Lhs { ln := lx.(*NameExpr).Name - if ln == "_" { + if ln == blankIdentifier { // ignore. } else { _, ok := last.GetLocalIndex(ln) @@ -235,7 +237,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { case *FuncTypeExpr: for i := range n.Params { p := &n.Params[i] - if p.Name == "" || p.Name == "_" { + if p.Name == "" || p.Name == blankIdentifier { // create a hidden var with leading dot. // NOTE: document somewhere. pn := fmt.Sprintf(".arg_%d", i) @@ -244,7 +246,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { } for i := range n.Results { r := &n.Results[i] - if r.Name == "_" { + if r.Name == blankIdentifier { // create a hidden var with leading dot. // NOTE: document somewhere. rn := fmt.Sprintf(".res_%d", i) @@ -683,8 +685,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { } // specific and general cases switch n.Name { - case "_": - n.Path = NewValuePathBlock(0, 0, "_") + case blankIdentifier: + n.Path = NewValuePathBlock(0, 0, blankIdentifier) return n, TRANS_CONTINUE case "iota": pd := lastDecl(ns) @@ -1246,7 +1248,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { } // Type assertions on the blank identifier are illegal. - if nx, ok := n.X.(*NameExpr); ok && string(nx.Name) == blankIdentifer { + if nx, ok := n.X.(*NameExpr); ok && string(nx.Name) == blankIdentifier { panic("cannot use _ as value or type") } @@ -1929,8 +1931,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { pn := fn.GetParentNode(nil).(*PackageNode) for i := 0; i < numNames; i++ { nx := &n.NameExprs[i] - if nx.Name == "_" { - nx.Path = NewValuePathBlock(0, 0, "_") + if nx.Name == blankIdentifier { + nx.Path = NewValuePathBlock(0, 0, blankIdentifier) } else { pn.Define2(n.Const, nx.Name, sts[i], tvs[i]) nx.Path = last.GetPathForName(nil, nx.Name) @@ -1939,8 +1941,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { } else { for i := 0; i < numNames; i++ { nx := &n.NameExprs[i] - if nx.Name == "_" { - nx.Path = NewValuePathBlock(0, 0, "_") + if nx.Name == blankIdentifier { + nx.Path = NewValuePathBlock(0, 0, blankIdentifier) } else { last.Define2(n.Const, nx.Name, sts[i], tvs[i]) nx.Path = last.GetPathForName(nil, nx.Name) @@ -3054,7 +3056,7 @@ func predefineNow2(store Store, last BlockNode, d Decl, m map[Name]struct{}) (De // NOTE: unlike the *ValueDecl case, this case doesn't // preprocess d itself (only d.Type). if cd.IsMethod { - if cd.Recv.Name == "" || cd.Recv.Name == "_" { + if cd.Recv.Name == "" || cd.Recv.Name == blankIdentifier { // create a hidden var with leading dot. // NOTE: document somewhere. cd.Recv.Name = ".recv" @@ -3173,7 +3175,7 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) { } if d.Name == "" { // use default d.Name = pv.PkgName - } else if d.Name == "_" { // no definition + } else if d.Name == blankIdentifier { // no definition return } else if d.Name == "." { // dot import panic("dot imports not allowed in Gno") @@ -3200,8 +3202,8 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) { } for i := 0; i < len(d.NameExprs); i++ { nx := &d.NameExprs[i] - if nx.Name == "_" { - nx.Path.Name = "_" + if nx.Name == blankIdentifier { + nx.Path.Name = blankIdentifier } else { last2 := skipFile(last) last2.Predefine(d.Const, nx.Name) @@ -3381,7 +3383,7 @@ func constUntypedBigint(source Expr, i64 int64) *ConstExpr { } func fillNameExprPath(last BlockNode, nx *NameExpr, isDefineLHS bool) { - if nx.Name == "_" { + if nx.Name == blankIdentifier { // Blank name has no path; caller error. panic("should not happen") } diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index fda0a06f3d2..b0996b98b6f 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -2344,7 +2344,7 @@ func (b *Block) GetPointerToInt(store Store, index int) PointerValue { func (b *Block) GetPointerTo(store Store, path ValuePath) PointerValue { if path.IsBlockBlankPath() { if debug { - if path.Name != "_" { + if path.Name != blankIdentifier { panic(fmt.Sprintf( "zero value path is reserved for \"_\", but got %s", path.Name))