Skip to content

cmd/compile: allows assigning to blank identifier in struct #31546

@bradfitz

Description

@bradfitz

https://play.golang.org/p/WMC4t5oiRR-

package main

import (
	"fmt"
	"reflect"
)

var x = struct{ a, _, c int }{1, 2, 3}

func main() {
	var y = struct{ a, _, c int }{1, 2, 3}
	fmt.Println(x)
	fmt.Println(y)
	fmt.Println(reflect.ValueOf(x).Field(1).Int())
	fmt.Println(reflect.ValueOf(y).Field(1).Int())
	fmt.Println(x == y)
}

Currently (Go 1.12, master) says:

{1 2 3}
{1 0 3}
2
0
true

The x == y part returning true is defined by the spec, but x and y getting a different value for the blank identifier is... weird. I see nothing in the spec that addresses this directly but if squint I might be able to connect some different parts and make guesses as to intent.

But Go 1.4 - Go 1.8 fail to compile with:

./x.go:11: cannot refer to blank field or method

Go 1.9 is the first version where the program above compiles and returns that result.

/cc @ianlancetaylor @griesemer @josharian @cherrymui @randall77 @davecheney

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions