]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: use zero error code to indicate unset error code
authorRobert Griesemer <gri@golang.org>
Fri, 7 Oct 2022 01:09:23 +0000 (18:09 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 10 Oct 2022 16:03:16 +0000 (16:03 +0000)
Use InvalidSyntaxError where the zero error code was used before.
Fix a couple of places that didn't set an error code.
Panic in error reporting if no error code is provided.

Change-Id: I3a537d42b720deb5c351bf38871e04919325e231
Reviewed-on: https://go-review.googlesource.com/c/go/+/439566
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
16 files changed:
src/cmd/compile/internal/types2/assignments.go
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/errors.go
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/index.go
src/cmd/compile/internal/types2/interface.go
src/cmd/compile/internal/types2/labels.go
src/cmd/compile/internal/types2/resolver.go
src/cmd/compile/internal/types2/signature.go
src/cmd/compile/internal/types2/stmt.go
src/cmd/compile/internal/types2/struct.go
src/cmd/compile/internal/types2/typexpr.go
src/go/types/assignments.go
src/go/types/call.go
src/go/types/errors.go

index 9120e8ce99b510ac378d390ee587c6c2ca3af334..73c126c027050618c48bc2101e1abf150bebe744 100644 (file)
@@ -28,7 +28,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
                // ok
        default:
                // we may get here because of other problems (issue #39634, crash 12)
-               check.errorf(x, 0, "cannot assign %s to %s in %s", x, T, context)
+               // TODO(gri) do we need a new "generic" error code here?
+               check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context)
                return
        }
 
index 0f8fbec43d34fa40e40d11d67ed1d5dc1001ca7b..5b1be07e84f3ce935dcdecc2ea82b8f35e700e9b 100644 (file)
@@ -287,7 +287,7 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
        for _, a := range args {
                switch a.mode {
                case typexpr:
-                       check.errorf(a, 0, "%s used as value", a)
+                       check.errorf(a, NotAnExpr, "%s used as value", a)
                        return
                case invalid:
                        return
index 7c6ecd8b02f012e8145f50e1932d1b0d13893c4d..a15d232aa3a83f491d45b112064688cf8b415af8 100644 (file)
@@ -899,7 +899,7 @@ func (check *Checker) declStmt(list []syntax.Decl) {
                        check.pop().setColor(black)
 
                default:
-                       check.errorf(s, 0, invalidAST+"unknown syntax.Decl node %T", s)
+                       check.errorf(s, InvalidSyntaxTree, invalidAST+"unknown syntax.Decl node %T", s)
                }
        }
 }
index ab9e483681b6414a75c5c68205c807a49fc45ebc..d1e4b65e1a7ad2a974417210efef7c90eab8fad5 100644 (file)
@@ -221,6 +221,10 @@ func (check *Checker) dump(format string, args ...interface{}) {
 }
 
 func (check *Checker) err(at poser, code Code, msg string, soft bool) {
+       if code == 0 {
+               panic("no error code provided")
+       }
+
        // Cheap trick: Don't report errors with messages containing
        // "invalid operand" or "invalid type" as those tend to be
        // follow-on errors which don't add useful information. Only
index 03040ae1b1c16d50e82de08592495a3a48f9676b..d2ec7bd7fd8efb0651e462433b552862285cb32f 100644 (file)
@@ -78,7 +78,7 @@ func (check *Checker) op(m opPredicates, x *operand, op syntax.Operator) bool {
                        return false
                }
        } else {
-               check.errorf(x, 0, invalidAST+"unknown operator %s", op)
+               check.errorf(x, InvalidSyntaxTree, invalidAST+"unknown operator %s", op)
                return false
        }
        return true
@@ -1337,7 +1337,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                        x.mode = value
                        x.typ = sig
                } else {
-                       check.errorf(e, 0, invalidAST+"invalid function literal %v", e)
+                       check.errorf(e, InvalidSyntaxTree, invalidAST+"invalid function literal %v", e)
                        goto Error
                }
 
@@ -1594,7 +1594,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                }
                // x.(type) expressions are encoded via TypeSwitchGuards
                if e.Type == nil {
-                       check.error(e, 0, invalidAST+"invalid use of AssertExpr")
+                       check.error(e, InvalidSyntaxTree, invalidAST+"invalid use of AssertExpr")
                        goto Error
                }
                T := check.varType(e.Type)
