Skip to content

cmd/compile: confusing error if composite literal field that is a method is specified #29855

@pwaller

Description

@pwaller

What version of Go are you using (go version)?

$ go version
tip, go playground.

Does this issue reproduce with the latest release?

Yes.

What did you do?

Playground link: https://play.golang.org/p/llL2iikbJmd

package main

type T struct {
	GlobalName string
}
var t = T{Name: "foo"}

func (t T) Name() string {
	return t.GlobalName
}

(Note: the field is called GlobalName but the composite literal specifies the incorrect field Name, which is also a method).

What did you expect to see?

An error message telling me that I'm trying to declare a composite literal field which doesn't exist (or is a method, not a struct field).

What did you see instead?

prog.go:6:11: cannot use promoted field Name in struct literal of type T

This lead to some confusion - in my case because the name of the method (Name) was similar to the name of the field I was trying to declare (GlobalName). The issue occurred because a dependency I was using changed the Name field to GlobalName and introduced an accessor method called (T).Name(). So I was in a larger process of code repair.

The struct in question also had a number of embedded fields, so it took a moment before I found the root cause (that there was Name method defined far away).

See also #23609 which improved error messages for embedding but perhaps made things more confusing in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.SuggestedIssues that may be good for new contributors looking for work to do.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions