"fmt"
"go/ast"
"go/token"
+ . "internal/types/errors"
+ "runtime"
"strconv"
"strings"
)
func assert(p bool) {
if !p {
- panic("assertion failed")
+ msg := "assertion failed"
+ // Include information about the assertion location. Due to panic recovery,
+ // this location is otherwise buried in the middle of the panicking stack.
+ if _, file, line, ok := runtime.Caller(1); ok {
+ msg = fmt.Sprintf("%s:%d: %s", file, line, msg)
+ }
+ panic(msg)
}
}
panic("unreachable")
}
+// An error_ represents a type-checking error.
+// To report an error_, call Checker.report.
+type error_ struct {
+ desc []errorDesc
+ code Code
+ soft bool // TODO(gri) eventually determine this from an error code
+}
+
+// An errorDesc describes part of a type-checking error.
+type errorDesc struct {
+ posn positioner
+ format string
+ args []interface{}
+}
+
+func (err *error_) empty() bool {
+ return err.desc == nil
+}
+
+func (err *error_) pos() token.Pos {
+ if err.empty() {
+ return nopos
+ }
+ return err.desc[0].posn.Pos()
+}
+
+func (err *error_) msg(fset *token.FileSet, qf Qualifier) string {
+ if err.empty() {
+ return "no error"
+ }
+ var buf strings.Builder
+ for i := range err.desc {
+ p := &err.desc[i]
+ if i > 0 {
+ fmt.Fprint(&buf, "\n\t")
+ if p.posn.Pos().IsValid() {
+ fmt.Fprintf(&buf, "%s: ", fset.Position(p.posn.Pos()))
+ }
+ }
+ buf.WriteString(sprintf(fset, qf, false, p.format, p.args...))
+ }
+ return buf.String()
+}
+
+// String is for testing.
+func (err *error_) String() string {
+ if err.empty() {
+ return "no error"
+ }
+ return fmt.Sprintf("%d: %s", err.pos(), err.msg(nil, nil))
+}
+
+// errorf adds formatted error information to err.
+// It may be called multiple times to provide additional information.
+func (err *error_) errorf(at token.Pos, format string, args ...interface{}) {
+ err.desc = append(err.desc, errorDesc{atPos(at), format, args})
+}
+
func (check *Checker) qualifier(pkg *Package) string {
// Qualify the package unless it's the package being type-checked.
if pkg != check.pkg {
return sprintf(fset, qf, false, format, args...)
}
-func sprintf(fset *token.FileSet, qf Qualifier, debug bool, format string, args ...any) string {
+func sprintf(fset *token.FileSet, qf Qualifier, tpSubscripts bool, format string, args ...any) string {
for i, arg := range args {
switch a := arg.(type) {
case nil:
case Object:
arg = ObjectString(a, qf)
case Type:
- arg = typeString(a, qf, debug)
+ var buf bytes.Buffer
+ w := newTypeWriter(&buf, qf)
+ w.tpSubscripts = tpSubscripts
+ w.typ(a)
+ arg = buf.String()
case []Type:
var buf bytes.Buffer
+ w := newTypeWriter(&buf, qf)
+ w.tpSubscripts = tpSubscripts
buf.WriteByte('[')
for i, x := range a {
if i > 0 {
buf.WriteString(", ")
}
- buf.WriteString(typeString(x, qf, debug))
+ w.typ(x)
}
buf.WriteByte(']')
arg = buf.String()
case []*TypeParam:
var buf bytes.Buffer
+ w := newTypeWriter(&buf, qf)
+ w.tpSubscripts = tpSubscripts
buf.WriteByte('[')
for i, x := range a {
if i > 0 {
buf.WriteString(", ")
}
- buf.WriteString(typeString(x, qf, debug)) // use typeString so we get subscripts when debugging
+ w.typ(x)
}
buf.WriteByte(']')
arg = buf.String()
// Report records the error pointed to by errp, setting check.firstError if
// necessary.
-func (check *Checker) report(errp *Error) {
- e := *errp
+func (check *Checker) report(errp *error_) {
+ if errp.empty() {
+ panic("empty error details")
+ }
+
+ msg := errp.msg(check.fset, check.qualifier)
+ switch errp.code {
+ case InvalidSyntaxTree:
+ msg = "invalid AST: " + msg
+ case 0:
+ panic("no error code provided")
+ }
+
+ // If we have a URL for error codes, add a link to the first line.
+ if errp.code != 0 && check.conf._ErrorURL != "" {
+ u := fmt.Sprintf(check.conf._ErrorURL, errp.code)
+ if i := strings.Index(msg, "\n"); i >= 0 {
+ msg = msg[:i] + u + msg[i:]
+ } else {
+ msg += u
+ }
+ }
+
+ span := spanOf(errp.desc[0].posn)
+ e := Error{
+ Fset: check.fset,
+ Pos: span.pos,
+ Msg: msg,
+ Soft: errp.soft,
+ go116code: errp.code,
+ go116start: span.start,
+ go116end: span.end,
+ }
+
// 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
check.firstErr = err
}
- if trace {
+ if check.conf._Trace {
pos := e.Pos
msg := e.Msg
check.trace(pos, "ERROR: %s", msg)
f(err)
}
-func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) *Error {
- span := spanOf(at)
- return &Error{
- Fset: check.fset,
- Pos: span.pos,
- Msg: msg,
- Soft: soft,
- go116code: code,
- go116start: span.start,
- go116end: span.end,
- }
-}
-
-// newErrorf creates a new Error, but does not handle it.
-func (check *Checker) newErrorf(at positioner, code errorCode, soft bool, format string, args ...any) *Error {
- msg := check.sprintf(format, args...)
- return check.newError(at, code, soft, msg)
-}
-
-func (check *Checker) error(at positioner, code errorCode, msg string) {
- check.report(check.newError(at, code, false, msg))
-}
+const (
+ invalidArg = "invalid argument: "
+ invalidOp = "invalid operation: "
+)
-func (check *Checker) errorf(at positioner, code errorCode, format string, args ...any) {
- check.error(at, code, check.sprintf(format, args...))
+// newErrorf creates a new error_ for later reporting with check.report.
+func newErrorf(at positioner, code Code, format string, args ...any) *error_ {
+ return &error_{
+ desc: []errorDesc{{at, format, args}},
+ code: code,
+ }
}
-func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...any) {
- check.report(check.newErrorf(at, code, true, format, args...))
+func (check *Checker) error(at positioner, code Code, msg string) {
+ check.report(newErrorf(at, code, "%s", msg))
}
-func (check *Checker) invalidAST(at positioner, format string, args ...any) {
- check.errorf(at, 0, "invalid AST: "+format, args...)
+func (check *Checker) errorf(at positioner, code Code, format string, args ...any) {
+ check.report(newErrorf(at, code, format, args...))
}
-func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...any) {
- check.errorf(at, code, "invalid argument: "+format, args...)
+func (check *Checker) softErrorf(at positioner, code Code, format string, args ...any) {
+ err := newErrorf(at, code, format, args...)
+ err.soft = true
+ check.report(err)
}
-func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...any) {
- check.errorf(at, code, "invalid operation: "+format, args...)
+func (check *Checker) versionErrorf(at positioner, v goVersion, format string, args ...interface{}) {
+ msg := check.sprintf(format, args...)
+ var err *error_
+ err = newErrorf(at, UnsupportedFeature, "%s requires %s or later", msg, v)
+ check.report(err)
}
// The positioner interface is used to extract the position of type-checker
pos := x.Pos()
return posSpan{pos, pos, x.expr.End()}
}
- return posSpan{token.NoPos, token.NoPos, token.NoPos}
+ return posSpan{nopos, nopos, nopos}
default:
pos := at.Pos()
return posSpan{pos, pos, pos}
// stripAnnotations removes internal (type) annotations from s.
func stripAnnotations(s string) string {
- var b strings.Builder
+ var buf strings.Builder
for _, r := range s {
// strip #'s and subscript digits
if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
- b.WriteRune(r)
+ buf.WriteRune(r)
}
}
- if b.Len() < len(s) {
- return b.String()
+ if buf.Len() < len(s) {
+ return buf.String()
}
return s
}