Source file test/decomposeaddr.go

     1  // errorcheck -0 -d=ssa/decompose_addr/debug=1
     2  
     3  // Copyright 2026 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test for variables that can be decomposed by the decomposeAddr pass.
     8  
     9  package main
    10  
    11  import "unsafe"
    12  
    13  type point struct {
    14  	x, y int
    15  }
    16  
    17  func simpleStruct(c bool) int {
    18  	p := point{10, 20} // ERROR "promoted p"
    19  	ptr := &p
    20  	if c {
    21  		ptr.x += 1
    22  	} else {
    23  		ptr.y += 1
    24  	}
    25  	return ptr.x + ptr.y
    26  }
    27  
    28  type inner struct {
    29  	a, b int
    30  }
    31  
    32  type outer struct {
    33  	in inner
    34  	c  int
    35  }
    36  
    37  func nestedStruct(c bool) int {
    38  	o := outer{inner{1, 2}, 3} // ERROR "promoted o"
    39  	ptr := &o
    40  	if c {
    41  		ptr.in.a += 10
    42  	} else {
    43  		ptr.in.b += 20
    44  	}
    45  	return ptr.in.a + ptr.in.b + ptr.c
    46  }
    47  
    48  func array(c bool) int {
    49  	var a [3]int // ERROR "promoted a"
    50  	pa := &a
    51  	pa[0] = 10
    52  	pa[1] = 20
    53  	pa[2] = 30
    54  	if c {
    55  		pa[1] += 5
    56  	}
    57  	return pa[0] + pa[1] + pa[2]
    58  }
    59  
    60  func zeroStruct(c bool) int {
    61  	var p point // ERROR "promoted p"
    62  	ptr := &p
    63  	if c {
    64  		ptr.x = 10
    65  	} else {
    66  		ptr.y = 20
    67  	}
    68  	return ptr.x + ptr.y
    69  }
    70  
    71  func moveStruct(x, y int, c bool) int {
    72  	p1 := point{x, y} // ERROR "promoted p1"
    73  	var p2 point      // ERROR "promoted p2"
    74  	ptr1 := &p1
    75  	ptr2 := &p2
    76  	if c {
    77  		*ptr2 = *ptr1
    78  	} else {
    79  		*ptr2 = point{30, 40}
    80  	}
    81  	return ptr2.x + ptr2.y + ptr1.x
    82  }
    83  
    84  type arrayStruct struct {
    85  	a [2]int
    86  	b int
    87  }
    88  
    89  func arrayStructAddr(c bool) int {
    90  	s := arrayStruct{[2]int{1, 2}, 3} // ERROR "promoted s"
    91  	ps := &s
    92  	if c {
    93  		ps.a[0] += 10
    94  	} else {
    95  		ps.b += 20
    96  	}
    97  	return ps.a[0] + ps.a[1] + ps.b
    98  }
    99  
   100  func partialZero() int {
   101  	type T struct {
   102  		a int
   103  		b [2]int
   104  		c int
   105  	}
   106  
   107  	t := T{a: 9, c: 9} // ERROR "promoted t"
   108  	p := &t.b          // OffPtr
   109  	*p = [2]int{}      // Zero
   110  	return t.a
   111  }
   112  
   113  func partialMove() int {
   114  	type T struct {
   115  		a int
   116  		b [2]int
   117  		c int
   118  	}
   119  
   120  	b2 := [2]int{1, 2} // ERROR "promoted b2"
   121  
   122  	t := T{a: 9, c: 9} // ERROR "promoted t"
   123  	p := &t.b          // OffPtr
   124  	*p = b2            // Move
   125  	return t.a
   126  }
   127  
   128  type bigStruct struct {
   129  	a, b, c, d, e int
   130  	f             [30]int
   131  	g, h, i, j, k int
   132  }
   133  
   134  func partialActiveBigStruct(branch bool) {
   135  	// branch is used to fool opt.
   136  	var x, y bigStruct // ERROR "promoted x"
   137  	xp := &x
   138  	yp := &y
   139  	if branch {
   140  		xp.a = 1
   141  		xp.b = 2
   142  	} else {
   143  		xp.a = 4
   144  		xp.b = 5
   145  	}
   146  	xp.f = [30]int{3, 4}
   147  	xp.c = xp.a + xp.b // 2 leaves of x are active
   148  	if branch {
   149  		yp.c = 1
   150  		yp.d = 2
   151  		yp.e = 3
   152  		yp.f[5] = 4
   153  		yp.g = 5
   154  	} else {
   155  		yp.c = 4
   156  		yp.d = 5
   157  		yp.e = 6
   158  		yp.f[5] = 7
   159  		yp.g = 8
   160  	}
   161  	yp.h = yp.c + yp.d + yp.e + yp.f[5] + yp.g // 5 leaves of y are used
   162  }
   163  
   164  // x looks at the Move first and would rule y out (y's leaves do not line
   165  // up with x's), but x then gives up on itself because of the 2-byte read
   166  // that load forwarding created on it. Giving up must also take back the
   167  // verdict on y: y is then free to break the Move down by its own leaves.
   168  func unsafeAliasMoveOrder(c bool) int {
   169  	var x [4]byte
   170  	x[0], x[1], x[2], x[3] = 1, 2, 3, 4
   171  	var y [2]uint16 // ERROR "promoted y"
   172  	y = *(*[2]uint16)(unsafe.Pointer(&x))
   173  	if c {
   174  		y[0]++
   175  	}
   176  	return int(y[0]) + int(y[1])
   177  }
   178  
   179  type paddedA struct {
   180  	a             uint8
   181  	b, c, d, e, f uint32
   182  }
   183  
   184  type paddedB struct {
   185  	x, y          uint8
   186  	b, c, d, e, f uint32
   187  }
   188  
   189  // a looks at the Move first. Every leaf of a lines up with a leaf of b,
   190  // but b has a leaf (y) where a only has padding, so a breakdown of the
   191  // Move by a's leaves would never write b.y. The Move must cover exactly
   192  // the same leaves on both sides for both to be decomposed, so b is ruled
   193  // out and stays in memory.
   194  func unsafeAliasMovePadding(v uint8, c bool) (uint8, uint32) {
   195  	var a paddedA // ERROR "promoted a"
   196  	a.a = v
   197  	a.b, a.c = 1, 2
   198  	var b paddedB
   199  	b.x, b.y = 7, 9
   200  	b = *(*paddedB)(unsafe.Pointer(&a))
   201  	b.b++
   202  	if c {
   203  		b.c++
   204  	}
   205  	return b.y, b.b + b.c
   206  }
   207  
   208  type stringStruct struct {
   209  	s string
   210  	i int
   211  }
   212  
   213  func stringField(c bool) int {
   214  	// The string field splits into a pointer leaf and a length leaf.
   215  	ss := stringStruct{"hello", 10} // ERROR "promoted ss"
   216  	p := &ss
   217  	if c {
   218  		p.i += 5
   219  	}
   220  	return len(p.s) + p.i
   221  }
   222  
   223  type sliceStruct struct {
   224  	sl []int
   225  	i  int
   226  }
   227  
   228  func sliceField(c bool, sl []int) int {
   229  	// The slice field splits into ptr/len/cap leaves.
   230  	ss := sliceStruct{sl, 10} // ERROR "promoted ss"
   231  	p := &ss
   232  	if c {
   233  		p.i += 5
   234  	}
   235  	return len(p.sl) + cap(p.sl) + p.i
   236  }
   237  
   238  type ifaceStruct struct {
   239  	e any
   240  	i int
   241  }
   242  
   243  func ifaceField(c bool, e any) int {
   244  	// The interface field splits into a type word and a data word.
   245  	is := ifaceStruct{e, 10} // ERROR "promoted is"
   246  	p := &is
   247  	if c {
   248  		p.i += 5
   249  	}
   250  	if p.e != nil {
   251  		p.i++
   252  	}
   253  	return p.i
   254  }
   255  
   256  type complexStruct struct {
   257  	z complex64
   258  	i int
   259  }
   260  
   261  func complexField(c bool) float32 {
   262  	// The complex field splits into two float leaves. complex64 rather
   263  	// than complex128 so the float leaves fit in a register on 32-bit
   264  	// platforms too.
   265  	cs := complexStruct{complex(3, 4), 10} // ERROR "promoted cs"
   266  	p := &cs
   267  	if c {
   268  		p.i += 5
   269  	}
   270  	return real(p.z) + imag(p.z) + float32(p.i)
   271  }
   272  
   273  func main() {}
   274  

View as plain text