New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: bug in global variable initialization #29013

Closed
randall77 opened this Issue Nov 30, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@randall77
Copy link
Contributor

randall77 commented Nov 30, 2018

package main

import "fmt"

type TestSuite struct {
	Tests []Test
}
type Test struct {
	Want interface{}
}
type Int struct {
	i int
}

func NewInt(v int) Int {
	return Int{i: v}
}

var Suites = []TestSuite{
	Dicts,
}
var Dicts = TestSuite{
	Tests: []Test{
		{
			Want: map[Int]bool{NewInt(1): true},
		},
		{
			Want: map[Int]string{
				NewInt(3): "3",
			},
		},
	},
}

func main() {
	var mym = Dicts.Tests[0].Want.(map[Int]bool)
	fmt.Printf("%p %p %v\n", &Dicts.Tests[0], mym, mym)
	var mym2 = Suites[0].Tests[0].Want.(map[Int]bool)
	fmt.Printf("%p %p %v\n", &Suites[0].Tests[0], mym2, mym2)
}

When I run this on tip, I get:

0x54d580 0xc00007c150 map[{1}:true]
0x54d5a0 0xc00007c1b0 map[{3}:true]

That's clearly not right. Where did the map[3]=true come from?

On 1.11, I get:

0x548640 0xc000082150 map[{1}:true]
0x548660 0xc0000821b0 map[{1}:true]

Which is more correct. But not actually completely correct. I think we should have &Suites[0].Tests[0] == &Dicts.Tests[0], but we don't. This may or may not be related to the first bug.

This started happening after my reuse temporaries change: https://go-review.googlesource.com/c/go/+/140301 I'm not sure yet if that CL is actually buggy or just triggered something underlying.

@randall77 randall77 added this to the Go1.12 milestone Nov 30, 2018

@randall77 randall77 self-assigned this Nov 30, 2018

@jingyugao

This comment has been minimized.

Copy link

jingyugao commented Nov 30, 2018

seems is a compiler bug。

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Dec 1, 2018

Change https://golang.org/cl/152081 mentions this issue: cmd/compile: fix static initializer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment