Skip to content

Commit e7a10a9

Browse files
griesemeradonovan
authored andcommitted
go/types: fix internal inInteger operand predicate
When testing if a value is an integer, if the value is a constant, don't ignore the type if it has one. Fixes #11594. Change-Id: I2ff387e4f9e8ab7cae35c4838350e0a1fce2e625 Reviewed-on: https://go-review.googlesource.com/12045 Reviewed-by: Alan Donovan <[email protected]>
1 parent 8b6527b commit e7a10a9

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/go/types/operand.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,10 @@ func (x *operand) assignableTo(conf *Config, T Type) bool {
278278
return false
279279
}
280280

281-
// isInteger reports whether x is a (typed or untyped) integer value.
281+
// isInteger reports whether x is value of integer type
282+
// or an untyped constant representable as an integer.
282283
func (x *operand) isInteger() bool {
283284
return x.mode == invalid ||
284285
isInteger(x.typ) ||
285-
x.mode == constant && representableConst(x.val, nil, UntypedInt, nil) // no *Config required for UntypedInt
286+
isUntyped(x.typ) && x.mode == constant && representableConst(x.val, nil, UntypedInt, nil) // no *Config required for UntypedInt
286287
}

src/go/types/testdata/decls0.src

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type (
5353
iA1 [1 /* ERROR "invalid array length" */ <<100]int
5454
iA2 [- /* ERROR "invalid array length" */ 1]complex128
5555
iA3 ["foo" /* ERROR "must be integer" */ ]string
56+
iA4 [float64 /* ERROR "must be integer" */ (0)]int
5657
)
5758

5859

src/go/types/testdata/shifts.src

+8
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,11 @@ func issue11325() {
331331
_ = 1. >> 1.
332332
_ = 1.1 /* ERROR "must be integer" */ >> 1
333333
}
334+
335+
func issue11594() {
336+
var _ = complex64 /* ERROR "must be integer" */ (1) << 2 // example from issue 11594
337+
_ = float32 /* ERROR "must be integer" */ (0) << 1
338+
_ = float64 /* ERROR "must be integer" */ (0) >> 2
339+
_ = complex64 /* ERROR "must be integer" */ (0) << 3
340+
_ = complex64 /* ERROR "must be integer" */ (0) >> 4
341+
}

0 commit comments

Comments
 (0)