github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/ssa/testdata/valueforexpr.go (about)

     1  //+build ignore
     2  
     3  package main
     4  
     5  // This file is the input to TestValueForExpr in source_test.go, which
     6  // ensures that each expression e immediately following a /*@kind*/(x)
     7  // annotation, when passed to Function.ValueForExpr(e), returns a
     8  // non-nil Value of the same type as e and of kind 'kind'.
     9  
    10  func f(spilled, unspilled int) {
    11  	_ = /*@UnOp*/ (spilled)
    12  	_ = /*@Parameter*/ (unspilled)
    13  	_ = /*@<nil>*/ (1 + 2) // (constant)
    14  	i := 0
    15  
    16  	f := func() (int, int) { return 0, 0 }
    17  
    18  	/*@Call*/ (print( /*@BinOp*/ (i + 1)))
    19  	_, _ = /*@Call*/ (f())
    20  	ch := /*@MakeChan*/ (make(chan int))
    21  	/*@UnOp*/ (<-ch)
    22  	x := /*@UnOp*/ (<-ch)
    23  	_ = x
    24  	select {
    25  	case /*@Extract*/ (<-ch):
    26  	case x := /*@Extract*/ (<-ch):
    27  		_ = x
    28  	}
    29  	defer /*@Function*/ (func() {
    30  	})()
    31  	go /*@Function*/ (func() {
    32  	})()
    33  	y := 0
    34  	if true && /*@BinOp*/ (bool(y > 0)) {
    35  		y = 1
    36  	}
    37  	_ = /*@Phi*/ (y)
    38  	map1 := /*@MakeMap*/ (make(map[string]string))
    39  	_ = map1
    40  	_ = /*@Slice*/ (make([]int, 0))
    41  	_ = /*@MakeClosure*/ (func() { print(spilled) })
    42  
    43  	sl := []int{}
    44  	_ = /*@Slice*/ (sl[:0])
    45  
    46  	tmp := /*@Alloc*/ (new(int))
    47  	_ = tmp
    48  	var iface interface{}
    49  	_ = /*@TypeAssert*/ (iface.(int))
    50  	_ = /*@UnOp*/ (sl[0])
    51  	_ = /*@IndexAddr*/ (&sl[0])
    52  	_ = /*@Index*/ ([2]int{}[0])
    53  	var p *int
    54  	_ = /*@UnOp*/ (*p)
    55  
    56  	_ = /*@UnOp*/ (global)
    57  	/*@UnOp*/ (global)[""] = ""
    58  	/*@Global*/ (global) = map[string]string{}
    59  
    60  	var local t
    61  	/*UnOp*/ (local.x) = 1
    62  
    63  	// Exercise corner-cases of lvalues vs rvalues.
    64  	type N *N
    65  	var n N
    66  	/*@UnOp*/ (n) = /*@UnOp*/ (n)
    67  	/*@ChangeType*/ (n) = /*@Alloc*/ (&n)
    68  	/*@UnOp*/ (n) = /*@UnOp*/ (*n)
    69  	/*@UnOp*/ (n) = /*@UnOp*/ (**n)
    70  }
    71  
    72  func complit() {
    73  	// Composite literals.
    74  	// We get different results for
    75  	// - composite literal as value (e.g. operand to print)
    76  	// - composite literal initializer for addressable value
    77  	// - composite literal value assigned to blank var
    78  
    79  	// 1. Slices
    80  	print( /*@Slice*/ ([]int{}))
    81  	print( /*@Alloc*/ (&[]int{}))
    82  	print(& /*@Slice*/ ([]int{}))
    83  
    84  	sl1 := /*@Slice*/ ([]int{})
    85  	sl2 := /*@Alloc*/ (&[]int{})
    86  	sl3 := & /*@Slice*/ ([]int{})
    87  	_, _, _ = sl1, sl2, sl3
    88  
    89  	_ = /*@Slice*/ ([]int{})
    90  	_ = & /*@Slice*/ ([]int{})
    91  
    92  	// 2. Arrays
    93  	print( /*@UnOp*/ ([1]int{}))
    94  	print( /*@Alloc*/ (&[1]int{}))
    95  	print(& /*@Alloc*/ ([1]int{}))
    96  
    97  	arr1 := /*@Alloc*/ ([1]int{})
    98  	arr2 := /*@Alloc*/ (&[1]int{})
    99  	arr3 := & /*@Alloc*/ ([1]int{})
   100  	_, _, _ = arr1, arr2, arr3
   101  
   102  	_ = /*@UnOp*/ ([1]int{})
   103  	_ = /*@Alloc*/ (& /*@Alloc*/ ([1]int{}))
   104  	_ = & /*@Alloc*/ ([1]int{})
   105  
   106  	// 3. Maps
   107  	type M map[int]int
   108  	print( /*@MakeMap*/ (M{}))
   109  	print( /*@Alloc*/ (&M{}))
   110  	print(& /*@MakeMap*/ (M{}))
   111  
   112  	m1 := /*@MakeMap*/ (M{})
   113  	m2 := /*@Alloc*/ (&M{})
   114  	m3 := & /*@MakeMap*/ (M{})
   115  	_, _, _ = m1, m2, m3
   116  
   117  	_ = /*@MakeMap*/ (M{})
   118  	_ = & /*@MakeMap*/ (M{})
   119  
   120  	// 4. Structs
   121  	print( /*@UnOp*/ (struct{}{}))
   122  	print( /*@Alloc*/ (&struct{}{}))
   123  	print(& /*@Alloc*/ (struct{}{}))
   124  
   125  	s1 := /*@Alloc*/ (struct{}{})
   126  	s2 := /*@Alloc*/ (&struct{}{})
   127  	s3 := & /*@Alloc*/ (struct{}{})
   128  	_, _, _ = s1, s2, s3
   129  
   130  	_ = /*@UnOp*/ (struct{}{})
   131  	_ = /*@Alloc*/ (& /*@Alloc*/ (struct{}{}))
   132  	_ = & /*@Alloc*/ (struct{}{})
   133  }
   134  
   135  type t struct{ x int }
   136  
   137  // Ensure we can locate methods of named types.
   138  func (t) f(param int) {
   139  	_ = /*@Parameter*/ (param)
   140  }
   141  
   142  // Ensure we can locate init functions.
   143  func init() {
   144  	m := /*@MakeMap*/ (make(map[string]string))
   145  	_ = m
   146  }
   147  
   148  // Ensure we can locate variables in initializer expressions.
   149  var global = /*@MakeMap*/ (make(map[string]string))