Skip to content

cmd/compile: bug in global variable initialization #29013

@randall77

Description

@randall77
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.release-blocker

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions