/go/gotour/src/code.google.com/p/go.tools/go/types/errors.go
Go | 96 lines | 76 code | 15 blank | 5 comment | 7 complexity | 7d47272f7c4f5bda2c349e4904193484 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, Apache-2.0, MIT
- // Copyright 2012 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- // This file implements various error reporters.
- package types
- import (
- "fmt"
- "go/ast"
- "go/token"
- "strings"
- )
- func assert(p bool) {
- if !p {
- panic("assertion failed")
- }
- }
- func unreachable() {
- panic("unreachable")
- }
- func (check *checker) sprintf(format string, args ...interface{}) string {
- for i, arg := range args {
- switch a := arg.(type) {
- case nil:
- arg = "<nil>"
- case operand:
- panic("internal error: should always pass *operand")
- case *operand:
- arg = operandString(check.pkg, a)
- case token.Pos:
- arg = check.fset.Position(a).String()
- case ast.Expr:
- arg = ExprString(a)
- case Object:
- arg = ObjectString(check.pkg, a)
- case Type:
- arg = TypeString(check.pkg, a)
- }
- args[i] = arg
- }
- return fmt.Sprintf(format, args...)
- }
- func (check *checker) trace(pos token.Pos, format string, args ...interface{}) {
- fmt.Printf("%s:\t%s%s\n",
- check.fset.Position(pos),
- strings.Repeat(". ", check.indent),
- check.sprintf(format, args...),
- )
- }
- // dump is only needed for debugging
- func (check *checker) dump(format string, args ...interface{}) {
- fmt.Println(check.sprintf(format, args...))
- }
- func (check *checker) err(pos token.Pos, msg string, soft bool) {
- err := Error{check.fset, pos, msg, soft}
- if check.firstErr == nil {
- check.firstErr = err
- }
- f := check.conf.Error
- if f == nil {
- panic(bailout{}) // report only first error
- }
- f(err)
- }
- func (check *checker) error(pos token.Pos, msg string) {
- check.err(pos, msg, false)
- }
- func (check *checker) errorf(pos token.Pos, format string, args ...interface{}) {
- check.err(pos, check.sprintf(format, args...), false)
- }
- func (check *checker) softErrorf(pos token.Pos, format string, args ...interface{}) {
- check.err(pos, check.sprintf(format, args...), true)
- }
- func (check *checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
- check.errorf(pos, "invalid AST: "+format, args...)
- }
- func (check *checker) invalidArg(pos token.Pos, format string, args ...interface{}) {
- check.errorf(pos, "invalid argument: "+format, args...)
- }
- func (check *checker) invalidOp(pos token.Pos, format string, args ...interface{}) {
- check.errorf(pos, "invalid operation: "+format, args...)
- }