@@ -1607,7 +1607,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
 
        case *syntax.TypeSwitchGuard:
                // x.(type) expressions are handled explicitly in type switches
-               check.error(e, 0, invalidAST+"use of .(type) outside type switch")
+               check.error(e, InvalidSyntaxTree, invalidAST+"use of .(type) outside type switch")
                goto Error
 
        case *syntax.CallExpr:
@@ -1615,7 +1615,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
 
        case *syntax.ListExpr:
                // catch-all for unexpected expression lists
-               check.error(e, 0, invalidAST+"unexpected list of expressions")
+               check.error(e, InvalidSyntaxTree, invalidAST+"unexpected list of expressions")
                goto Error
 
        // case *syntax.UnaryExpr:
@@ -1692,7 +1692,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
 
        case *syntax.KeyValueExpr:
                // key:value expressions are handled in composite literals
-               check.error(e, 0, invalidAST+"no key:value expected")
+               check.error(e, InvalidSyntaxTree, invalidAST+"no key:value expected")
                goto Error
 
        case *syntax.ArrayType, *syntax.SliceType, *syntax.StructType, *syntax.FuncType,
index 71c4152ec508a315380189d3e2daa75a194f427c..0cf6072cabf6980f0539226d8bc8143e0c400eb7 100644 (file)
@@ -274,7 +274,7 @@ func (check *Checker) sliceExpr(x *operand, e *syntax.SliceExpr) {
 
        // spec: "Only the first index may be omitted; it defaults to 0."
        if e.Full && (e.Index[1] == nil || e.Index[2] == nil) {
-               check.error(e, 0, invalidAST+"2nd and 3rd index required in 3-index slice")
+               check.error(e, InvalidSyntaxTree, invalidAST+"2nd and 3rd index required in 3-index slice")
                x.mode = invalid
                return
        }
@@ -329,12 +329,12 @@ L:
 func (check *Checker) singleIndex(e *syntax.IndexExpr) syntax.Expr {
        index := e.Index
        if index == nil {
-               check.errorf(e, 0, invalidAST+"missing index for %s", e.X)
+               check.errorf(e, InvalidSyntaxTree, invalidAST+"missing index for %s", e.X)
                return nil
        }
        if l, _ := index.(*syntax.ListExpr); l != nil {
                if n := len(l.ElemList); n <= 1 {
-                       check.errorf(e, 0, invalidAST+"invalid use of ListExpr for index expression %v with %d indices", e, n)
+                       check.errorf(e, InvalidSyntaxTree, invalidAST+"invalid use of ListExpr for index expression %v with %d indices", e, n)
                        return nil
                }
                // len(l.ElemList) > 1
index b18900888bc73b42c3cb531f9327ae3d51cf7b90..6382ceedce263c3f9ab21d8eaf8aa5d01d596d2d 100644 (file)
@@ -143,7 +143,7 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
                sig, _ := typ.(*Signature)
                if sig == nil {
                        if typ != Typ[Invalid] {
-                               check.errorf(f.Type, 0, invalidAST+"%s is not a method signature", typ)
+                               check.errorf(f.Type, InvalidSyntaxTree, invalidAST+"%s is not a method signature", typ)
                        }
                        continue // ignore
                }
index 1f67e65abd79d324a97984523d80f3575c0a2c48..dd6f54ac05ac8be66c818753d8797573ad959b73 100644 (file)
@@ -219,7 +219,7 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab
                                }
 
                        default:
-                               check.errorf(s, 0, invalidAST+"branch statement: %s %s", s.Tok, name)
+                               check.errorf(s, InvalidSyntaxTree, invalidAST+"branch statement: %s %s", s.Tok, name)
                                return
                        }
 
index 2df74ae093de45dffad875a051f0915aaf8d7a11..122d5ec49c1064b9ffe53a5e78959f8783452992 100644 (file)
@@ -467,7 +467,7 @@ func (check *Checker) collectObjects() {
                                obj.setOrder(uint32(len(check.objMap)))
 
                        default:
-                               check.errorf(s, 0, invalidAST+"unknown syntax.Decl node %T", s)
+                               check.errorf(s, InvalidSyntaxTree, invalidAST+"unknown syntax.Decl node %T", s)
                        }
                }
        }
