github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/cmd/vet/copylock.go (about)

     1  // Copyright 2013 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  // This file contains the code to check that locks are not passed by value.
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"go/ast"
    13  	"go/token"
    14  	"go/types"
    15  )
    16  
    17  func init() {
    18  	register("copylocks",
    19  		"check that locks are not passed by value",
    20  		checkCopyLocks,
    21  		funcDecl, rangeStmt, funcLit, assignStmt)
    22  }
    23  
    24  // checkCopyLocks checks whether node might
    25  // inadvertently copy a lock.
    26  func checkCopyLocks(f *File, node ast.Node) {
    27  	switch node := node.(type) {
    28  	case *ast.RangeStmt:
    29  		checkCopyLocksRange(f, node)
    30  	case *ast.FuncDecl:
    31  		checkCopyLocksFunc(f, node.Name.Name, node.Recv, node.Type)
    32  	case *ast.FuncLit:
    33  		checkCopyLocksFunc(f, "func", nil, node.Type)
    34  	case *ast.AssignStmt:
    35  		checkCopyLocksAssign(f, node)
    36  	}
    37  }
    38  
    39  // checkCopyLocksAssign checks whether an assignment
    40  // copies a lock.
    41  func checkCopyLocksAssign(f *File, as *ast.AssignStmt) {
    42  	for _, x := range as.Lhs {
    43  		if path := lockPath(f.pkg.typesPkg, f.pkg.types[x].Type); path != nil {
    44  			f.Badf(x.Pos(), "assignment copies lock value to %v: %v", f.gofmt(x), path)
    45  		}
    46  	}
    47  }
    48  
    49  // checkCopyLocksFunc checks whether a function might
    50  // inadvertently copy a lock, by checking whether
    51  // its receiver, parameters, or return values
    52  // are locks.
    53  func checkCopyLocksFunc(f *File, name string, recv *ast.FieldList, typ *ast.FuncType) {
    54  	if recv != nil && len(recv.List) > 0 {
    55  		expr := recv.List[0].Type
    56  		if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
    57  			f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path)
    58  		}
    59  	}
    60  
    61  	if typ.Params != nil {
    62  		for _, field := range typ.Params.List {
    63  			expr := field.Type
    64  			if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
    65  				f.Badf(expr.Pos(), "%s passes lock by value: %v", name, path)
    66  			}
    67  		}
    68  	}
    69  
    70  	if typ.Results != nil {
    71  		for _, field := range typ.Results.List {
    72  			expr := field.Type
    73  			if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
    74  				f.Badf(expr.Pos(), "%s returns lock by value: %v", name, path)
    75  			}
    76  		}
    77  	}
    78  }
    79  
    80  // checkCopyLocksRange checks whether a range statement
    81  // might inadvertently copy a lock by checking whether
    82  // any of the range variables are locks.
    83  func checkCopyLocksRange(f *File, r *ast.RangeStmt) {
    84  	checkCopyLocksRangeVar(f, r.Tok, r.Key)
    85  	checkCopyLocksRangeVar(f, r.Tok, r.Value)
    86  }
    87  
    88  func checkCopyLocksRangeVar(f *File, rtok token.Token, e ast.Expr) {
    89  	if e == nil {
    90  		return
    91  	}
    92  	id, isId := e.(*ast.Ident)
    93  	if isId && id.Name == "_" {
    94  		return
    95  	}
    96  
    97  	var typ types.Type
    98  	if rtok == token.DEFINE {
    99  		if !isId {
   100  			return
   101  		}
   102  		obj := f.pkg.defs[id]
   103  		if obj == nil {
   104  			return
   105  		}
   106  		typ = obj.Type()
   107  	} else {
   108  		typ = f.pkg.types[e].Type
   109  	}
   110  
   111  	if typ == nil {
   112  		return
   113  	}
   114  	if path := lockPath(f.pkg.typesPkg, typ); path != nil {
   115  		f.Badf(e.Pos(), "range var %s copies lock: %v", f.gofmt(e), path)
   116  	}
   117  }
   118  
   119  type typePath []types.Type
   120  
   121  // String pretty-prints a typePath.
   122  func (path typePath) String() string {
   123  	n := len(path)
   124  	var buf bytes.Buffer
   125  	for i := range path {
   126  		if i > 0 {
   127  			fmt.Fprint(&buf, " contains ")
   128  		}
   129  		// The human-readable path is in reverse order, outermost to innermost.
   130  		fmt.Fprint(&buf, path[n-i-1].String())
   131  	}
   132  	return buf.String()
   133  }
   134  
   135  // lockPath returns a typePath describing the location of a lock value
   136  // contained in typ. If there is no contained lock, it returns nil.
   137  func lockPath(tpkg *types.Package, typ types.Type) typePath {
   138  	if typ == nil {
   139  		return nil
   140  	}
   141  
   142  	// We're only interested in the case in which the underlying
   143  	// type is a struct. (Interfaces and pointers are safe to copy.)
   144  	styp, ok := typ.Underlying().(*types.Struct)
   145  	if !ok {
   146  		return nil
   147  	}
   148  
   149  	// We're looking for cases in which a reference to this type
   150  	// can be locked, but a value cannot. This differentiates
   151  	// embedded interfaces from embedded values.
   152  	if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil {
   153  		if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil {
   154  			return []types.Type{typ}
   155  		}
   156  	}
   157  
   158  	nfields := styp.NumFields()
   159  	for i := 0; i < nfields; i++ {
   160  		ftyp := styp.Field(i).Type()
   161  		subpath := lockPath(tpkg, ftyp)
   162  		if subpath != nil {
   163  			return append(subpath, typ)
   164  		}
   165  	}
   166  
   167  	return nil
   168  }