diff options
author | Russ Cox <rsc@golang.org> | 2009-03-12 19:04:38 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-03-12 19:04:38 -0700 |
commit | e858a2016e5a7553985dc0f871a96700a65518aa (patch) | |
tree | 215ba82d2713550ef0ead44a627dd78579275bfa /test | |
parent | 0de1c1a72174fce2275aaa474c3f538b866c7105 (diff) | |
download | golang-e858a2016e5a7553985dc0f871a96700a65518aa.tar.gz |
make 6g constants behave as ken proposes. (i hope.)
various bug fixes and tests involving constants.
test/const1.go is the major new test case.
R=ken
OCL=26216
CL=26224
Diffstat (limited to 'test')
-rw-r--r-- | test/const.go | 6 | ||||
-rw-r--r-- | test/const1.go | 79 | ||||
-rw-r--r-- | test/const2.go | 12 | ||||
-rw-r--r-- | test/const3.go | 29 | ||||
-rw-r--r-- | test/convert.go | 53 | ||||
-rw-r--r-- | test/convlit.go | 8 | ||||
-rwxr-xr-x | test/errchk | 4 | ||||
-rw-r--r-- | test/fixedbugs/bug090.go | 9 | ||||
-rw-r--r-- | test/fixedbugs/bug127.go | 4 | ||||
-rw-r--r-- | test/golden.out | 46 |
10 files changed, 210 insertions, 40 deletions
diff --git a/test/const.go b/test/const.go index 85c0a91b6..8e587cfe5 100644 --- a/test/const.go +++ b/test/const.go @@ -14,6 +14,9 @@ const ( c1 = chuge >> 100; c3div2 = 3/2; c1e3 = 1e3; + + ctrue = true; + cfalse = !ctrue; ) const ( @@ -111,4 +114,7 @@ func floats() { func main() { ints(); floats(); + + assert(ctrue == true, "ctrue == true"); + assert(cfalse == false, "cfalse == false"); } diff --git a/test/const1.go b/test/const1.go new file mode 100644 index 000000000..09125a121 --- /dev/null +++ b/test/const1.go @@ -0,0 +1,79 @@ +// errchk $G -e $F.go + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +type I interface {} +const ( + // assume all types behave similarly to int8/uint8 + Int8 int8 = 101; + Minus1 int8 = -1; + Uint8 uint8 = 102; + Const = 103; + + Float32 float32 = 104.5; + Float float = 105.5; + ConstFloat = 106.5; + Big float64 = 1e300; + + String = "abc"; + Bool = true; +) + +var ( + a1 = Int8 * 100; // ERROR "overflows" + a2 = Int8 * -1; // OK + a3 = Int8 * 1000; // ERROR "overflows" + a4 = Int8 * int8(1000); // ERROR "overflows" + a5 = int8(Int8 * 1000); // ERROR "overflows" + a6 = int8(Int8 * int8(1000)); // ERROR "overflows" + a7 = Int8 - 2*Int8 - 2*Int8; // ERROR "overflows" + a8 = Int8 * Const / 100; // ERROR "overflows" + a9 = Int8 * (Const / 100); // OK + + b1 = Uint8 * Uint8; // ERROR "overflows" + b2 = Uint8 * -1; // ERROR "overflows" + b3 = Uint8 - Uint8; // OK + b4 = Uint8 - Uint8 - Uint8; // ERROR "overflows" + b5 = uint8(^0); // ERROR "overflows" + b6 = ^uint8(0); // ERROR "overflows" + b7 = uint8(Minus1); // ERROR "overflows" + b8 = uint8(int8(-1)); // ERROR "overflows" + b8a = uint8(-1); // ERROR "overflows" + b9 byte = (1<<10) >> 8; // OK + b10 byte = (1<<10); // ERROR "overflows" + b11 byte = (byte(1)<<10) >> 8; // ERROR "overflows" + b12 byte = 1000; // ERROR "overflows" + b13 byte = byte(1000); // ERROR "overflows" + b14 byte = byte(100) * byte(100); // ERROR "overflows" + b15 byte = byte(100) * 100; // ERROR "overflows" + b16 byte = byte(0) * 1000; // ERROR "overflows" + b16a byte = 0 * 1000; // OK + b17 byte = byte(0) * byte(1000); // ERROR "overflows" + b18 byte = Uint8/0; // ERROR "division by zero" + + c1 float64 = Big; + c2 float64 = Big*Big; // ERROR "overflows" + c3 float64 = float64(Big)*Big; // ERROR "overflows" + c4 = Big*Big; // ERROR "overflows" + c5 = Big/0; // ERROR "division by zero" +) + +func f(int); + +func main() { + f(Int8); // ERROR "convert" + f(Minus1); // ERROR "convert" + f(Uint8); // ERROR "convert" + f(Const); // OK + f(Float32); // ERROR "convert" + f(Float); // ERROR "convert" + f(ConstFloat); // ERROR "truncate" + f(ConstFloat - 0.5); // OK + f(Big); // ERROR "convert" + f(String); // ERROR "convert" + f(Bool); // ERROR "convert" +} diff --git a/test/const2.go b/test/const2.go new file mode 100644 index 000000000..b3b10de8e --- /dev/null +++ b/test/const2.go @@ -0,0 +1,12 @@ +// errchk $G $D/$F.go + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +const ( + A int = 1; + B byte; // ERROR "type without expr" +) diff --git a/test/const3.go b/test/const3.go new file mode 100644 index 000000000..fc734377e --- /dev/null +++ b/test/const3.go @@ -0,0 +1,29 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import "fmt" + +type T int +func (t T) String() string { + return fmt.Sprintf("T%d", t); +} + +const ( + A T = 1<<(1<<iota); + B; + C; + D; + E; +) + +func main() { + s := fmt.Sprintf("%v %v %v %v %v", A, B, C, D, E); + if s != "T2 T4 T16 T256 T65536" { + panicln("type info didn't propagate in const: got", s); + } +} diff --git a/test/convert.go b/test/convert.go new file mode 100644 index 000000000..11369e521 --- /dev/null +++ b/test/convert.go @@ -0,0 +1,53 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import "unsafe" + +func typeof(x interface{}) string { + val, typ, indir := sys.Reflect(x); + return typ; +} + +func f() int { + return 0; +} + +func g() int { + return 0; +} + +type T func() int + +var m = map[string] T { + "f": f +} + +type A int +type B int + +var a A = 1; +var b B = 2; +var x int; + +func main() { + want := typeof(g); + if t := typeof(f); t != want { + panicln("type of f is", t, "want", want); + } + + want = typeof(x); + if t := typeof(+a); t != want { + panicln("type of +a is", t, "want", want); + } + if t := typeof(a+0); t != want { + panicln("type of a+0 is", t, "want", want); + } + if t := typeof(a+b); t != want { + panicln("type of a+b is", t, "want", want); + } +} diff --git a/test/convlit.go b/test/convlit.go index 23dab0a9c..cfa772712 100644 --- a/test/convlit.go +++ b/test/convlit.go @@ -4,6 +4,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// ! errchk $G -e $D/$F.go + package main // explicit conversions are okay, even if they overflow @@ -20,10 +22,10 @@ var s string; var bad1 string = 1; // ERROR "conver|incompatible" var bad2 = s + 1; // ERROR "conver|incompatible" var bad3 = s + 'a'; // ERROR "conver|incompatible" -var bad4 = "a" + 1; // ERROR "literals|incompatible" -var bad5 = "a" + 'a'; // ERROR "literals|incompatible" +var bad4 = "a" + 1; // ERROR "literals|incompatible|convert" +var bad5 = "a" + 'a'; // ERROR "literals|incompatible|convert" -var bad6 int = 1.5; // ERROR "convert" +var bad6 int = 1.5; // ERROR "convert|truncate" var bad7 int = 1e100; // ERROR "overflow" var bad8 float32 = 1e200; // ERROR "overflow" diff --git a/test/errchk b/test/errchk index 2b602c3c7..c85f36f9b 100755 --- a/test/errchk +++ b/test/errchk @@ -63,11 +63,11 @@ pr -n -t $SOURCEFILE | grep '// ERROR' | while read line; do mv -f $TMPTMP $TMPALL if test -z "$errmsg"; then bug - echo 1>&2 "errchk: $SOURCEFILE: missing expected error message on line $lineno: '$regexp'" + echo 1>&2 "errchk: $SOURCEFILE:$lineno: missing expected error: '$regexp'" echo 1 > $TMPSTAT elif ! echo "$errmsg" | egrep -q "$regexp"; then bug - echo 1>&2 "errchk: $SOURCEFILE: error message on line $lineno does not match '$regexp'" + echo 1>&2 "errchk: $SOURCEFILE:$lineno: error message does not match '$regexp'" echo 1>&2 $errmsg echo 1 > $TMPSTAT fi diff --git a/test/fixedbugs/bug090.go b/test/fixedbugs/bug090.go index 0654cff9d..2b6f7deb8 100644 --- a/test/fixedbugs/bug090.go +++ b/test/fixedbugs/bug090.go @@ -33,13 +33,14 @@ func main() { f = f3div2; assert(f == f3div2, "f == f3div2"); - i = f3div2; // BUG: probably shouldn't compile + i = f3div2; // ERROR "truncate" assert(i == c3div2, "i == c3div2 from f3div2"); - assert(i != f3div2, "i != f3div2"); // BUG: certainly shouldn't fail + assert(i != f3div2, "i != f3div2"); // ERROR "truncate" const g float64 = 1.0; - i = g; // BUG: shouldn't compile + i = g; // ERROR "convert" const h float64 = 3.14; - i = h; // BUG: certainly shouldn't compile + i = h; // ERROR "convert" + i = int(h); // ERROR "truncate" } diff --git a/test/fixedbugs/bug127.go b/test/fixedbugs/bug127.go index b463d233c..604b43eff 100644 --- a/test/fixedbugs/bug127.go +++ b/test/fixedbugs/bug127.go @@ -7,6 +7,6 @@ package main func main() { var x int64 = 0; - println(x != nil); // ERROR "illegal|incompatible" - println(0 != nil); // ERROR "illegal|incompatible" + println(x != nil); // ERROR "illegal|incompatible|nil constant" + println(0 != nil); // ERROR "illegal|incompatible|nil constant" } diff --git a/test/golden.out b/test/golden.out index df7be75c1..c91bb450f 100644 --- a/test/golden.out +++ b/test/golden.out @@ -32,17 +32,12 @@ pc: xxx =========== ./convlit.go -BUG: errchk: ./convlit.go: missing expected error message on line 16: 'conver|incompatible' -errchk: ./convlit.go: missing expected error message on line 22: 'convert' -errchk: ./convlit.go: missing expected error message on line 23: 'overflow' -errchk: ./convlit.go: missing expected error message on line 24: 'overflow' -errchk: ./convlit.go: unmatched error messages: +BUG: errchk: ./convlit.go: unmatched error messages: ================================================== -./convlit.go:8: cannot convert non-integer constant to int -./convlit.go:11: overflow converting constant to int -./convlit.go:12: overflow converting constant to float -./convlit.go:8: cannot convert non-integer constant to int -./convlit.go:8: fatal error: too many errors +./convlit.go:8: constant 1.5 truncated to integer +./convlit.go:11: constant 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 overflows int +./convlit.go:12: constant 34911850510716223476646871064527264675788468424693128821036252992306087892081078460155404277013793117885253p+2968 overflows float +./convlit.go:9: constant 1.5 truncated to integer ================================================== =========== ./helloworld.go @@ -116,7 +111,7 @@ bugs/bug108.go:4: stupid shift: 1025 BUG: errchk: command succeeded unexpectedly: 6g bugs/bug108.go =========== bugs/bug115.go -bugs/bug115.go:8: overflow converting constant to uint +bugs/bug115.go:8: constant -1 overflows uint BUG: bug115 should compile =========== bugs/bug117.go @@ -129,7 +124,9 @@ BUG: should compile BUG: errchk: command succeeded unexpectedly: 6g bugs/bug125.go =========== bugs/bug131.go -BUG: should not compile +bugs/bug131.go:7: cannot convert uint64 constant to int64 +bugs/bug131.go:7: illegal types for operand: AS + int64 =========== bugs/bug132.go BUG: compilation succeeds incorrectly @@ -138,12 +135,11 @@ BUG: compilation succeeds incorrectly BUG: should not compile =========== bugs/bug138.go -bugs/bug138.go:8: overflow converting constant to uint -bugs/bug138.go:8: illegal combination of literals CONV 7 +bugs/bug138.go:8: constant -1 overflows uint BUG should compile =========== fixedbugs/bug016.go -fixedbugs/bug016.go:7: overflow converting constant to uint +fixedbugs/bug016.go:7: constant -3 overflows uint =========== fixedbugs/bug027.go hi @@ -177,7 +173,7 @@ fixedbugs/bug039.go:6: variable x redeclared in this block fixedbugs/bug041.go:5: export of incomplete type t =========== fixedbugs/bug049.go -fixedbugs/bug049.go:6: illegal conversion of nil to string +fixedbugs/bug049.go:6: cannot convert nil constant to string =========== fixedbugs/bug050.go fixedbugs/bug050.go:3: package statement must be first @@ -187,7 +183,7 @@ sys.6:1 fixedbugs/bug050.go:3: syntax error near package fixedbugs/bug051.go:10: expression must be a constant =========== fixedbugs/bug062.go -fixedbugs/bug062.go:6: illegal conversion of nil to string +fixedbugs/bug062.go:6: cannot convert nil constant to string fixedbugs/bug062.go:6: illegal types for operand: AS string @@ -210,9 +206,13 @@ fixedbugs/bug072.go:6: bug: undefined fixedbugs/bug073.go:8: illegal types for operand: LSH int int +fixedbugs/bug073.go:8: illegal types for operand: AS + int fixedbugs/bug073.go:9: illegal types for operand: RSH int int +fixedbugs/bug073.go:9: illegal types for operand: AS + int =========== fixedbugs/bug074.go fixedbugs/bug074.go:6: invalid type for composite literal: string @@ -227,18 +227,6 @@ fixedbugs/bug083.dir/bug1.go:9: syntax error near t0 =========== fixedbugs/bug086.go fixedbugs/bug086.go:5: function ends without a return statement -=========== fixedbugs/bug090.go -fixedbugs/bug090.go:32: cannot convert non-integer constant to int -fixedbugs/bug090.go:32: illegal types for operand: AS - int -fixedbugs/bug090.go:34: cannot convert non-integer constant to int -fixedbugs/bug090.go:34: illegal types for operand: CALL - bool -fixedbugs/bug090.go:40: cannot convert non-integer constant to int -fixedbugs/bug090.go:40: illegal types for operand: AS - int - float64 - =========== fixedbugs/bug091.go fixedbugs/bug091.go:15: c: undefined fixedbugs/bug091.go:15: illegal types for operand: AS |