@@ -550,7 +550,7 @@ L: // unpack receiver type
                                case *syntax.BadExpr:
                                        // ignore - error already reported by parser
                                case nil:
-                                       check.error(ptyp, 0, invalidAST+"parameterized receiver contains nil parameters")
+                                       check.error(ptyp, InvalidSyntaxTree, invalidAST+"parameterized receiver contains nil parameters")
                                default:
                                        check.errorf(arg, BadDecl, "receiver type parameter %s must be an identifier", arg)
                                }
index eab18b6b7c09f2fdd2d5ba4c977e707be5a4bd8a..a6afc0ffe6a5ce5ae4ca3288f9c038f014b4c776 100644 (file)
@@ -289,7 +289,7 @@ func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, variadic
                        // named parameter
                        name := field.Name.Value
                        if name == "" {
-                               check.error(field.Name, 0, invalidAST+"anonymous parameter")
+                               check.error(field.Name, InvalidSyntaxTree, invalidAST+"anonymous parameter")
                                // ok to continue
                        }
                        par := NewParam(field.Name.Pos(), check.pkg, name, typ)
@@ -306,7 +306,7 @@ func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, variadic
        }
 
        if named && anonymous {
-               check.error(list[0], 0, invalidAST+"list contains both named and anonymous parameters")
+               check.error(list[0], InvalidSyntaxTree, invalidAST+"list contains both named and anonymous parameters")
                // ok to continue
        }
 
index c5f801e45fb7c051de5a3a7306e4faa4ac2f26f1..9dcb375e7a2d827b52c862d1536225e82d3225d9 100644 (file)
@@ -450,7 +450,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
                if s.Rhs == nil {
                        // x++ or x--
                        if len(lhs) != 1 {
-                               check.errorf(s, 0, invalidAST+"%s%s requires one operand", s.Op, s.Op)
+                               check.errorf(s, InvalidSyntaxTree, invalidAST+"%s%s requires one operand", s.Op, s.Op)
                                return
                        }
                        var x operand
@@ -554,7 +554,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
                        // goto's must have labels, should have been caught above
                        fallthrough
                default:
-                       check.errorf(s, 0, invalidAST+"branch statement: %s", s.Tok)
+                       check.errorf(s, InvalidSyntaxTree, invalidAST+"branch statement: %s", s.Tok)
                }
 
        case *syntax.BlockStmt:
@@ -582,7 +582,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
                case *syntax.IfStmt, *syntax.BlockStmt:
                        check.stmt(inner, s.Else)
                default:
-                       check.error(s.Else, 0, invalidAST+"invalid else branch in if statement")
+                       check.error(s.Else, InvalidSyntaxTree, invalidAST+"invalid else branch in if statement")
                }
 
        case *syntax.SwitchStmt:
@@ -674,7 +674,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
                check.stmt(inner, s.Body)
 
        default:
-               check.error(s, 0, invalidAST+"invalid statement")
+               check.error(s, InvalidSyntaxTree, invalidAST+"invalid statement")
        }
 }
 
