Skip to content

Commit 388fbf2

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: use zero error code to indicate unset error code
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 <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 578523e commit 388fbf2

File tree

16 files changed

+42
-32
lines changed

16 files changed

+42
-32
lines changed

src/cmd/compile/internal/types2/assignments.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
2828
// ok
2929
default:
3030
// we may get here because of other problems (issue #39634, crash 12)
31-
check.errorf(x, 0, "cannot assign %s to %s in %s", x, T, context)
31+
// TODO(gri) do we need a new "generic" error code here?
32+
check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context)
3233
return
3334
}
3435

src/cmd/compile/internal/types2/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
287287
for _, a := range args {
288288
switch a.mode {
289289
case typexpr:
290-
check.errorf(a, 0, "%s used as value", a)
290+
check.errorf(a, NotAnExpr, "%s used as value", a)
291291
return
292292
case invalid:
293293
return

src/cmd/compile/internal/types2/decl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ func (check *Checker) declStmt(list []syntax.Decl) {
899899
check.pop().setColor(black)
900900

901901
default:
902-
check.errorf(s, 0, invalidAST+"unknown syntax.Decl node %T", s)
902+
check.errorf(s, InvalidSyntaxTree, invalidAST+"unknown syntax.Decl node %T", s)
903903
}
904904
}
905905
}

src/cmd/compile/internal/types2/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ func (check *Checker) dump(format string, args ...interface{}) {
221221
}
222222

223223
func (check *Checker) err(at poser, code Code, msg string, soft bool) {
224+
if code == 0 {
225+
panic("no error code provided")
226+
}
227+
224228
// Cheap trick: Don't report errors with messages containing
225229
// "invalid operand" or "invalid type" as those tend to be
226230
// follow-on errors which don't add useful information. Only

src/cmd/compile/internal/types2/expr.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (check *Checker) op(m opPredicates, x *operand, op syntax.Operator) bool {
7878
return false
7979
}
8080
} else {
81-
check.errorf(x, 0, invalidAST+"unknown operator %s", op)
81+
check.errorf(x, InvalidSyntaxTree, invalidAST+"unknown operator %s", op)
8282
return false
8383
}
8484
return true
@@ -1337,7 +1337,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
13371337
x.mode = value
13381338
x.typ = sig
13391339
} else {
1340-
check.errorf(e, 0, invalidAST+"invalid function literal %v", e)
1340+
check.errorf(e, InvalidSyntaxTree, invalidAST+"invalid function literal %v", e)
13411341
goto Error
13421342
}
13431343

@@ -1594,7 +1594,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
15941594
}
15951595
// x.(type) expressions are encoded via TypeSwitchGuards
15961596
if e.Type == nil {
1597-
check.error(e, 0, invalidAST+"invalid use of AssertExpr")
1597+
check.error(e, InvalidSyntaxTree, invalidAST+"invalid use of AssertExpr")
15981598
goto Error
15991599
}
16001600
T := check.varType(e.Type)
@@ -1607,15 +1607,15 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
16071607

16081608
case *syntax.TypeSwitchGuard:
16091609
// x.(type) expressions are handled explicitly in type switches
1610-
check.error(e, 0, invalidAST+"use of .(type) outside type switch")
1610+
check.error(e, InvalidSyntaxTree, invalidAST+"use of .(type) outside type switch")
16111611
goto Error
16121612

16131613
case *syntax.CallExpr:
16141614
return check.callExpr(x, e)
16151615

16161616
case *syntax.ListExpr:
16171617
// catch-all for unexpected expression lists
1618-
check.error(e, 0, invalidAST+"unexpected list of expressions")
1618+
check.error(e, InvalidSyntaxTree, invalidAST+"unexpected list of expressions")
16191619
goto Error
16201620

16211621
// case *syntax.UnaryExpr:
@@ -1692,7 +1692,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
16921692

16931693
case *syntax.KeyValueExpr:
16941694
// key:value expressions are handled in composite literals
1695-
check.error(e, 0, invalidAST+"no key:value expected")
1695+
check.error(e, InvalidSyntaxTree, invalidAST+"no key:value expected")
16961696
goto Error
16971697

16981698
case *syntax.ArrayType, *syntax.SliceType, *syntax.StructType, *syntax.FuncType,

