diff options
author | Alan Donovan <adonovan@google.com> | 2014-12-29 15:47:06 -0500 |
---|---|---|
committer | Alan Donovan <adonovan@google.com> | 2015-01-21 18:49:27 +0000 |
commit | 4d45c8502056aca783c84cb9f5824c3301490945 (patch) | |
tree | ab0751a0980a24f35d828e247972e84a6b15c864 | |
parent | d702aaaabeb8e868ad03c3514a593da7673453d1 (diff) | |
download | tools-4d45c8502056aca783c84cb9f5824c3301490945.tar.gz |
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>
-rw-r--r-- | go/pointer/util.go | 6 | ||||
-rw-r--r-- | go/ssa/builder.go | 2 | ||||
-rw-r--r-- | go/ssa/util.go | 10 | ||||
-rw-r--r-- | go/types/assignments.go | 2 | ||||
-rw-r--r-- | go/types/call.go | 2 | ||||
-rw-r--r-- | go/types/conversions.go | 2 | ||||
-rw-r--r-- | go/types/predicates.go | 3 | ||||
-rw-r--r-- | godoc/analysis/typeinfo.go | 5 | ||||
-rw-r--r-- | oracle/implements.go | 5 | ||||
-rw-r--r-- | refactor/rename/check.go | 5 | ||||
-rw-r--r-- | 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 88bada8..d4ccbb5 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 1f9511e..93755d0 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 cd476d8..10ebb8c 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 7ee1abc..14ee286 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 5811a12..7f366a8 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 32cc822..f7b2a56 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 2e36a72..b5c39d9 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 82f2f29..bd1b0c1 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 d07a02c..8e54dea 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 324d099..017a604 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 9770a02..20fb288 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) } |