@@ -24,12 +24,12 @@ import (
24
24
// which declares an untyped constant of the given length.
25
25
// testProg compiles this package and checks for the absence or
26
26
// presence of a constant literal error.
27
- func testProg (dir , name string , length int , ok bool ) {
27
+ func testProg (dir , name string , length int , msg string ) {
28
28
var buf bytes.Buffer
29
29
30
30
fmt .Fprintf (& buf ,
31
- "package %s; const _ = %s // %d digits " ,
32
- name , strings .Repeat ("9 " , length ), length ,
31
+ "package %s; const _ = 0b %s // %d bits " ,
32
+ name , strings .Repeat ("1 " , length ), length ,
33
33
)
34
34
35
35
filename := filepath .Join (dir , fmt .Sprintf ("%s.go" , name ))
@@ -41,7 +41,7 @@ func testProg(dir, name string, length int, ok bool) {
41
41
cmd .Dir = dir
42
42
output , err := cmd .CombinedOutput ()
43
43
44
- if ok {
44
+ if msg == "" {
45
45
// no error expected
46
46
if err != nil {
47
47
log .Fatalf ("%s: compile failed unexpectedly: %v" , name , err )
@@ -53,7 +53,7 @@ func testProg(dir, name string, length int, ok bool) {
53
53
if err == nil {
54
54
log .Fatalf ("%s: compile succeeded unexpectedly" , name )
55
55
}
56
- if ! bytes .Contains (output , []byte ("excessively long constant" )) {
56
+ if ! bytes .Contains (output , []byte (msg )) {
57
57
log .Fatalf ("%s: wrong compiler error message:\n %s\n " , name , output )
58
58
}
59
59
}
@@ -69,7 +69,10 @@ func main() {
69
69
}
70
70
defer os .RemoveAll (dir )
71
71
72
- const limit = 10000 // compiler-internal constant length limit
73
- testProg (dir , "x1" , limit , true )
74
- testProg (dir , "x2" , limit + 1 , false )
72
+ const bitLimit = 512
73
+ const charLimit = 10000 // compiler-internal constant length limit
74
+ testProg (dir , "x1" , bitLimit , "" )
75
+ testProg (dir , "x2" , bitLimit + 1 , "constant overflow" )
76
+ testProg (dir , "x3" , charLimit - 2 , "constant overflow" ) // -2 because literal contains 0b prefix
77
+ testProg (dir , "x4" , charLimit - 1 , "excessively long constant" )
75
78
}
0 commit comments