Skip to content

Commit 6bc5938

Browse files
valyalajosharian
authored andcommitted
cmd/vet: eliminate "might be too small for shift" warnings
Determine int, uint and uintptr bit sizes from GOARCH environment variable if it is set. Otherwise use host-specific sizes. Fixes #19321 Change-Id: I494b8e4b49b59d32794f50ff2ce06ba040cb8460 Reviewed-on: https://go-review.googlesource.com/37950 Run-TryBot: Josh Bleecher Snyder <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent fe34585 commit 6bc5938

File tree

7 files changed

+102
-96
lines changed

7 files changed

+102
-96
lines changed

src/cmd/vet/all/whitelist/32bit.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 32bit-specific vet whitelist. See readme.txt for details.
2+
3+
// TODO: fix these warnings after the CL 37950 .
4+
math/big/float.go: x[i] (32 bits) too small for shift of 32
5+
math/big/nat.go: Word(rand.Uint32()) (32 bits) too small for shift of 32
6+
runtime/malloc.go: uintptr(i) (32 bits) too small for shift of 40
7+
runtime/malloc.go: uintptr(i) (32 bits) too small for shift of 40
8+
runtime/malloc.go: uintptr(i) (32 bits) too small for shift of 40
9+
sync/atomic/atomic_test.go: uintptr(seed + i) (32 bits) too small for shift of 32
10+
sync/atomic/atomic_test.go: uintptr(seed+i) << 32 (32 bits) too small for shift of 32
11+
sync/atomic/atomic_test.go: uintptr(seed + i) (32 bits) too small for shift of 32
12+
sync/atomic/atomic_test.go: old (32 bits) too small for shift of 32
13+
sync/atomic/atomic_test.go: old << 32 (32 bits) too small for shift of 32
14+
sync/atomic/atomic_test.go: old (32 bits) too small for shift of 32
15+
sync/atomic/atomic_test.go: v (32 bits) too small for shift of 32
16+
sync/atomic/atomic_test.go: v (32 bits) too small for shift of 32

src/cmd/vet/all/whitelist/64bit.txt

-26
This file was deleted.

src/cmd/vet/all/whitelist/all.txt

-18
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,6 @@ encoding/xml/marshal_test.go: method MarshalXML(e *Encoder, start StartElement)
5151
encoding/xml/read.go: method UnmarshalXML(d *Decoder, start StartElement) error should have signature UnmarshalXML(*xml.Decoder, xml.StartElement) error
5252
encoding/xml/read_test.go: method UnmarshalXML(d *Decoder, start StartElement) error should have signature UnmarshalXML(*xml.Decoder, xml.StartElement) error
5353

