From 4d45c8502056aca783c84cb9f5824c3301490945 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 29 Dec 2014 15:47:06 -0500 Subject: [PATCH] go/types: expose IsInterface predicate, eliminating 6 copies Change-Id: I3704d7bd7a11f691c66556c1b77ef79a503d2fe9 Reviewed-on: https://go-review.googlesource.com/2173 Reviewed-by: Brad Fitzpatrick --- go/pointer/util.go | 6 +----- go/ssa/builder.go | 2 +- go/ssa/util.go | 10 +--------- go/types/assignments.go | 2 +- go/types/call.go | 2 +- go/types/conversions.go | 2 +- go/types/predicates.go | 3 ++- godoc/analysis/typeinfo.go | 5 +---- oracle/implements.go | 5 +---- refactor/rename/check.go | 5 +---- refactor/satisfy/find.go | 5 +---- 11 files changed, 12 insertions(+), 35 deletions(-) diff --git a/go/pointer/util.go b/go/pointer/util.go index 88bada89..d4ccbb59 100644 --- a/go/pointer/util.go +++ b/go/pointer/util.go @@ -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. diff --git a/go/ssa/builder.go b/go/ssa/builder.go index 1f9511e4..93755d00 100644 --- a/go/ssa/builder.go +++ b/go/ssa/builder.go @@ -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) diff --git a/go/ssa/util.go b/go/ssa/util.go index cd476d86..10ebb8cd 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -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 { diff --git a/go/types/assignments.go b/go/types/assignments.go index 7ee1abcc..14ee286b 100644 --- a/go/types/assignments.go +++ b/go/types/assignments.go @@ -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 diff --git a/go/types/call.go b/go/types/call.go index 5811a12b..7f366a8c 100644 --- a/go/types/call.go +++ b/go/types/call.go @@ -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} } } diff --git a/go/types/conversions.go b/go/types/conversions.go index 32cc8220..f7b2a567 100644 --- a/go/types/conversions.go +++ b/go/types/conversions.go @@ -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) diff --git a/go/types/predicates.go b/go/types/predicates.go index 2e36a729..b5c39d9d 100644 --- a/go/types/predicates.go +++ b/go/types/predicates.go @@ -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 } diff --git a/godoc/analysis/typeinfo.go b/godoc/analysis/typeinfo.go index 82f2f29d..bd1b0c1d 100644 --- a/godoc/analysis/typeinfo.go +++ b/godoc/analysis/typeinfo.go @@ -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 { diff --git a/oracle/implements.go b/oracle/implements.go index d07a02c3..8e54dea8 100644 --- a/oracle/implements.go +++ b/oracle/implements.go @@ -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 diff --git a/refactor/rename/check.go b/refactor/rename/check.go index 324d0994..017a6046 100644 --- a/refactor/rename/check.go +++ b/refactor/rename/check.go @@ -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 { diff --git a/refactor/satisfy/find.go b/refactor/satisfy/find.go index 9770a02b..20fb2889 100644 --- a/refactor/satisfy/find.go +++ b/refactor/satisfy/find.go @@ -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) }