Skip to content

cmd/compile: change in initialization order when using go test -cover #56293

@ianlancetaylor

Description

@ianlancetaylor

m.go:

package main

import (
	"flag"
)

var (
	fooFlag = flag.String("foo", "", "this should be ok")
	foo     = flag.Lookup("foo")

	barFlag = flag.String("bar", "", "this should be also ok, but is "+notOK()+".")
	bar     = flag.Lookup("bar")
)

func notOK() string {
	return "not OK"
}

m_test.go:

package main

import (
	"testing"
)

func TestFoo(t *testing.T) {
	if foo == nil {
		t.Fatal()
	}
}

func TestBar(t *testing.T) {
	if bar == nil {
		t.Fatal()
	}
}

When run with go test, this passes. When run with go test -cover it fails:

--- FAIL: TestBar (0.00s)
    m_test.go:15: 
FAIL
	main	coverage: 100.0% of statements
exit status 1
FAIL	example.com	0.003s

The variables have no initialization dependencies visible to the compiler, so the order of initialization rules say that they should be initialized in declaration order. That is what happens with go test, but it appears to not happen with go test -cover.

This test passed with go test -cover in versions of Go before Go 1.13.

CC @golang/runtime @thanm

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions