golang.org/x/tools@v0.21.0/go/analysis/passes/atomicalign/testdata/src/a/a.go (about)

     1  // Copyright 2019 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 tests for the atomic alignment checker.
     6  
     7  //go:build arm || 386
     8  // +build arm 386
     9  
    10  package testdata
    11  
    12  import (
    13  	"io"
    14  	"sync/atomic"
    15  )
    16  
    17  func intsAlignment() {
    18  	var s struct {
    19  		a bool
    20  		b uint8
    21  		c int8
    22  		d byte
    23  		f int16
    24  		g int16
    25  		h int64
    26  		i byte
    27  		j uint64
    28  	}
    29  	atomic.AddInt64(&s.h, 9)
    30  	atomic.AddUint64(&s.j, 0) // want "address of non 64-bit aligned field .j passed to atomic.AddUint64"
    31  }
    32  
    33  func floatAlignment() {
    34  	var s struct {
    35  		a float32
    36  		b int64
    37  		c float32
    38  		d float64
    39  		e uint64
    40  	}
    41  	atomic.LoadInt64(&s.b) // want "address of non 64-bit aligned field .b passed to atomic.LoadInt64"
    42  	atomic.LoadUint64(&s.e)
    43  }
    44  
    45  func uintptrAlignment() {
    46  	var s struct {
    47  		a uintptr
    48  		b int64
    49  		c int
    50  		d uint
    51  		e int32
    52  		f uint64
    53  	}
    54  	atomic.StoreInt64(&s.b, 0) // want "address of non 64-bit aligned field .b passed to atomic.StoreInt64"
    55  	atomic.StoreUint64(&s.f, 0)
    56  }
    57  
    58  func runeAlignment() {
    59  	var s struct {
    60  		a rune
    61  		b int64
    62  		_ rune
    63  		c uint64
    64  	}
    65  	atomic.SwapInt64(&s.b, 0) // want "address of non 64-bit aligned field .b passed to atomic.SwapInt64"
    66  	atomic.SwapUint64(&s.c, 0)
    67  }
    68  
    69  func complexAlignment() {
    70  	var s struct {
    71  		a complex64
    72  		b int64
    73  		c complex128
    74  		d uint64
    75  	}
    76  	atomic.CompareAndSwapInt64(&s.b, 0, 1)
    77  	atomic.CompareAndSwapUint64(&s.d, 0, 1)
    78  }
    79  
    80  // continuer ici avec les tests
    81  
    82  func channelAlignment() {
    83  	var a struct {
    84  		a chan struct{}
    85  		b int64
    86  		c <-chan struct{}
    87  		d uint64
    88  	}
    89  
    90  	atomic.AddInt64(&a.b, 0) // want "address of non 64-bit aligned field .b passed to atomic.AddInt64"
    91  	atomic.AddUint64(&a.d, 0)
    92  }
    93  
    94  func arrayAlignment() {
    95  	var a struct {
    96  		a [1]uint16
    97  		b int64
    98  		_ [2]uint16
    99  		c int64
   100  		d [1]uint16
   101  		e uint64
   102  	}
   103  
   104  	atomic.LoadInt64(&a.b) // want "address of non 64-bit aligned field .b passed to atomic.LoadInt64"
   105  	atomic.LoadInt64(&a.c)
   106  	atomic.LoadUint64(&a.e)   // want "address of non 64-bit aligned field .e passed to atomic.LoadUint64"
   107  	(atomic.LoadUint64)(&a.e) // want "address of non 64-bit aligned field .e passed to atomic.LoadUint64"
   108  }
   109  
   110  func anonymousFieldAlignment() {
   111  	var f struct {
   112  		a, b int32
   113  		c, d int64
   114  		_    bool
   115  		e, f uint64
   116  	}
   117  
   118  	atomic.StoreInt64(&f.c, 12)
   119  	atomic.StoreInt64(&f.d, 27)
   120  	atomic.StoreUint64(&f.e, 6)  // want "address of non 64-bit aligned field .e passed to atomic.StoreUint64"
   121  	atomic.StoreUint64(&f.f, 79) // want "address of non 64-bit aligned field .f passed to atomic.StoreUint64"
   122  }
   123  
   124  type ts struct {
   125  	e  int64
   126  	e2 []int
   127  	f  uint64
   128  }
   129  
   130  func typedStructAlignment() {
   131  	var b ts
   132  	atomic.SwapInt64(&b.e, 9)
   133  	atomic.SwapUint64(&b.f, 9) // want "address of non 64-bit aligned field .f passed to atomic.SwapUint64"
   134  }
   135  
   136  func aliasAlignment() {
   137  	type (
   138  		mybytea uint8
   139  		mybyteb byte
   140  		mybytec = uint8
   141  		mybyted = byte
   142  	)
   143  
   144  	var e struct {
   145  		a    byte
   146  		b    mybytea
   147  		c    mybyteb
   148  		e    mybytec
   149  		f    int64
   150  		g, h uint16
   151  		i    uint64
   152  	}
   153  
   154  	atomic.CompareAndSwapInt64(&e.f, 0, 1) // want "address of non 64-bit aligned field .f passed to atomic.CompareAndSwapInt64"
   155  	atomic.CompareAndSwapUint64(&e.i, 1, 2)
   156  }
   157  
   158  func stringAlignment() {
   159  	var a struct {
   160  		a uint32
   161  		b string
   162  		c int64
   163  	}
   164  	atomic.AddInt64(&a.c, 10) // want "address of non 64-bit aligned field .c passed to atomic.AddInt64"
   165  }
   166  
   167  func sliceAlignment() {
   168  	var s struct {
   169  		a []int32
   170  		b int64
   171  		c uint32
   172  		d uint64
   173  	}
   174  
   175  	atomic.LoadInt64(&s.b) // want "address of non 64-bit aligned field .b passed to atomic.LoadInt64"
   176  	atomic.LoadUint64(&s.d)
   177  }
   178  
   179  func interfaceAlignment() {
   180  	var s struct {
   181  		a interface{}
   182  		b int64
   183  		c io.Writer
   184  		e int64
   185  		_ int32
   186  		f uint64
   187  	}
   188  
   189  	atomic.StoreInt64(&s.b, 9)
   190  	atomic.StoreInt64(&s.e, 9)
   191  	atomic.StoreUint64(&s.f, 9) // want "address of non 64-bit aligned field .f passed to atomic.StoreUint64"
   192  }
   193  
   194  func pointerAlignment() {
   195  	var s struct {
   196  		a, b *int
   197  		c    int64
   198  		d    *interface{}
   199  		e    uint64
   200  	}
   201  
   202  	atomic.SwapInt64(&s.c, 9)
   203  	atomic.SwapUint64(&s.e, 9) // want "address of non 64-bit aligned field .e passed to atomic.SwapUint64"
   204  }
   205  
   206  // non-struct fields are already 64-bits correctly aligned per Go spec
   207  func nonStructFields() {
   208  	var (
   209  		a *int64
   210  		b [2]uint64
   211  		c int64
   212  	)
   213  
   214  	atomic.CompareAndSwapInt64(a, 10, 11)
   215  	atomic.CompareAndSwapUint64(&b[0], 5, 23)
   216  	atomic.CompareAndSwapInt64(&c, -1, -15)
   217  }
   218  
   219  func embeddedStructFields() {
   220  	var s1 struct {
   221  		_ struct{ _ int32 }
   222  		a int64
   223  		_ struct{}
   224  		b uint64
   225  		_ struct{ _ [2]uint16 }
   226  		c int64
   227  	}
   228  
   229  	atomic.AddInt64(&s1.a, 9)  // want "address of non 64-bit aligned field .a passed to atomic.AddInt64"
   230  	atomic.AddUint64(&s1.b, 9) // want "address of non 64-bit aligned field .b passed to atomic.AddUint64"
   231  	atomic.AddInt64(&s1.c, 9)
   232  }