src/cmd/compile/internal/types2/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (check *Checker) sliceExpr(x *operand, e *syntax.SliceExpr) {
274274

275275
// spec: "Only the first index may be omitted; it defaults to 0."
276276
if e.Full && (e.Index[1] == nil || e.Index[2] == nil) {
277-
check.error(e, 0, invalidAST+"2nd and 3rd index required in 3-index slice")
277+
check.error(e, InvalidSyntaxTree, invalidAST+"2nd and 3rd index required in 3-index slice")
278278
x.mode = invalid
279279
return
280280
}
@@ -329,12 +329,12 @@ L:
329329
func (check *Checker) singleIndex(e *syntax.IndexExpr) syntax.Expr {
330330
index := e.Index
331331
if index == nil {
332-
check.errorf(e, 0, invalidAST+"missing index for %s", e.X)
332+
check.errorf(e, InvalidSyntaxTree, invalidAST+"missing index for %s", e.X)
333333
return nil
334334
}
335335
if l, _ := index.(*syntax.ListExpr); l != nil {
336336
if n := len(l.ElemList); n <= 1 {
337-
check.errorf(e, 0, invalidAST+"invalid use of ListExpr for index expression %v with %d indices", e, n)
337+
check.errorf(e, InvalidSyntaxTree, invalidAST+"invalid use of ListExpr for index expression %v with %d indices", e, n)
338338
return nil
339339
}
340340
// len(l.ElemList) > 1

src/cmd/compile/internal/types2/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
143143
sig, _ := typ.(*Signature)
144144
if sig == nil {
145145
if typ != Typ[Invalid] {
146-
check.errorf(f.Type, 0, invalidAST+"%s is not a method signature", typ)
146+
check.errorf(f.Type, InvalidSyntaxTree, invalidAST+"%s is not a method signature", typ)
147147
}
148148
continue // ignore
149149
}

src/cmd/compile/internal/types2/labels.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (check *Checker) blockBranches(all *Scope, parent *block, lstmt *syntax.Lab
219219
}
220220

221221
default:
222-
check.errorf(s, 0, invalidAST+"branch statement: %s %s", s.Tok, name)
222+
check.errorf(s, InvalidSyntaxTree, invalidAST+"branch statement: %s %s", s.Tok, name)
223223
return
224224
}
225225

src/cmd/compile/internal/types2/resolver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (check *Checker) collectObjects() {
467467
obj.setOrder(uint32(len(check.objMap)))
468468

469469
default:
470-
check.errorf(s, 0, invalidAST+"unknown syntax.Decl node %T", s)
470+
check.errorf(s, InvalidSyntaxTree, invalidAST+"unknown syntax.Decl node %T", s)
471471
}
472472
}
473473
}
@@ -550,7 +550,7 @@ L: // unpack receiver type
550550
case *syntax.BadExpr:
551551
// ignore - error already reported by parser
552552
case nil:
553-
check.error(ptyp, 0, invalidAST+"parameterized receiver contains nil parameters")
553+
check.error(ptyp, InvalidSyntaxTree, invalidAST+"parameterized receiver contains nil parameters")
554554
default:
555555
check.errorf(arg, BadDecl, "receiver type parameter %s must be an identifier", arg)
556556
}

src/cmd/compile/internal/types2/signature.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, variadic
289289
// named parameter
290290
name := field.Name.Value
291291
if name == "" {
292-
check.error(field.Name, 0, invalidAST+"anonymous parameter")
292+
check.error(field.Name, InvalidSyntaxTree, invalidAST+"anonymous parameter")
293293
// ok to continue
294294
}
295295
par := NewParam(field.Name.Pos(), check.pkg, name, typ)
@@ -306,7 +306,7 @@ func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, variadic
306306
}
307307

308308
if named && anonymous {
309-
check.error(list[0], 0, invalidAST+"list contains both named and anonymous parameters")
309+
check.error(list[0], InvalidSyntaxTree, invalidAST+"list contains both named and anonymous parameters")
310310
// ok to continue
311311
}
312312

src/cmd/compile/internal/types2/stmt.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
450450
if s.Rhs == nil {
451451
// x++ or x--
452452
if len(lhs) != 1 {
453-
check.errorf(s, 0, invalidAST+"%s%s requires one operand", s.Op, s.Op)
453+
check.errorf(s, InvalidSyntaxTree, invalidAST+"%s%s requires one operand", s.Op, s.Op)
454454
return
455455
}
456456
var x operand
@@ -554,7 +554,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
554554
// goto's must have labels, should have been caught above
555555
fallthrough
556556
default:
557-
check.errorf(s, 0, invalidAST+"branch statement: %s", s.Tok)
557+
check.errorf(s, InvalidSyntaxTree, invalidAST+"branch statement: %s", s.Tok)
558558
}
559559

560560
case *syntax.BlockStmt:
@@ -582,7 +582,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
582582
case *syntax.IfStmt, *syntax.BlockStmt:
583583
check.stmt(inner, s.Else)
584584
default:
585-
check.error(s.Else, 0, invalidAST+"invalid else branch in if statement")
585+
check.error(s.Else, InvalidSyntaxTree, invalidAST+"invalid else branch in if statement")
586586
}
587587

588588
case *syntax.SwitchStmt:
@@ -674,7 +674,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
674674
check.stmt(inner, s.Body)
675675

676676
default:
677-
check.error(s, 0, invalidAST+"invalid statement")
677+
check.error(s, InvalidSyntaxTree, invalidAST+"invalid statement")
678678
}
679679
}
680680

