зеркало из https://github.com/golang/tools.git
go/types: expose IsInterface predicate, eliminating 6 copies
Change-Id: I3704d7bd7a11f691c66556c1b77ef79a503d2fe9 Reviewed-on: https://go-review.googlesource.com/2173 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Родитель
d702aaaabe
Коммит
4d45c85020
|
@ -50,11 +50,7 @@ func CanHaveDynamicTypes(T types.Type) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// isInterface reports whether T is an interface type.
|
||||
func isInterface(T types.Type) bool {
|
||||
_, ok := T.Underlying().(*types.Interface)
|
||||
return ok
|
||||
}
|
||||
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
||||
|
||||
// mustDeref returns the element type of its argument, which must be a
|
||||
// pointer; panic ensues otherwise.
|
||||
|
|
|
@ -600,7 +600,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
|
|||
case *types.Basic, *types.Slice, *types.Pointer: // *array
|
||||
x = b.expr(fn, e.X)
|
||||
default:
|
||||
unreachable()
|
||||
panic("unreachable")
|
||||
}
|
||||
if e.High != nil {
|
||||
high = b.expr(fn, e.High)
|
||||
|
|
|
@ -17,10 +17,6 @@ import (
|
|||
"golang.org/x/tools/go/types"
|
||||
)
|
||||
|
||||
func unreachable() {
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
//// AST utilities
|
||||
|
||||
func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
|
||||
|
@ -41,11 +37,7 @@ func isPointer(typ types.Type) bool {
|
|||
return ok
|
||||
}
|
||||
|
||||
// isInterface reports whether T's underlying type is an interface.
|
||||
func isInterface(T types.Type) bool {
|
||||
_, ok := T.Underlying().(*types.Interface)
|
||||
return ok
|
||||
}
|
||||
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
||||
|
||||
// deref returns a pointer's element type; otherwise it returns typ.
|
||||
func deref(typ types.Type) types.Type {
|
||||
|
|
|
@ -45,7 +45,7 @@ func (check *Checker) assignment(x *operand, T Type) bool {
|
|||
// bool, rune, int, float64, complex128 or string respectively, depending
|
||||
// on whether the value is a boolean, rune, integer, floating-point, complex,
|
||||
// or string constant."
|
||||
if T == nil || isInterface(T) {
|
||||
if T == nil || IsInterface(T) {
|
||||
if T == nil && x.typ == Typ[UntypedNil] {
|
||||
check.errorf(x.pos(), "use of untyped nil")
|
||||
x.mode = invalid
|
||||
|
|
|
@ -397,7 +397,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
|
|||
// includes the methods of typ.
|
||||
// Variables are addressable, so we can always take their
|
||||
// address.
|
||||
if _, ok := typ.(*Pointer); !ok && !isInterface(typ) {
|
||||
if _, ok := typ.(*Pointer); !ok && !IsInterface(typ) {
|
||||
typ = &Pointer{base: typ}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ func (check *Checker) conversion(x *operand, T Type) {
|
|||
// use the default type (e.g., []byte("foo") should report string
|
||||
// not []byte as type for the constant "foo").
|
||||
// - Keep untyped nil for untyped nil arguments.
|
||||
if isInterface(T) || constArg && !isConstType(T) {
|
||||
if IsInterface(T) || constArg && !isConstType(T) {
|
||||
final = defaultType(x.typ)
|
||||
}
|
||||
check.updateExprType(x.expr, final, true)
|
||||
|
|
|
@ -71,7 +71,8 @@ func isConstType(typ Type) bool {
|
|||
return ok && t.info&IsConstType != 0
|
||||
}
|
||||
|
||||
func isInterface(typ Type) bool {
|
||||
// IsInterface reports whether typ is an interface type.
|
||||
func IsInterface(typ Type) bool {
|
||||
_, ok := typ.Underlying().(*Interface)
|
||||
return ok
|
||||
}
|
||||
|
|
|
@ -215,10 +215,7 @@ func (a *analysis) namedType(obj *types.TypeName, implements map[*types.Named]im
|
|||
|
||||
// -- utilities --------------------------------------------------------
|
||||
|
||||
func isInterface(T types.Type) bool {
|
||||
_, isI := T.Underlying().(*types.Interface)
|
||||
return isI
|
||||
}
|
||||
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
||||
|
||||
// deref returns a pointer's element type; otherwise it returns typ.
|
||||
func deref(typ types.Type) types.Type {
|
||||
|
|
|
@ -191,10 +191,7 @@ func typeKind(T types.Type) string {
|
|||
return strings.ToLower(strings.TrimPrefix(s, "*types."))
|
||||
}
|
||||
|
||||
func isInterface(T types.Type) bool {
|
||||
_, isI := T.Underlying().(*types.Interface)
|
||||
return isI
|
||||
}
|
||||
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
||||
|
||||
type typesByString []types.Type
|
||||
|
||||
|
|
|
@ -727,10 +727,7 @@ func someUse(info *loader.PackageInfo, obj types.Object) *ast.Ident {
|
|||
|
||||
// -- Plundered from golang.org/x/tools/go/ssa -----------------
|
||||
|
||||
func isInterface(T types.Type) bool {
|
||||
_, ok := T.Underlying().(*types.Interface)
|
||||
return ok
|
||||
}
|
||||
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
||||
|
||||
func deref(typ types.Type) types.Type {
|
||||
if p, _ := typ.(*types.Pointer); p != nil {
|
||||
|
|
|
@ -701,7 +701,4 @@ func deref(typ types.Type) types.Type {
|
|||
|
||||
func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
|
||||
|
||||
func isInterface(T types.Type) bool {
|
||||
_, ok := T.Underlying().(*types.Interface)
|
||||
return ok
|
||||
}
|
||||
func isInterface(T types.Type) bool { return types.IsInterface(T) }
|
||||
|
|
Загрузка…
Ссылка в новой задаче