54-
// Lots of false positives from the "large shift" check.
55-
// Mostly code that uses clever const tricks to determine
56-
// or use the size of an int or pointer (and related values).
57-
image/png/paeth.go: x might be too small for shift of 63
58-
math/big/float.go: x[i] might be too small for shift of 32
59-
math/big/nat.go: Word(rand.Uint32()) might be too small for shift of 32
60-
runtime/malloc.go: uintptr(i) might be too small for shift of 40
61-
runtime/malloc.go: uintptr(i) might be too small for shift of 40
62-
runtime/malloc.go: uintptr(i) might be too small for shift of 40
63-
sync/atomic/atomic_test.go: uintptr(seed + i) might be too small for shift of 32
64-
sync/atomic/atomic_test.go: uintptr(seed+i) << 32 might be too small for shift of 32
65-
sync/atomic/atomic_test.go: uintptr(seed + i) might be too small for shift of 32
66-
sync/atomic/atomic_test.go: old might be too small for shift of 32
67-
sync/atomic/atomic_test.go: old << 32 might be too small for shift of 32
68-
sync/atomic/atomic_test.go: old might be too small for shift of 32
69-
sync/atomic/atomic_test.go: v might be too small for shift of 32
70-
sync/atomic/atomic_test.go: v might be too small for shift of 32
71-
7254
// Long struct tags used to test reflect internals
7355
cmd/link/link_test.go: struct field tag "\n\tLondon. Michaelmas term lately over, and the Lord Chancellor sitting in Lincoln’s Inn Hall. Implacable November weather. As much mud in the streets as if the waters had but newly retired from the face of the earth, and it would not be wonderful to meet a Megalosaurus, forty feet long or so, waddling like an elephantine lizard up Holborn Hill. Smoke lowering down from chimney-pots, making a soft black drizzle, with flakes of soot in it as big as full-grown snowflakes—gone into mourning, one might imagine, for the death of the sun. Dogs, undistinguishable in mire. Horses, scarcely better; splashed to their very blinkers. Foot passengers, jostling one another’s umbrellas in a general infection of ill temper, and losing their foot-hold at street-corners, where tens of thousands of other foot passengers have been slipping and sliding since the day broke (if this day ever broke), adding new deposits to the crust upon crust of mud, sticking at those points tenaciously to the pavement, and accumulating at compound interest.\n\n\tFog everywhere. Fog up the river, where it flows among green aits and meadows; fog down the river, where it rolls defiled among the tiers of shipping and the waterside pollutions of a great (and dirty) city. Fog on the Essex marshes, fog on the Kentish heights. Fog creeping into the cabooses of collier-brigs; fog lying out on the yards and hovering in the rigging of great ships; fog drooping on the gunwales of barges and small boats. Fog in the eyes and throats of ancient Greenwich pensioners, wheezing by the firesides of their wards; fog in the stem and bowl of the afternoon pipe of the wrathful skipper, down in his close cabin; fog cruelly pinching the toes and fingers of his shivering little ‘prentice boy on deck. Chance people on the bridges peeping over the parapets into a nether sky of fog, with fog all round them, as if they were up in a balloon and hanging in the misty clouds.\n\n\tGas looming through the fog in divers places in the streets, much as the sun may, from the spongey fields, be seen to loom by husbandman and ploughboy. Most of the shops lighted two hours before their time—as the gas seems to know, for it has a haggard and unwilling look.\n\n\tThe raw afternoon is rawest, and the dense fog is densest, and the muddy streets are muddiest near that leaden-headed old obstruction, appropriate ornament for the threshold of a leaden-headed old corporation, Temple Bar. And hard by Temple Bar, in Lincoln’s Inn Hall, at the very heart of the fog, sits the Lord High Chancellor in his High Court of Chancery." not compatible with reflect.StructTag.Get: bad syntax for struct tag key
7456
cmd/link/link_test.go: struct field tag "\n\tIt was grand to see how the wind awoke, and bent the trees, and drove the rain before it like a cloud of smoke; and to hear the solemn thunder, and to see the lightning; and while thinking with awe of the tremendous powers by which our little lives are encompassed, to consider how beneficent they are, and how upon the smallest flower and leaf there was already a freshness poured from all this seeming rage, which seemed to make creation new again." not compatible with reflect.StructTag.Get: bad syntax for struct tag key

src/cmd/vet/shift.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ func checkLongShift(f *File, node ast.Node, x, y ast.Expr) {
8787
return
8888
}
8989
var size int64
90-
var msg string
9190
switch b.Kind() {
9291
case types.Uint8, types.Int8:
9392
size = 8
@@ -97,15 +96,20 @@ func checkLongShift(f *File, node ast.Node, x, y ast.Expr) {
9796
size = 32
9897
case types.Uint64, types.Int64:
9998
size = 64
100-
case types.Int, types.Uint, types.Uintptr:
101-
// These types may be as small as 32 bits, but no smaller.
102-
size = 32
103-
msg = "might be "
99+
case types.Int, types.Uint:
100+
size = uintBitSize
101+
case types.Uintptr:
102+
size = uintptrBitSize
104103
default:
105104
return
106105
}
107106
if amt >= size {
108107
ident := f.gofmt(x)
109-
f.Badf(node.Pos(), "%s %stoo small for shift of %d", ident, msg, amt)
108+
f.Badf(node.Pos(), "%s (%d bits) too small for shift of %d", ident, size, amt)
110109
}
111110
}
111+
112+
var (
113+
uintBitSize = 8 * archSizes.Sizeof(types.Typ[types.Uint])
114+
uintptrBitSize = 8 * archSizes.Sizeof(types.Typ[types.Uintptr])
115+
)

src/cmd/vet/testdata/shift.go

