github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/testdata/maps.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  // Test of maps.
     7  
     8  var a, b, c int
     9  
    10  func maps1() {
    11  	m1 := map[*int]*int{&a: &b} // @line m1m1
    12  	m2 := make(map[*int]*int)   // @line m1m2
    13  	m2[&b] = &a
    14  
    15  	print(m1[nil]) // @pointsto command-line-arguments.b | command-line-arguments.c
    16  	print(m2[nil]) // @pointsto command-line-arguments.a
    17  
    18  	print(m1) // @pointsto makemap@m1m1:21
    19  	print(m2) // @pointsto makemap@m1m2:12
    20  
    21  	m1[&b] = &c
    22  
    23  	for k, v := range m1 {
    24  		print(k) // @pointsto command-line-arguments.a | command-line-arguments.b
    25  		print(v) // @pointsto command-line-arguments.b | command-line-arguments.c
    26  	}
    27  
    28  	for k, v := range m2 {
    29  		print(k) // @pointsto command-line-arguments.b
    30  		print(v) // @pointsto command-line-arguments.a
    31  	}
    32  
    33  	// Lookup doesn't create any aliases.
    34  	print(m2[&c]) // @pointsto command-line-arguments.a
    35  	if _, ok := m2[&a]; ok {
    36  		print(m2[&c]) // @pointsto command-line-arguments.a
    37  	}
    38  }
    39  
    40  func maps2() {
    41  	m1 := map[*int]*int{&a: &b}
    42  	m2 := map[*int]*int{&b: &c}
    43  	_ = []map[*int]*int{m1, m2} // (no spurious merging of m1, m2)
    44  
    45  	print(m1[nil]) // @pointsto command-line-arguments.b
    46  	print(m2[nil]) // @pointsto command-line-arguments.c
    47  }
    48  
    49  var g int
    50  
    51  func maps3() {
    52  	// Regression test for a constraint generation bug for map range
    53  	// loops in which the key is unused: the (ok, k, v) tuple
    54  	// returned by ssa.Next may have type 'invalid' for the k and/or
    55  	// v components, so copying the map key or value may cause
    56  	// miswiring if the key has >1 components.  In the worst case,
    57  	// this causes a crash.  The test below used to report that
    58  	// pts(v) includes not just command-line-arguments.g but new(float64) too, which
    59  	// is ill-typed.
    60  
    61  	// sizeof(K) > 1, abstractly
    62  	type K struct{ a, b, c, d *float64 }
    63  	k := K{new(float64), nil, nil, nil}
    64  	m := map[K]*int{k: &g}
    65  
    66  	for _, v := range m {
    67  		print(v) // @pointsto command-line-arguments.g
    68  	}
    69  }
    70  
    71  var v float64
    72  
    73  func maps4() {
    74  	// Regression test for generating constraints for cases of key and values
    75  	// being blank identifiers or different types assignable from the
    76  	// corresponding map types in a range stmt.
    77  	type K struct{ a *float64 }
    78  	k := K{&v}
    79  	m := map[K]*int{k: &g}
    80  
    81  	for x, y := range m {
    82  		print(x.a) // @pointsto command-line-arguments.v
    83  		print(y)   // @pointsto command-line-arguments.g
    84  	}
    85  	var i struct{ a *float64 }
    86  	for i, _ = range m {
    87  		print(i.a) // @pointsto command-line-arguments.v
    88  	}
    89  	var j interface{}
    90  	for _, j = range m {
    91  		// TODO support the statement `print(j.(*int))`
    92  		print(j) // @pointsto command-line-arguments.g
    93  	}
    94  	for _, _ = range m {
    95  	}
    96  	// do something after 'for _, _ =' to exercise the
    97  	// effects of indexing
    98  	for _, j = range m {
    99  		// TODO support the statement `print(j.(*int))`
   100  		print(j) // @pointsto command-line-arguments.g
   101  	}
   102  }
   103  
   104  func main() {
   105  	maps1()
   106  	maps2()
   107  	maps3()
   108  	maps4()
   109  }