@@ -710,7 +710,7 @@ func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
710710
seen := make(valueMap) // map of seen case values to positions and types
711711
for i, clause := range s.Body {
712712
if clause == nil {
713-
check.error(clause, 0, invalidAST+"incorrect expression switch case")
713+
check.error(clause, InvalidSyntaxTree, invalidAST+"incorrect expression switch case")
714714
continue
715715
}
716716
end := s.Rbrace
@@ -773,7 +773,7 @@ func (check *Checker) typeSwitchStmt(inner stmtContext, s *syntax.SwitchStmt, gu
773773
seen := make(map[Type]syntax.Expr) // map of seen types to positions
774774
for i, clause := range s.Body {
775775
if clause == nil {
776-
check.error(s, 0, invalidAST+"incorrect type switch case")
776+
check.error(s, InvalidSyntaxTree, invalidAST+"incorrect type switch case")
777777
continue
778778
}
779779
end := s.Rbrace
@@ -836,7 +836,7 @@ func (check *Checker) rangeStmt(inner stmtContext, s *syntax.ForStmt, rclause *s
836836
var sValue, sExtra syntax.Expr
837837
if p, _ := sKey.(*syntax.ListExpr); p != nil {
838838
if len(p.ElemList) < 2 {
839-
check.error(s, 0, invalidAST+"invalid lhs in range clause")
839+
check.error(s, InvalidSyntaxTree, invalidAST+"invalid lhs in range clause")
840840
return
841841
}
842842
// len(p.ElemList) >= 2
@@ -918,7 +918,7 @@ func (check *Checker) rangeStmt(inner stmtContext, s *syntax.ForStmt, rclause *s
918918
vars = append(vars, obj)
919919
}
920920
} else {
921-
check.errorf(lhs, 0, invalidAST+"cannot declare %s", lhs)
921+
check.errorf(lhs, InvalidSyntaxTree, invalidAST+"cannot declare %s", lhs)
922922
obj = NewVar(lhs.Pos(), check.pkg, "_", nil) // dummy variable
923923
}
924924

src/cmd/compile/internal/types2/struct.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (check *Checker) structType(styp *Struct, e *syntax.StructType) {
130130
pos := syntax.StartPos(f.Type)
131131
name := embeddedFieldIdent(f.Type)
132132
if name == nil {
133-
check.errorf(pos, 0, invalidAST+"invalid embedded field type %s", f.Type)
133+
check.errorf(pos, InvalidSyntaxTree, invalidAST+"invalid embedded field type %s", f.Type)
134134
name = &syntax.Name{Value: "_"} // TODO(gri) need to set position to pos
135135
addInvalid(name, pos)
136136
continue
@@ -217,7 +217,7 @@ func (check *Checker) tag(t *syntax.BasicLit) string {
217217
return val
218218
}
219219
}
220-
check.errorf(t, 0, invalidAST+"incorrect tag syntax: %q", t.Value)
220+
check.errorf(t, InvalidSyntaxTree, invalidAST+"incorrect tag syntax: %q", t.Value)
221221
}
222222
return ""
223223
}

src/cmd/compile/internal/types2/typexpr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
385385
case syntax.RecvOnly:
386386
dir = RecvOnly
387387
default:
388-
check.errorf(e, 0, invalidAST+"unknown channel direction %d", e.Dir)
388+
check.errorf(e, InvalidSyntaxTree, invalidAST+"unknown channel direction %d", e.Dir)
389389
// ok to continue
390390
}
391391

src/go/types/assignments.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
2929
// ok
3030
default:
3131
// we may get here because of other problems (issue #39634, crash 12)
32-
check.errorf(x, 0, "cannot assign %s to %s in %s", x, T, context)
32+
// TODO(gri) do we need a new "generic" error code here?
33+
check.errorf(x, IncompatibleAssign, "cannot assign %s to %s in %s", x, T, context)
3334
return
3435
}
3536

src/go/types/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
293293
for _, a := range args {
294294
switch a.mode {
295295
case typexpr:
296-
check.errorf(a, 0, "%s used as value", a)
296+
check.errorf(a, NotAnExpr, "%s used as value", a)
297297
return
298298
case invalid:
299299
return

src/go/types/errors.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ func (check *Checker) report(errp *error_) {
220220
panic("empty error details")
221221
}
222222

223+
if errp.code == 0 {
224+
panic("no error code provided")
225+
}
226+
223227
span := spanOf(errp.desc[0].posn)
224228
e := Error{
225229
Fset: check.fset,
@@ -301,7 +305,7 @@ func (check *Checker) versionErrorf(at positioner, goVersion string, format stri
301305
}
302306

303307
func (check *Checker) invalidAST(at positioner, format string, args ...any) {
304-
check.errorf(at, 0, "invalid AST: "+format, args...)
308+
check.errorf(at, InvalidSyntaxTree, "invalid AST: "+format, args...)
305309
}
306310

307311
func (check *Checker) invalidArg(at positioner, code Code, format string, args ...any) {

0 commit comments

Comments
 (0)