go/test/codegen/issue52635.go
Keith Randall 8af32240c6 cmd/compile: don't evaluate side effects of range over array
If the thing we're ranging over is an array or ptr to array, and
it doesn't have a function call or channel receive in it, then we
shouldn't evaluate it.

Typecheck the ranged-over value as a constant in that case.
That makes the unified exporter replace the range expression
with a constant int.

Change-Id: I0d4ea081de70d20cf6d1fa8d25ef6cb021975554
Reviewed-on: https://go-review.googlesource.com/c/go/+/659317
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
2025-04-21 15:50:43 -07:00

54 lines
1011 B
Go

// asmcheck
// Copyright 2022 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.
// Test that optimized range memclr works with pointers to arrays.
// The clears get inlined, see https://github.com/golang/go/issues/56997
package codegen
type T struct {
a *[10]int
b [10]int
s []int
}
func (t *T) f() {
// amd64:-".*runtime.memclrNoHeapPointers"
// amd64:"DUFFZERO"
for i := range t.a {
t.a[i] = 0
}
// amd64:-".*runtime.memclrNoHeapPointers"
// amd64:"DUFFZERO"
for i := range *t.a {
t.a[i] = 0
}
// amd64:-".*runtime.memclrNoHeapPointers"
// amd64:"DUFFZERO"
for i := range t.a {
(*t.a)[i] = 0
}
// amd64:-".*runtime.memclrNoHeapPointers"
// amd64:"DUFFZERO"
for i := range *t.a {
(*t.a)[i] = 0
}
// amd64:-".*runtime.memclrNoHeapPointers"
// amd64:"DUFFZERO"
for i := range t.b {
t.b[i] = 0
}
// amd64:".*runtime.memclrNoHeapPointers"
for i := range t.s {
t.s[i] = 0
}
}