3 // Copyright 2021 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.
7 // Check that the compiler refuses excessively long constants.
23 // testProg creates a package called name, with path dir/name.go,
24 // which declares an untyped constant of the given length.
25 // testProg compiles this package and checks for the absence or
26 // presence of a constant literal error.
27 func testProg(dir, name string, length int, msg string) {
31 "package %s; const _ = 0b%s // %d bits",
32 name, strings.Repeat("1", length), length,
35 filename := filepath.Join(dir, fmt.Sprintf("%s.go", name))
36 if err := os.WriteFile(filename, buf.Bytes(), 0666); err != nil {
40 cmd := exec.Command("go", "tool", "compile", "-p=p", filename)
42 output, err := cmd.CombinedOutput()
47 log.Fatalf("%s: compile failed unexpectedly: %v", name, err)
54 log.Fatalf("%s: compile succeeded unexpectedly", name)
56 if !bytes.Contains(output, []byte(msg)) {
57 log.Fatalf("%s: wrong compiler error message:\n%s\n", name, output)
62 if runtime.GOOS == "js" || runtime.GOOS == "wasip1" || runtime.Compiler != "gc" {
66 dir, err := ioutil.TempDir("", "const7_")
68 log.Fatalf("creating temp dir: %v\n", err)
70 defer os.RemoveAll(dir)
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")