Skip to content

Commit 0b65b02

Browse files
cuonglmrandall77
authored andcommitted
cmd/compile: fix clear on slice with zero size elem
Fixed #61127 Change-Id: If07b04ebcc98438c66f273c0c94bea1f230dc2e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/507535 Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]>
1 parent c4db811 commit 0b65b02

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/cmd/compile/internal/walk/builtin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ func walkClear(n *ir.UnaryExpr) ir.Node {
135135
typ := n.X.Type()
136136
switch {
137137
case typ.IsSlice():
138-
return arrayClear(n.X.Pos(), n.X, nil)
138+
if n := arrayClear(n.X.Pos(), n.X, nil); n != nil {
139+
return n
140+
}
141+
// If n == nil, we are clearing an array which takes zero memory, do nothing.
142+
return ir.NewBlockStmt(n.Pos(), nil)
139143
case typ.IsMap():
140144
return mapClear(n.X, reflectdata.TypePtrAt(n.X.Pos(), n.X.Type()))
141145
}

test/fixedbugs/issue61127.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile
2+
3+
// Copyright 2023 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.
6+
7+
package main
8+
9+
var V = []struct{}{}
10+
11+
func main() {
12+
clear(V)
13+
}

0 commit comments

Comments
 (0)