Source file src/cmd/vendor/golang.org/x/tools/refactor/satisfy/find.go

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package satisfy inspects the type-checked ASTs of Go packages and
     6  // reports the set of discovered type constraints of the form (lhs, rhs
     7  // Type) where lhs is a non-trivial interface, rhs satisfies this
     8  // interface, and this fact is necessary for the package to be
     9  // well-typed.
    10  //
    11  // It requires well-typed inputs.
    12  package satisfy // import "golang.org/x/tools/refactor/satisfy"
    13  
    14  // NOTES:
    15  //
    16  // We don't care about numeric conversions, so we don't descend into
    17  // types or constant expressions.  This is unsound because
    18  // constant expressions can contain arbitrary statements, e.g.
    19  //   const x = len([1]func(){func() {
    20  //     ...
    21  //   }})
    22  //
    23  // Assignability conversions are possible in the following places:
    24  // - in assignments y = x, y := x, var y = x.
    25  // - from call argument types to formal parameter types
    26  // - in append and delete calls
    27  // - from return operands to result parameter types
    28  // - in composite literal T{k:v}, from k and v to T's field/element/key type
    29  // - in map[key] from key to the map's key type
    30  // - in comparisons x==y and switch x { case y: }.
    31  // - in explicit conversions T(x)
    32  // - in sends ch <- x, from x to the channel element type
    33  // - in type assertions x.(T) and switch x.(type) { case T: }
    34  //
    35  // The results of this pass provide information equivalent to the
    36  // ssa.MakeInterface and ssa.ChangeInterface instructions.
    37  
    38  import (
    39  	"fmt"
    40  	"go/ast"
    41  	"go/token"
    42  	"go/types"
    43  
    44  	"golang.org/x/tools/go/types/typeutil"
    45  	"golang.org/x/tools/internal/typeparams"
    46  )
    47  
    48  // A Constraint records the fact that the RHS type does and must
    49  // satisfy the LHS type, which is an interface.
    50  // The names are suggestive of an assignment statement LHS = RHS.
    51  //
    52  // The constraint is implicitly universally quantified over any type
    53  // parameters appearing within the two types.
    54  type Constraint struct {
    55  	LHS, RHS types.Type
    56  }
    57  
    58  // A Finder inspects the type-checked ASTs of Go packages and
    59  // accumulates the set of type constraints (x, y) such that x is
    60  // assignable to y, y is an interface, and both x and y have methods.
    61  //
    62  // In other words, it returns the subset of the "implements" relation
    63  // that is checked during compilation of a package.  Refactoring tools
    64  // will need to preserve at least this part of the relation to ensure
    65  // continued compilation.
    66  type Finder struct {
    67  	Result    map[Constraint]bool
    68  	msetcache typeutil.MethodSetCache
    69  
    70  	// per-Find state
    71  	info *types.Info
    72  	sig  *types.Signature
    73  }
    74  
    75  // Find inspects a single package, populating Result with its pairs of
    76  // constrained types.
    77  //
    78  // The result is non-canonical and thus may contain duplicates (but this
    79  // tends to preserves names of interface types better).
    80  //
    81  // The package must be free of type errors, and
    82  // info.{Defs,Uses,Selections,Types} must have been populated by the
    83  // type-checker.
    84  func (f *Finder) Find(info *types.Info, files []*ast.File) {
    85  	if info.Defs == nil || info.Uses == nil || info.Selections == nil || info.Types == nil {
    86  		panic("Finder.Find: one of info.{Defs,Uses,Selections.Types} is not populated")
    87  	}
    88  	if f.Result == nil {
    89  		f.Result = make(map[Constraint]bool)
    90  	}
    91  
    92  	f.info = info
    93  	for _, file := range files {
    94  		for _, d := range file.Decls {
    95  			switch d := d.(type) {
    96  			case *ast.GenDecl:
    97  				if d.Tok == token.VAR { // ignore consts
    98  					for _, spec := range d.Specs {
    99  						f.valueSpec(spec.(*ast.ValueSpec))
   100  					}
   101  				}
   102  
   103  			case *ast.FuncDecl:
   104  				if d.Body != nil {
   105  					f.sig = f.info.Defs[d.Name].Type().(*types.Signature)
   106  					f.stmt(d.Body)
   107  					f.sig = nil
   108  				}
   109  			}
   110  		}
   111  	}
   112  	f.info = nil
   113  }
   114  
   115  var (
   116  	tInvalid     = types.Typ[types.Invalid]
   117  	tUntypedBool = types.Typ[types.UntypedBool]
   118  	tUntypedNil  = types.Typ[types.UntypedNil]
   119  )
   120  
   121  // exprN visits an expression in a multi-value context.
   122  func (f *Finder) exprN(e ast.Expr) types.Type {
   123  	typ := f.info.Types[e].Type.(*types.Tuple)
   124  	switch e := e.(type) {
   125  	case *ast.ParenExpr:
   126  		return f.exprN(e.X)
   127  
   128  	case *ast.CallExpr:
   129  		// x, err := f(args)
   130  		sig := typeparams.CoreType(f.expr(e.Fun)).(*types.Signature)
   131  		f.call(sig, e.Args)
   132  
   133  	case *ast.IndexExpr:
   134  		// y, ok := x[i]
   135  		x := f.expr(e.X)
   136  		f.assign(f.expr(e.Index), typeparams.CoreType(x).(*types.Map).Key())
   137  
   138  	case *ast.TypeAssertExpr:
   139  		// y, ok := x.(T)
   140  		f.typeAssert(f.expr(e.X), typ.At(0).Type())
   141  
   142  	case *ast.UnaryExpr: // must be receive <-
   143  		// y, ok := <-x
   144  		f.expr(e.X)
   145  
   146  	default:
   147  		panic(e)
   148  	}
   149  	return typ
   150  }
   151  
   152  func (f *Finder) call(sig *types.Signature, args []ast.Expr) {
   153  	if len(args) == 0 {
   154  		return
   155  	}
   156  
   157  	// Ellipsis call?  e.g. f(x, y, z...)
   158  	if _, ok := args[len(args)-1].(*ast.Ellipsis); ok {
   159  		for i, arg := range args {
   160  			// The final arg is a slice, and so is the final param.
   161  			f.assign(sig.Params().At(i).Type(), f.expr(arg))
   162  		}
   163  		return
   164  	}
   165  
   166  	var argtypes []types.Type
   167  
   168  	// Gather the effective actual parameter types.
   169  	if tuple, ok := f.info.Types[args[0]].Type.(*types.Tuple); ok {
   170  		// f(g()) call where g has multiple results?
   171  		f.expr(args[0])
   172  		// unpack the tuple
   173  		for v := range tuple.Variables() {
   174  			argtypes = append(argtypes, v.Type())
   175  		}
   176  	} else {
   177  		for _, arg := range args {
   178  			argtypes = append(argtypes, f.expr(arg))
   179  		}
   180  	}
   181  
   182  	// Assign the actuals to the formals.
   183  	if !sig.Variadic() {
   184  		for i, argtype := range argtypes {
   185  			f.assign(sig.Params().At(i).Type(), argtype)
   186  		}
   187  	} else {
   188  		// The first n-1 parameters are assigned normally.
   189  		nnormals := sig.Params().Len() - 1
   190  		for i, argtype := range argtypes[:nnormals] {
   191  			f.assign(sig.Params().At(i).Type(), argtype)
   192  		}
   193  		// Remaining args are assigned to elements of varargs slice.
   194  		tElem := sig.Params().At(nnormals).Type().(*types.Slice).Elem()
   195  		for i := nnormals; i < len(argtypes); i++ {
   196  			f.assign(tElem, argtypes[i])
   197  		}
   198  	}
   199  }
   200  
   201  // builtin visits the arguments of a builtin type with signature sig.
   202  func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Expr) {
   203  	switch obj.Name() {
   204  	case "make", "new":
   205  		for i, arg := range args {
   206  			if i == 0 && f.info.Types[arg].IsType() {
   207  				continue // skip the type operand
   208  			}
   209  			f.expr(arg)
   210  		}
   211  
   212  	case "append":
   213  		s := f.expr(args[0])
   214  		if _, ok := args[len(args)-1].(*ast.Ellipsis); ok && len(args) == 2 {
   215  			// append(x, y...)   including append([]byte, "foo"...)
   216  			f.expr(args[1])
   217  		} else {
   218  			// append(x, y, z)
   219  			tElem := typeparams.CoreType(s).(*types.Slice).Elem()
   220  			for _, arg := range args[1:] {
   221  				f.assign(tElem, f.expr(arg))
   222  			}
   223  		}
   224  
   225  	case "delete":
   226  		m := f.expr(args[0])
   227  		k := f.expr(args[1])
   228  		f.assign(typeparams.CoreType(m).(*types.Map).Key(), k)
   229  
   230  	default:
   231  		// ordinary call
   232  		f.call(sig, args)
   233  	}
   234  }
   235  
   236  func (f *Finder) extract(tuple types.Type, i int) types.Type {
   237  	if tuple, ok := tuple.(*types.Tuple); ok && i < tuple.Len() {
   238  		return tuple.At(i).Type()
   239  	}
   240  	return tInvalid
   241  }
   242  
   243  func (f *Finder) valueSpec(spec *ast.ValueSpec) {
   244  	var T types.Type
   245  	if spec.Type != nil {
   246  		T = f.info.Types[spec.Type].Type
   247  	}
   248  	switch len(spec.Values) {
   249  	case len(spec.Names): // e.g. var x, y = f(), g()
   250  		for _, value := range spec.Values {
   251  			v := f.expr(value)
   252  			if T != nil {
   253  				f.assign(T, v)
   254  			}
   255  		}
   256  
   257  	case 1: // e.g. var x, y = f()
   258  		tuple := f.exprN(spec.Values[0])
   259  		for i := range spec.Names {
   260  			if T != nil {
   261  				f.assign(T, f.extract(tuple, i))
   262  			}
   263  		}
   264  	}
   265  }
   266  
   267  // assign records pairs of distinct types that are related by
   268  // assignability, where the left-hand side is an interface and both
   269  // sides have methods.
   270  //
   271  // It should be called for all assignability checks, type assertions,
   272  // explicit conversions and comparisons between two types, unless the
   273  // types are uninteresting (e.g. lhs is a concrete type, or the empty
   274  // interface; rhs has no methods).
   275  func (f *Finder) assign(lhs, rhs types.Type) {
   276  	if types.Identical(lhs, rhs) {
   277  		return
   278  	}
   279  	if !types.IsInterface(lhs) {
   280  		return
   281  	}
   282  
   283  	if f.msetcache.MethodSet(lhs).Len() == 0 {
   284  		return
   285  	}
   286  	if f.msetcache.MethodSet(rhs).Len() == 0 {
   287  		return
   288  	}
   289  	// record the pair
   290  	f.Result[Constraint{lhs, rhs}] = true
   291  }
   292  
   293  // typeAssert must be called for each type assertion x.(T) where x has
   294  // interface type I.
   295  func (f *Finder) typeAssert(I, T types.Type) {
   296  	// Type assertions are slightly subtle, because they are allowed
   297  	// to be "impossible", e.g.
   298  	//
   299  	// 	var x interface{f()}
   300  	//	_ = x.(interface{f()int}) // legal
   301  	//
   302  	// (In hindsight, the language spec should probably not have
   303  	// allowed this, but it's too late to fix now.)
   304  	//
   305  	// This means that a type assert from I to T isn't exactly a
   306  	// constraint that T is assignable to I, but for a refactoring
   307  	// tool it is a conditional constraint that, if T is assignable
   308  	// to I before a refactoring, it should remain so after.
   309  
   310  	if types.AssignableTo(T, I) {
   311  		f.assign(I, T)
   312  	}
   313  }
   314  
   315  // compare must be called for each comparison x==y.
   316  func (f *Finder) compare(x, y types.Type) {
   317  	if types.AssignableTo(x, y) {
   318  		f.assign(y, x)
   319  	} else if types.AssignableTo(y, x) {
   320  		f.assign(x, y)
   321  	}
   322  }
   323  
   324  // expr visits a true expression (not a type or defining ident)
   325  // and returns its type.
   326  func (f *Finder) expr(e ast.Expr) types.Type {
   327  	tv := f.info.Types[e]
   328  	if tv.Value != nil {
   329  		return tv.Type // prune the descent for constants
   330  	}
   331  
   332  	// tv.Type may be nil for an ast.Ident.
   333  
   334  	switch e := e.(type) {
   335  	case *ast.BadExpr, *ast.BasicLit:
   336  		// no-op
   337  
   338  	case *ast.Ident:
   339  		// (referring idents only)
   340  		if obj, ok := f.info.Uses[e]; ok {
   341  			return obj.Type()
   342  		}
   343  		if e.Name == "_" { // e.g. "for _ = range x"
   344  			return tInvalid
   345  		}
   346  		panic("undefined ident: " + e.Name)
   347  
   348  	case *ast.Ellipsis:
   349  		if e.Elt != nil {
   350  			f.expr(e.Elt)
   351  		}
   352  
   353  	case *ast.FuncLit:
   354  		saved := f.sig
   355  		f.sig = tv.Type.(*types.Signature)
   356  		f.stmt(e.Body)
   357  		f.sig = saved
   358  
   359  	case *ast.CompositeLit:
   360  		switch T := typeparams.CoreType(typeparams.Deref(tv.Type)).(type) {
   361  		case *types.Struct:
   362  			for i, elem := range e.Elts {
   363  				if kv, ok := elem.(*ast.KeyValueExpr); ok {
   364  					f.assign(f.info.Uses[kv.Key.(*ast.Ident)].Type(), f.expr(kv.Value))
   365  				} else {
   366  					f.assign(T.Field(i).Type(), f.expr(elem))
   367  				}
   368  			}
   369  
   370  		case *types.Map:
   371  			for _, elem := range e.Elts {
   372  				elem := elem.(*ast.KeyValueExpr)
   373  				f.assign(T.Key(), f.expr(elem.Key))
   374  				f.assign(T.Elem(), f.expr(elem.Value))
   375  			}
   376  
   377  		case *types.Array, *types.Slice:
   378  			tElem := T.(interface {
   379  				Elem() types.Type
   380  			}).Elem()
   381  			for _, elem := range e.Elts {
   382  				if kv, ok := elem.(*ast.KeyValueExpr); ok {
   383  					// ignore the key
   384  					f.assign(tElem, f.expr(kv.Value))
   385  				} else {
   386  					f.assign(tElem, f.expr(elem))
   387  				}
   388  			}
   389  
   390  		default:
   391  			panic(fmt.Sprintf("unexpected composite literal type %T: %v", tv.Type, tv.Type.String()))
   392  		}
   393  
   394  	case *ast.ParenExpr:
   395  		f.expr(e.X)
   396  
   397  	case *ast.SelectorExpr:
   398  		if _, ok := f.info.Selections[e]; ok {
   399  			f.expr(e.X) // selection
   400  		} else {
   401  			return f.info.Uses[e.Sel].Type() // qualified identifier
   402  		}
   403  
   404  	case *ast.IndexExpr:
   405  		if instance(f.info, e.X) {
   406  			// f[T] or C[T] -- generic instantiation
   407  		} else {
   408  			// x[i] or m[k] -- index or lookup operation
   409  			x := f.expr(e.X)
   410  			i := f.expr(e.Index)
   411  			if ux, ok := typeparams.CoreType(x).(*types.Map); ok {
   412  				f.assign(ux.Key(), i)
   413  			}
   414  		}
   415  
   416  	case *ast.IndexListExpr:
   417  		// f[X, Y] -- generic instantiation
   418  
   419  	case *ast.SliceExpr:
   420  		f.expr(e.X)
   421  		if e.Low != nil {
   422  			f.expr(e.Low)
   423  		}
   424  		if e.High != nil {
   425  			f.expr(e.High)
   426  		}
   427  		if e.Max != nil {
   428  			f.expr(e.Max)
   429  		}
   430  
   431  	case *ast.TypeAssertExpr:
   432  		x := f.expr(e.X)
   433  		f.typeAssert(x, f.info.Types[e.Type].Type)
   434  
   435  	case *ast.CallExpr:
   436  		if tvFun := f.info.Types[e.Fun]; tvFun.IsType() {
   437  			// conversion
   438  			arg0 := f.expr(e.Args[0])
   439  			f.assign(tvFun.Type, arg0)
   440  		} else {
   441  			// function call
   442  
   443  			// unsafe call. Treat calls to functions in unsafe like ordinary calls,
   444  			// except that their signature cannot be determined by their func obj.
   445  			// Without this special handling, f.expr(e.Fun) would fail below.
   446  			if s, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok {
   447  				if obj, ok := f.info.Uses[s.Sel].(*types.Builtin); ok && obj.Pkg().Path() == "unsafe" {
   448  					sig := f.info.Types[e.Fun].Type.(*types.Signature)
   449  					f.call(sig, e.Args)
   450  					return tv.Type
   451  				}
   452  			}
   453  
   454  			// builtin call
   455  			if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok {
   456  				if obj, ok := f.info.Uses[id].(*types.Builtin); ok {
   457  					sig := f.info.Types[id].Type.(*types.Signature)
   458  					f.builtin(obj, sig, e.Args)
   459  					return tv.Type
   460  				}
   461  			}
   462  
   463  			// ordinary call
   464  			f.call(typeparams.CoreType(f.expr(e.Fun)).(*types.Signature), e.Args)
   465  		}
   466  
   467  	case *ast.StarExpr:
   468  		f.expr(e.X)
   469  
   470  	case *ast.UnaryExpr:
   471  		f.expr(e.X)
   472  
   473  	case *ast.BinaryExpr:
   474  		x := f.expr(e.X)
   475  		y := f.expr(e.Y)
   476  		if e.Op == token.EQL || e.Op == token.NEQ {
   477  			f.compare(x, y)
   478  		}
   479  
   480  	case *ast.KeyValueExpr:
   481  		f.expr(e.Key)
   482  		f.expr(e.Value)
   483  
   484  	case *ast.ArrayType,
   485  		*ast.StructType,
   486  		*ast.FuncType,
   487  		*ast.InterfaceType,
   488  		*ast.MapType,
   489  		*ast.ChanType:
   490  		panic(e)
   491  	}
   492  
   493  	if tv.Type == nil {
   494  		panic(fmt.Sprintf("no type for %T", e))
   495  	}
   496  
   497  	return tv.Type
   498  }
   499  
   500  func (f *Finder) stmt(s ast.Stmt) {
   501  	switch s := s.(type) {
   502  	case *ast.BadStmt,
   503  		*ast.EmptyStmt,
   504  		*ast.BranchStmt:
   505  		// no-op
   506  
   507  	case *ast.DeclStmt:
   508  		d := s.Decl.(*ast.GenDecl)
   509  		if d.Tok == token.VAR { // ignore consts
   510  			for _, spec := range d.Specs {
   511  				f.valueSpec(spec.(*ast.ValueSpec))
   512  			}
   513  		}
   514  
   515  	case *ast.LabeledStmt:
   516  		f.stmt(s.Stmt)
   517  
   518  	case *ast.ExprStmt:
   519  		f.expr(s.X)
   520  
   521  	case *ast.SendStmt:
   522  		ch := f.expr(s.Chan)
   523  		val := f.expr(s.Value)
   524  		f.assign(typeparams.CoreType(ch).(*types.Chan).Elem(), val)
   525  
   526  	case *ast.IncDecStmt:
   527  		f.expr(s.X)
   528  
   529  	case *ast.AssignStmt:
   530  		switch s.Tok {
   531  		case token.ASSIGN, token.DEFINE:
   532  			// y := x   or   y = x
   533  			var rhsTuple types.Type
   534  			if len(s.Lhs) != len(s.Rhs) {
   535  				rhsTuple = f.exprN(s.Rhs[0])
   536  			}
   537  			for i := range s.Lhs {
   538  				var lhs, rhs types.Type
   539  				if rhsTuple == nil {
   540  					rhs = f.expr(s.Rhs[i]) // 1:1 assignment
   541  				} else {
   542  					rhs = f.extract(rhsTuple, i) // n:1 assignment
   543  				}
   544  
   545  				if id, ok := s.Lhs[i].(*ast.Ident); ok {
   546  					if id.Name != "_" {
   547  						if obj, ok := f.info.Defs[id]; ok {
   548  							lhs = obj.Type() // definition
   549  						}
   550  					}
   551  				}
   552  				if lhs == nil {
   553  					lhs = f.expr(s.Lhs[i]) // assignment
   554  				}
   555  				f.assign(lhs, rhs)
   556  			}
   557  
   558  		default:
   559  			// y op= x
   560  			f.expr(s.Lhs[0])
   561  			f.expr(s.Rhs[0])
   562  		}
   563  
   564  	case *ast.GoStmt:
   565  		f.expr(s.Call)
   566  
   567  	case *ast.DeferStmt:
   568  		f.expr(s.Call)
   569  
   570  	case *ast.ReturnStmt:
   571  		formals := f.sig.Results()
   572  		switch len(s.Results) {
   573  		case formals.Len(): // 1:1
   574  			for i, result := range s.Results {
   575  				f.assign(formals.At(i).Type(), f.expr(result))
   576  			}
   577  
   578  		case 1: // n:1
   579  			tuple := f.exprN(s.Results[0])
   580  			for i := 0; i < formals.Len(); i++ {
   581  				f.assign(formals.At(i).Type(), f.extract(tuple, i))
   582  			}
   583  		}
   584  
   585  	case *ast.SelectStmt:
   586  		f.stmt(s.Body)
   587  
   588  	case *ast.BlockStmt:
   589  		for _, s := range s.List {
   590  			f.stmt(s)
   591  		}
   592  
   593  	case *ast.IfStmt:
   594  		if s.Init != nil {
   595  			f.stmt(s.Init)
   596  		}
   597  		f.expr(s.Cond)
   598  		f.stmt(s.Body)
   599  		if s.Else != nil {
   600  			f.stmt(s.Else)
   601  		}
   602  
   603  	case *ast.SwitchStmt:
   604  		if s.Init != nil {
   605  			f.stmt(s.Init)
   606  		}
   607  		var tag types.Type = tUntypedBool
   608  		if s.Tag != nil {
   609  			tag = f.expr(s.Tag)
   610  		}
   611  		for _, cc := range s.Body.List {
   612  			cc := cc.(*ast.CaseClause)
   613  			for _, cond := range cc.List {
   614  				f.compare(tag, f.info.Types[cond].Type)
   615  			}
   616  			for _, s := range cc.Body {
   617  				f.stmt(s)
   618  			}
   619  		}
   620  
   621  	case *ast.TypeSwitchStmt:
   622  		if s.Init != nil {
   623  			f.stmt(s.Init)
   624  		}
   625  		var I types.Type
   626  		switch ass := s.Assign.(type) {
   627  		case *ast.ExprStmt: // x.(type)
   628  			I = f.expr(ast.Unparen(ass.X).(*ast.TypeAssertExpr).X)
   629  		case *ast.AssignStmt: // y := x.(type)
   630  			I = f.expr(ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
   631  		}
   632  		for _, cc := range s.Body.List {
   633  			cc := cc.(*ast.CaseClause)
   634  			for _, cond := range cc.List {
   635  				tCase := f.info.Types[cond].Type
   636  				if tCase != tUntypedNil {
   637  					f.typeAssert(I, tCase)
   638  				}
   639  			}
   640  			for _, s := range cc.Body {
   641  				f.stmt(s)
   642  			}
   643  		}
   644  
   645  	case *ast.CommClause:
   646  		if s.Comm != nil {
   647  			f.stmt(s.Comm)
   648  		}
   649  		for _, s := range s.Body {
   650  			f.stmt(s)
   651  		}
   652  
   653  	case *ast.ForStmt:
   654  		if s.Init != nil {
   655  			f.stmt(s.Init)
   656  		}
   657  		if s.Cond != nil {
   658  			f.expr(s.Cond)
   659  		}
   660  		if s.Post != nil {
   661  			f.stmt(s.Post)
   662  		}
   663  		f.stmt(s.Body)
   664  
   665  	case *ast.RangeStmt:
   666  		x := f.expr(s.X)
   667  		// No conversions are involved when Tok==DEFINE.
   668  		if s.Tok == token.ASSIGN {
   669  			if s.Key != nil {
   670  				k := f.expr(s.Key)
   671  				var xelem types.Type
   672  				// Keys of array, *array, slice, string aren't interesting
   673  				// since the RHS key type is just an int.
   674  				switch ux := typeparams.CoreType(x).(type) {
   675  				case *types.Chan:
   676  					xelem = ux.Elem()
   677  				case *types.Map:
   678  					xelem = ux.Key()
   679  				}
   680  				if xelem != nil {
   681  					f.assign(k, xelem)
   682  				}
   683  			}
   684  			if s.Value != nil {
   685  				val := f.expr(s.Value)
   686  				var xelem types.Type
   687  				// Values of type strings aren't interesting because
   688  				// the RHS value type is just a rune.
   689  				switch ux := typeparams.CoreType(x).(type) {
   690  				case *types.Array:
   691  					xelem = ux.Elem()
   692  				case *types.Map:
   693  					xelem = ux.Elem()
   694  				case *types.Pointer: // *array
   695  					xelem = typeparams.CoreType(typeparams.Deref(ux)).(*types.Array).Elem()
   696  				case *types.Slice:
   697  					xelem = ux.Elem()
   698  				}
   699  				if xelem != nil {
   700  					f.assign(val, xelem)
   701  				}
   702  			}
   703  		}
   704  		f.stmt(s.Body)
   705  
   706  	default:
   707  		panic(s)
   708  	}
   709  }
   710  
   711  // -- Plundered from golang.org/x/tools/go/ssa -----------------
   712  
   713  func instance(info *types.Info, expr ast.Expr) bool {
   714  	var id *ast.Ident
   715  	switch x := expr.(type) {
   716  	case *ast.Ident:
   717  		id = x
   718  	case *ast.SelectorExpr:
   719  		id = x.Sel
   720  	default:
   721  		return false
   722  	}
   723  	_, ok := info.Instances[id]
   724  	return ok
   725  }
   726  

View as plain text