Skip to content

Commit 0edafef

Browse files
committed
cmd/gc: emit code for extern = <N>
https://golang.org/cl/152700045/ made it possible for struct literals assigned to globals to use <N> as the RHS. Normally, this is to zero out variables on first use. Because globals are already zero (or their linker initialized value), we just ignored this. Now that <N> can occur from non-initialization code, we need to emit this code. We don't use <N> for initialization of globals any more, so this shouldn't cause any excessive zeroing. Fixes #8961. LGTM=rsc R=golang-codereviews, rsc CC=bradfitz, golang-codereviews https://golang.org/cl/154540044
1 parent 63acc48 commit 0edafef

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/cmd/gc/gen.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,9 @@ cgen_as(Node *nl, Node *nr)
732732
}
733733

734734
if(nr == N || iszero(nr)) {
735-
// externals and heaps should already be clear
736-
if(nr == N) {
737-
if(nl->class == PEXTERN)
738-
return;
739-
if(nl->class & PHEAP)
740-
return;
741-
}
735+
// heaps should already be clear
736+
if(nr == N && (nl->class & PHEAP))
737+
return;
742738

743739
tl = nl->type;
744740
if(tl == T)

test/fixedbugs/issue8961.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run
2+
3+
// Copyright 2014 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+
// Issue 8961. Empty composite literals to small globals were not filled in
8+
package main
9+
10+
type small struct { a int }
11+
var foo small
12+
13+
func main() {
14+
foo.a = 1
15+
foo = small{}
16+
if foo.a != 0 {
17+
println("expected foo.a to be 0, was", foo.a)
18+
panic("composite literal not filled in")
19+
}
20+
}

0 commit comments

Comments
 (0)