@@ -710,7 +710,7 @@ func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
        seen := make(valueMap) // map of seen case values to positions and types
        for i, clause := range s.Body {
                if clause == nil {
-                       check.error(clause, 0, invalidAST+"incorrect expression switch case")
+                       check.error(clause, InvalidSyntaxTree, invalidAST+"incorrect expression switch case")
                        continue
                }
                end := s.Rbrace
@@ -773,7 +773,7 @@ func (check *Checker) typeSwitchStmt(inner stmtContext, s *syntax.SwitchStmt, gu
        seen := make(map[Type]syntax.Expr) // map of seen types to positions
        for i, clause := range s.Body {
                if clause == nil {
-                       check.error(s, 0, invalidAST+"incorrect type switch case")
+                       check.error(s, InvalidSyntaxTree, invalidAST+"incorrect type switch case")
                        continue
                }
                end := s.Rbrace
@@ -836,7 +836,7 @@ func (check *Checker) rangeStmt(inner stmtContext, s *syntax.ForStmt, rclause *s
        var sValue, sExtra syntax.Expr
        if p, _ := sKey.(*syntax.ListExpr); p != nil {
                if len(p.ElemList) < 2 {
-                       check.error(s, 0, invalidAST+"invalid lhs in range clause")
+                       check.error(s, InvalidSyntaxTree, invalidAST+"invalid lhs in range clause")
                        return
                }
                // len(p.ElemList) >= 2
@@ -918,7 +918,7 @@ func (check *Checker) rangeStmt(inner stmtContext, s *syntax.ForStmt, rclause *s
                                        vars = append(vars, obj)
                                }
                        } else {
-                               check.errorf(lhs, 0, invalidAST+"cannot declare %s", lhs)
+                               check.errorf(lhs, InvalidSyntaxTree, invalidAST+"cannot declare %s", lhs)
                                obj = NewVar(lhs.Pos(), check.pkg, "_", nil) // dummy variable
                        }
 
index 6d37710724c5bdaaf00310afda044cf099831cfc..ccf66d68cba1a00454894da013daa0817b53d2f1 100644 (file)
@@ -130,7 +130,7 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) {
                        pos := syntax.StartPos(f.Type)
                        name := embeddedFieldIdent(f.Type)
                        if name == nil {
-                               check.errorf(pos, 0, invalidAST+"invalid embedded field type %s", f.Type)
+                               check.errorf(pos, InvalidSyntaxTree, invalidAST+"invalid embedded field type %s", f.Type)
                                name = &syntax.Name{Value: "_"} // TODO(gri) need to set position to pos
                                addInvalid(name, pos)
                                continue
@@ -217,7 +217,7 @@ func (check *Checker) tag(t *syntax.BasicLit) string {
                                return val
                        }
                }
-               check.errorf(t, 0, invalidAST+"incorrect tag syntax: %q", t.Value)
+               check.errorf(t, InvalidSyntaxTree, invalidAST+"incorrect tag syntax: %q", t.Value)
        }
        return ""
 }
index a85e2d9ce0f1430968cb3c01f0f0177bc5cece99..cd75e6ed00a02f59511fe892db0b2b109a1f7375 100644 (file)
@@ -385,7 +385,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
                case syntax.RecvOnly:
                        dir = RecvOnly
                default:
-                       check.errorf(e, 0, invalidAST+"unknown channel direction %d", e.Dir)
+                       check.errorf(e, InvalidSyntaxTree, invalidAST+"unknown channel direction %d", e.Dir)
                        // ok to continue
                }
 
index a0fe55ac0d2de6073482392d29f2d48366b50adc..4d5acb10520c962f9fc4e71762c404fa466e97b2 100644 (file)
@@ -29,7 +29,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
                // ok
        default:
                // we may get here because of other problems (issue #39634, crash 12)
-               check.errorf(x, 0, "cannot assign %s to %s in %s", x, T, context)
+               // TODO(gri) do we need a new "generic" error code here?
+               check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context)
                return
        }
 
index 6fcbc1461a6e8143154a7b003045d03ff14c69ea..82d4533ee7cc7dcbff0d936d9efa7d09107dd3d4 100644 (file)
@@ -293,7 +293,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
        for _, a := range args {
                switch a.mode {
                case typexpr:
-                       check.errorf(a, 0, "%s used as value", a)
+                       check.errorf(a, NotAnExpr, "%s used as value", a)
                        return
                case invalid:
                        return
index 3f36bebc2cb6d864f9ca310b85952de2877d3dd3..bbcf4e6e75243dd64f6dd1fe4a984e36d84caf71 100644 (file)
@@ -220,6 +220,10 @@ func (check *Checker) report(errp *error_) {
                panic("empty error details")
        }
 
+       if errp.code == 0 {
+               panic("no error code provided")
+       }
+
        span := spanOf(errp.desc[0].posn)
        e := Error{
                Fset:       check.fset,
@@ -301,7 +305,7 @@ func (check *Checker) versionErrorf(at positioner, goVersion string, format stri
 }
 
 func (check *Checker) invalidAST(at positioner, format string, args ...any) {
-       check.errorf(at, 0, "invalid AST: "+format, args...)
+       check.errorf(at, InvalidSyntaxTree, "invalid AST: "+format, args...)
 }
 
 func (check *Checker) invalidArg(at positioner, code Code, format string, args ...any) {