Skip to content

Commit fd208c8

Browse files
committed
cmd/compile: remove constant arithmetic overflows during typecheck
Since go1.19, these errors are already reported by types2 for any user's Go code. Compiler generated code, which looks like constant expression should be evaluated as non-constant semantic, which allows overflows. Fixes #58293 Change-Id: I6f0049a69bdb0a8d0d7a0db49c7badaa92598ea2 Reviewed-on: https://go-review.googlesource.com/c/go/+/465096 Reviewed-by: Matthew Dempsky <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Tobias Klauser <[email protected]>
1 parent 8fb9565 commit fd208c8

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

src/cmd/compile/internal/typecheck/const.go

+2-34
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ func roundFloat(v constant.Value, sz int64) constant.Value {
3434
// truncate float literal fv to 32-bit or 64-bit precision
3535
// according to type; return truncated value.
3636
func truncfltlit(v constant.Value, t *types.Type) constant.Value {
37-
if t.IsUntyped() || overflow(v, t) {
38-
// If there was overflow, simply continuing would set the
39-
// value to Inf which in turn would lead to spurious follow-on
40-
// errors. Avoid this by returning the existing value.
37+
if t.IsUntyped() {
4138
return v
4239
}
4340

@@ -48,10 +45,7 @@ func truncfltlit(v constant.Value, t *types.Type) constant.Value {
4845
// precision, according to type; return truncated value. In case of
4946
// overflow, calls Errorf but does not truncate the input value.
5047
func trunccmplxlit(v constant.Value, t *types.Type) constant.Value {
51-
if t.IsUntyped() || overflow(v, t) {
52-
// If there was overflow, simply continuing would set the
53-
// value to Inf which in turn would lead to spurious follow-on
54-
// errors. Avoid this by returning the existing value.
48+
if t.IsUntyped() {
5549
return v
5650
}
5751

@@ -251,7 +245,6 @@ func convertVal(v constant.Value, t *types.Type, explicit bool) constant.Value {
251245
switch {
252246
case t.IsInteger():
253247
v = toint(v)
254-
overflow(v, t)
255248
return v
256249
case t.IsFloat():
257250
v = toflt(v)
@@ -273,9 +266,6 @@ func tocplx(v constant.Value) constant.Value {
273266

274267
func toflt(v constant.Value) constant.Value {
275268
if v.Kind() == constant.Complex {
276-
if constant.Sign(constant.Imag(v)) != 0 {
277-
base.Errorf("constant %v truncated to real", v)
278-
}
279269
v = constant.Real(v)
280270
}
281271

@@ -284,9 +274,6 @@ func toflt(v constant.Value) constant.Value {
284274

285275
func toint(v constant.Value) constant.Value {
286276
if v.Kind() == constant.Complex {
287-
if constant.Sign(constant.Imag(v)) != 0 {
288-
base.Errorf("constant %v truncated to integer", v)
289-
}
290277
v = constant.Real(v)
291278
}
292279

@@ -321,25 +308,6 @@ func toint(v constant.Value) constant.Value {
321308
return constant.MakeInt64(1)
322309
}
323310

324-
// overflow reports whether constant value v is too large
325-
// to represent with type t, and emits an error message if so.
326-
func overflow(v constant.Value, t *types.Type) bool {
327-
// v has already been converted
328-
// to appropriate form for t.
329-
if t.IsUntyped() {
330-
return false
331-
}
332-
if v.Kind() == constant.Int && constant.BitLen(v) > ir.ConstPrec {
333-
base.Errorf("integer too large")
334-
return true
335-
}
336-
if ir.ConstOverflow(v, t) {
337-
base.Errorf("constant %v overflows %v", types.FmtConst(v, false), t)
338-
return true
339-
}
340-
return false
341-
}
342-
343311
func tostr(v constant.Value) constant.Value {
344312
if v.Kind() == constant.Int {
345313
r := unicode.ReplacementChar

test/fixedbugs/issue58293.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile
2+
3+
// Copyright 2023 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+
package p
8+
9+
var bar = f(13579)
10+
11+
func f(x uint16) uint16 {
12+
return x>>8 | x<<8
13+
}

0 commit comments

Comments
 (0)