}
// For matching historical "constant OP overflow" error messages.
+// TODO(mdempsky): Replace with error messages like go/types uses.
var overflowNames = [...]string{
- OADD: "addition",
- OSUB: "subtraction",
- OMUL: "multiplication",
- OLSH: "shift",
+ OADD: "addition",
+ OSUB: "subtraction",
+ OMUL: "multiplication",
+ OLSH: "shift",
+ OXOR: "bitwise XOR",
+ OBITNOT: "bitwise complement",
}
// origConst returns an OLITERAL with orig n and value v.
lineno = lno
switch v.Kind() {
+ case constant.Int:
+ if constant.BitLen(v) <= Mpprec {
+ break
+ }
+ fallthrough
case constant.Unknown:
- // If constant folding was attempted (we were called)
- // but it produced an invalid constant value,
- // mark n as broken and give up.
- if Errors() == 0 {
- Fatalf("should have reported an error")
+ what := overflowNames[n.Op]
+ if what == "" {
+ Fatalf("unexpected overflow: %v", n.Op)
}
+ yyerrorl(n.Pos, "constant %v overflow", what)
n.Type = nil
return n
-
- case constant.Int:
- if constant.BitLen(v) > Mpprec {
- what := overflowNames[n.Op]
- if what == "" {
- Fatalf("unexpected overflow: %v", n.Op)
- }
- yyerror("constant %v overflow", what)
- n.Type = nil
- return n
- }
}
orig := n
- n = nod(OLITERAL, nil, nil)
+ n = nodl(orig.Pos, OLITERAL, nil, nil)
n.Orig = orig
- n.Pos = orig.Pos
n.Type = orig.Type
n.SetVal(v)
return n
// nodlit returns a new untyped constant with value v.
func nodlit(v constant.Value) *Node {
n := nod(OLITERAL, nil, nil)
- n.Type = idealType(v.Kind())
- n.SetVal(v)
+ if k := v.Kind(); k != constant.Unknown {
+ n.Type = idealType(k)
+ n.SetVal(v)
+ }
return n
}
const LargeC = LargeB * LargeB * LargeB // GC_ERROR "constant multiplication overflow"
const AlsoLargeA = LargeA << 400 << 400 >> 400 >> 400 // GC_ERROR "constant shift overflow"
+
+// Issue #42732.
+
+const a = 1e+500000000
+const b = a * a // ERROR "constant multiplication overflow"
+const c = b * b
+
+const MaxInt512 = (1<<256 - 1) * (1<<256 + 1)
+const _ = MaxInt512 + 1 // ERROR "constant addition overflow"
+const _ = MaxInt512 ^ -1 // ERROR "constant bitwise XOR overflow"
+const _ = ^MaxInt512 // ERROR "constant bitwise complement overflow"