cmd/compile, go/types: disable constant string size check

The go/constant and go/types packages try very hard not to eagerly
construct string constant. The length check now calls
constant.StringVal effectively on every string constant, which
eagerly constructs the strings and can cause it to use
significantly more memory. We may need a different mechanism to do
the length check, like a new API that returns the length without
constructing the string. For now, disable the code.

Updates #78346.

Change-Id: I46a9586614150e7d5353803904e101d808a683f5
Reviewed-on: https://go-review.googlesource.com/c/go/+/772220
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Cherry Mui 2026-04-29 14:14:15 -04:00
parent cbaecb2830
commit d8cab4c45a
3 changed files with 11 additions and 3 deletions

View file

@ -50,7 +50,11 @@ func (check *Checker) overflow(x *operand, opPos syntax.Pos) {
}
const maxLen = int(2e9) // cmd/internal/obj.MaxSymSize
if x.val.Kind() == constant.String && len(constant.StringVal(x.val)) > maxLen {
// Disable the length check for now, as calling constant.StringVal
// eagerly constructs the string and can lead to significant memory
// usage increase. We may want a StringLen function.
// TODO(go.dev/issue/78346): reenable the check.
if false && x.val.Kind() == constant.String && len(constant.StringVal(x.val)) > maxLen {
check.errorf(atPos(opPos), InvalidConstVal, "constant string too long (%d bytes > %d bytes)",
len(constant.StringVal(x.val)), maxLen)
x.val = constant.MakeUnknown()

View file

@ -52,7 +52,11 @@ func (check *Checker) overflow(x *operand, opPos token.Pos) {
}
const maxLen = int(2e9) // cmd/internal/obj.MaxSymSize
if x.val.Kind() == constant.String && len(constant.StringVal(x.val)) > maxLen {
// Disable the length check for now, as calling constant.StringVal
// eagerly constructs the string and can lead to significant memory
// usage increase. We may want a StringLen function.
// TODO(go.dev/issue/78346): reenable the check.
if false && x.val.Kind() == constant.String && len(constant.StringVal(x.val)) > maxLen {
check.errorf(atPos(opPos), InvalidConstVal, "constant string too long (%d bytes > %d bytes)",
len(constant.StringVal(x.val)), maxLen)
x.val = constant.MakeUnknown()

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !386 && !arm && !mips && !mipsle && !wasm
//go:build ignore && !386 && !arm && !mips && !mipsle && !wasm
package p