cmd/compile: fix ICE when indexing zero-sized arrays of non-SSAable types

Fixes #77868

Change-Id: I3348825e24a71595c514113497db365a6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/749881
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Mateusz Poliwczak
2026-02-28 12:05:10 +01:00
committed by Gopher Robot
parent 50126a8e44
commit b16360470c
2 changed files with 23 additions and 3 deletions

View File

@@ -3555,9 +3555,9 @@ func (s *state) exprCheckPtr(n ir.Node, checkPtrOK bool) *ssa.Value {
// use constants for the bounds check.
z := s.constInt(types.Types[types.TINT], 0)
s.boundsCheck(z, z, ssa.BoundsIndex, false)
// The return value won't be live, return junk.
// But not quite junk, in case bounds checks are turned off. See issue 48092.
return s.zeroVal(n.Type())
// The return value won't be live. In case bounds checks
// are turned off, load from (*T)(nil) to cause a segfault.
return s.load(n.Type(), s.constNil(n.Type().PtrTo()))
}
len := s.constInt(types.Types[types.TINT], bound)
s.boundsCheck(i, len, ssa.BoundsIndex, n.Bounded()) // checks i == 0

View File

@@ -0,0 +1,20 @@
// compile
// Copyright 2026 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 p
type S struct {
n int
a [2]int
}
func f(i int) int {
var arr [0]S
// Accessing a zero-length array must not trigger an internal compiler error.
// This code is invalid, but make sure that we can compile it.
return arr[i].n
}