From 83feedf7bf7147021761fd8b5a2a157095fcabc9 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sun, 19 Feb 2012 00:12:31 -0500 Subject: [PATCH] gc: fix error for floating-point constant % R=ken2 CC=golang-dev https://golang.org/cl/5674108 --- src/cmd/gc/const.c | 8 +++++++ test/const1.go | 55 +++++++++++++++++++++++----------------------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/cmd/gc/const.c b/src/cmd/gc/const.c index 1136277dcca..abdd424394c 100644 --- a/src/cmd/gc/const.c +++ b/src/cmd/gc/const.c @@ -660,6 +660,14 @@ evconst(Node *n) } mpdivfltflt(v.u.fval, rv.u.fval); break; + case TUP(OMOD, CTFLT): + // The default case above would print 'ideal % ideal', + // which is not quite an ideal error. + if(!n->diag) { + yyerror("illegal constant expression: floating-point %% operation"); + n->diag = 1; + } + return; case TUP(OADD, CTCPLX): mpaddfltflt(&v.u.cval->real, &rv.u.cval->real); mpaddfltflt(&v.u.cval->imag, &rv.u.cval->imag); diff --git a/test/const1.go b/test/const1.go index b24d274faa4..f944cde96e2 100644 --- a/test/const1.go +++ b/test/const1.go @@ -16,11 +16,11 @@ const ( Int8 int8 = 101 Minus1 int8 = -1 Uint8 uint8 = 102 - Const = 103 + Const = 103 Float32 float32 = 104.5 Float64 float64 = 105.5 - ConstFloat = 106.5 + ConstFloat = 106.5 Big float64 = 1e300 String = "abc" @@ -38,32 +38,33 @@ var ( a8 = Int8 * Const / 100 // ERROR "overflow" a9 = Int8 * (Const / 100) // OK - b1 = Uint8 * Uint8 // ERROR "overflow" - b2 = Uint8 * -1 // ERROR "overflow" - b3 = Uint8 - Uint8 // OK - b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow" - b5 = uint8(^0) // ERROR "overflow" - b6 = ^uint8(0) // OK - b7 = uint8(Minus1) // ERROR "overflow" - b8 = uint8(int8(-1)) // ERROR "overflow" - b8a = uint8(-1) // ERROR "overflow" - b9 byte = (1 << 10) >> 8 // OK - b10 byte = (1 << 10) // ERROR "overflow" - b11 byte = (byte(1) << 10) >> 8 // ERROR "overflow" - b12 byte = 1000 // ERROR "overflow" - b13 byte = byte(1000) // ERROR "overflow" - b14 byte = byte(100) * byte(100) // ERROR "overflow" - b15 byte = byte(100) * 100 // ERROR "overflow" - b16 byte = byte(0) * 1000 // ERROR "overflow" - b16a byte = 0 * 1000 // OK - b17 byte = byte(0) * byte(1000) // ERROR "overflow" - b18 byte = Uint8 / 0 // ERROR "division by zero" + b1 = Uint8 * Uint8 // ERROR "overflow" + b2 = Uint8 * -1 // ERROR "overflow" + b3 = Uint8 - Uint8 // OK + b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow" + b5 = uint8(^0) // ERROR "overflow" + b6 = ^uint8(0) // OK + b7 = uint8(Minus1) // ERROR "overflow" + b8 = uint8(int8(-1)) // ERROR "overflow" + b8a = uint8(-1) // ERROR "overflow" + b9 byte = (1 << 10) >> 8 // OK + b10 byte = (1 << 10) // ERROR "overflow" + b11 byte = (byte(1) << 10) >> 8 // ERROR "overflow" + b12 byte = 1000 // ERROR "overflow" + b13 byte = byte(1000) // ERROR "overflow" + b14 byte = byte(100) * byte(100) // ERROR "overflow" + b15 byte = byte(100) * 100 // ERROR "overflow" + b16 byte = byte(0) * 1000 // ERROR "overflow" + b16a byte = 0 * 1000 // OK + b17 byte = byte(0) * byte(1000) // ERROR "overflow" + b18 byte = Uint8 / 0 // ERROR "division by zero" - c1 float64 = Big - c2 float64 = Big * Big // ERROR "overflow" - c3 float64 = float64(Big) * Big // ERROR "overflow" - c4 = Big * Big // ERROR "overflow" - c5 = Big / 0 // ERROR "division by zero" + c1 float64 = Big + c2 float64 = Big * Big // ERROR "overflow" + c3 float64 = float64(Big) * Big // ERROR "overflow" + c4 = Big * Big // ERROR "overflow" + c5 = Big / 0 // ERROR "division by zero" + c6 = 1000 % 1e3 // ERROR "floating-point % operation" ) func f(int)