+66-45
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,93 @@ import "unsafe"
1111
func ShiftTest() {
1212
var i8 int8
1313
_ = i8 << 7
14-
_ = (i8 + 1) << 8 // ERROR "\(i8 \+ 1\) too small for shift of 8"
15-
_ = i8 << (7 + 1) // ERROR "i8 too small for shift of 8"
16-
_ = i8 >> 8 // ERROR "i8 too small for shift of 8"
17-
i8 <<= 8 // ERROR "i8 too small for shift of 8"
18-
i8 >>= 8 // ERROR "i8 too small for shift of 8"
14+
_ = (i8 + 1) << 8 // ERROR ".i8 . 1. .8 bits. too small for shift of 8"
15+
_ = i8 << (7 + 1) // ERROR "i8 .8 bits. too small for shift of 8"
16+
_ = i8 >> 8 // ERROR "i8 .8 bits. too small for shift of 8"
17+
i8 <<= 8 // ERROR "i8 .8 bits. too small for shift of 8"
18+
i8 >>= 8 // ERROR "i8 .8 bits. too small for shift of 8"
1919
var i16 int16
2020
_ = i16 << 15
21-
_ = i16 << 16 // ERROR "i16 too small for shift of 16"
22-
_ = i16 >> 16 // ERROR "i16 too small for shift of 16"
23-
i16 <<= 16 // ERROR "i16 too small for shift of 16"
24-
i16 >>= 16 // ERROR "i16 too small for shift of 16"
21+
_ = i16 << 16 // ERROR "i16 .16 bits. too small for shift of 16"
22+
_ = i16 >> 16 // ERROR "i16 .16 bits. too small for shift of 16"
23+
i16 <<= 16 // ERROR "i16 .16 bits. too small for shift of 16"
24+
i16 >>= 16 // ERROR "i16 .16 bits. too small for shift of 16"
2525
var i32 int32
2626
_ = i32 << 31
27-
_ = i32 << 32 // ERROR "i32 too small for shift of 32"
28-
_ = i32 >> 32 // ERROR "i32 too small for shift of 32"
29-
i32 <<= 32 // ERROR "i32 too small for shift of 32"
30-
i32 >>= 32 // ERROR "i32 too small for shift of 32"
27+
_ = i32 << 32 // ERROR "i32 .32 bits. too small for shift of 32"
28+
_ = i32 >> 32 // ERROR "i32 .32 bits. too small for shift of 32"
29+
i32 <<= 32 // ERROR "i32 .32 bits. too small for shift of 32"
30+
i32 >>= 32 // ERROR "i32 .32 bits. too small for shift of 32"
3131
var i64 int64
3232
_ = i64 << 63
33-
_ = i64 << 64 // ERROR "i64 too small for shift of 64"
34-
_ = i64 >> 64 // ERROR "i64 too small for shift of 64"
35-
i64 <<= 64 // ERROR "i64 too small for shift of 64"
36-
i64 >>= 64 // ERROR "i64 too small for shift of 64"
33+
_ = i64 << 64 // ERROR "i64 .64 bits. too small for shift of 64"
34+
_ = i64 >> 64 // ERROR "i64 .64 bits. too small for shift of 64"
35+
i64 <<= 64 // ERROR "i64 .64 bits. too small for shift of 64"
36+
i64 >>= 64 // ERROR "i64 .64 bits. too small for shift of 64"
3737
var u8 uint8
3838
_ = u8 << 7
39-
_ = u8 << 8 // ERROR "u8 too small for shift of 8"
40-
_ = u8 >> 8 // ERROR "u8 too small for shift of 8"
41-
u8 <<= 8 // ERROR "u8 too small for shift of 8"
42-
u8 >>= 8 // ERROR "u8 too small for shift of 8"
39+
_ = u8 << 8 // ERROR "u8 .8 bits. too small for shift of 8"
40+
_ = u8 >> 8 // ERROR "u8 .8 bits. too small for shift of 8"
41+
u8 <<= 8 // ERROR "u8 .8 bits. too small for shift of 8"
42+
u8 >>= 8 // ERROR "u8 .8 bits. too small for shift of 8"
4343
var u16 uint16
4444
_ = u16 << 15
45-
_ = u16 << 16 // ERROR "u16 too small for shift of 16"
46-
_ = u16 >> 16 // ERROR "u16 too small for shift of 16"
47-
u16 <<= 16 // ERROR "u16 too small for shift of 16"
48-
u16 >>= 16 // ERROR "u16 too small for shift of 16"
45+
_ = u16 << 16 // ERROR "u16 .16 bits. too small for shift of 16"
46+
_ = u16 >> 16 // ERROR "u16 .16 bits. too small for shift of 16"
47+
u16 <<= 16 // ERROR "u16 .16 bits. too small for shift of 16"
48+
u16 >>= 16 // ERROR "u16 .16 bits. too small for shift of 16"
4949
var u32 uint32
5050
_ = u32 << 31
51-
_ = u32 << 32 // ERROR "u32 too small for shift of 32"
52-
_ = u32 >> 32 // ERROR "u32 too small for shift of 32"
53-
u32 <<= 32 // ERROR "u32 too small for shift of 32"
54-
u32 >>= 32 // ERROR "u32 too small for shift of 32"
51+
_ = u32 << 32 // ERROR "u32 .32 bits. too small for shift of 32"
52+
_ = u32 >> 32 // ERROR "u32 .32 bits. too small for shift of 32"
53+
u32 <<= 32 // ERROR "u32 .32 bits. too small for shift of 32"
54+
u32 >>= 32 // ERROR "u32 .32 bits. too small for shift of 32"
5555
var u64 uint64
5656
_ = u64 << 63
57-
_ = u64 << 64 // ERROR "u64 too small for shift of 64"
58-
_ = u64 >> 64 // ERROR "u64 too small for shift of 64"
59-
u64 <<= 64 // ERROR "u64 too small for shift of 64"
60-
u64 >>= 64 // ERROR "u64 too small for shift of 64"
57+
_ = u64 << 64 // ERROR "u64 .64 bits. too small for shift of 64"
58+
_ = u64 >> 64 // ERROR "u64 .64 bits. too small for shift of 64"
59+
u64 <<= 64 // ERROR "u64 .64 bits. too small for shift of 64"
60+
u64 >>= 64 // ERROR "u64 .64 bits. too small for shift of 64"
6161
_ = u64 << u64 // Non-constant shifts should succeed.
62+
6263
var i int
6364
_ = i << 31
64-
_ = i << 32 // ERROR "i might be too small for shift of 32"
65-
_ = i >> 32 // ERROR "i might be too small for shift of 32"
66-
i <<= 32 // ERROR "i might be too small for shift of 32"
67-
i >>= 32 // ERROR "i might be too small for shift of 32"
65+
const in = 8 * unsafe.Sizeof(i)
66+
_ = i << in // ERROR "too small for shift"
67+
_ = i >> in // ERROR "too small for shift"
68+
i <<= in // ERROR "too small for shift"
69+
i >>= in // ERROR "too small for shift"
70+
const ix = 8*unsafe.Sizeof(i) - 1
71+
_ = i << ix
72+
_ = i >> ix
73+
i <<= ix
74+
i >>= ix
75+
6876
var u uint
6977
_ = u << 31
70-
_ = u << 32 // ERROR "u might be too small for shift of 32"
71-
_ = u >> 32 // ERROR "u might be too small for shift of 32"
72-
u <<= 32 // ERROR "u might be too small for shift of 32"
73-
u >>= 32 // ERROR "u might be too small for shift of 32"
78+
const un = 8 * unsafe.Sizeof(u)
79+
_ = u << un // ERROR "too small for shift"
80+
_ = u >> un // ERROR "too small for shift"
81+
u <<= un // ERROR "too small for shift"
82+
u >>= un // ERROR "too small for shift"
83+
const ux = 8*unsafe.Sizeof(u) - 1
84+
_ = u << ux
85+
_ = u >> ux
86+
u <<= ux
87+
u >>= ux
88+
7489
var p uintptr
7590
_ = p << 31
76-
_ = p << 32 // ERROR "p might be too small for shift of 32"
77-
_ = p >> 32 // ERROR "p might be too small for shift of 32"
78-
p <<= 32 // ERROR "p might be too small for shift of 32"
79-
p >>= 32 // ERROR "p might be too small for shift of 32"
91+
const pn = 8 * unsafe.Sizeof(p)
92+
_ = p << pn // ERROR "too small for shift"
93+
_ = p >> pn // ERROR "too small for shift"
94+
p <<= pn // ERROR "too small for shift"
95+
p >>= pn // ERROR "too small for shift"
96+
const px = 8*unsafe.Sizeof(p) - 1
97+
_ = p << px
98+
_ = p >> px
99+
p <<= px
100+
p >>= px
80101

81102
const oneIf64Bit = ^uint(0) >> 63 // allow large shifts of constants; they are used for 32/64 bit compatibility tricks
82103

src/cmd/vet/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package main
88

99
import (
1010
"go/ast"
11+
"go/build"
1112
"go/importer"
1213
"go/token"
1314
"go/types"
@@ -80,6 +81,8 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {
8081
// By providing a Config with our own error function, it will continue
8182
// past the first error. There is no need for that function to do anything.
8283
Error: func(error) {},
84+
85+
Sizes: archSizes,
8386
}
8487
info := &types.Info{
8588
Selections: pkg.selectors,
@@ -289,3 +292,5 @@ func (f *File) hasMethod(typ types.Type, name string) bool {
289292
_, ok := obj.(*types.Func)
290293
return ok
291294
}
295+
296+
var archSizes = types.SizesFor("gc", build.Default.GOARCH)

src/go/types/sizes.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ func SizesFor(compiler, arch string) Sizes {
181181
if compiler != "gc" {
182182
return nil
183183
}
184-
return gcArchSizes[arch]
184+
s, ok := gcArchSizes[arch]
185+
if !ok {
186+
return nil
187+
}
188+
return s
185189
}
186190

187191
// stdSizes is used if Config.Sizes == nil.

0 commit comments

Comments
 (0)