Skip to content
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: confusing error if composite literal field that is a method is specified #29855

Closed
pwaller opened this issue Jan 21, 2019 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@pwaller
Copy link
Contributor

pwaller commented Jan 21, 2019

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.

@mvdan
Copy link
Member

mvdan commented Jan 21, 2019

/cc @mdempsky @griesemer @ChrisALiles, who worked on the previous issue.

@beoran
Copy link

beoran commented Jan 22, 2019

I agree that this would probably be more clear in this case:

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

@mdempsky
Copy link
Member

mdempsky commented Jan 22, 2019

@pwaller Thanks for the report, and sorry about the confusing error message.

Looks like

 p, _ := dotpath(l.Sym, t, nil, true)
 if p == nil {
     yyerror("unknown field '%v' in struct literal of type %v", l.Sym, t)

in typecheck.go:typecheckcomplit needs to be changed to used dotpath's save parameter, and then we need to validate that the returned *types.Field is a field and not a method (i.e., f.Type.Etype != TFUNC || f.Type.Recv() == nil).

This should probably be abstracted into an IsMethod method on *types.Field since it's non-intuitive and appears in a few places already:

reflect.go:		if f.Type.Etype != TFUNC || f.Type.Recv() == nil {
subr.go:			if f.Sym == s || (ignorecase && f.Type.Etype == TFUNC && f.Type.Recv() != nil && strings.EqualFold(f.Sym.Name, s.Name)) {
subr.go:		if f.Type.Etype != TFUNC || f.Type.Recv() == nil {
subr.go:	if m.Type.Etype != TFUNC || m.Type.Recv() == nil {

Should be a relatively straight forward CL if anyone wants to volunteer to take it.

@mdempsky mdempsky added the NeedsFix The path to resolution is known, but the work has not been done. label Jan 22, 2019
@mdempsky mdempsky added this to the Go1.13 milestone Jan 22, 2019
@mdempsky mdempsky added the Suggested Issues that may be good for new contributors looking for work to do. label Jan 22, 2019
@gopherbot
Copy link

Change https://golang.org/cl/158938 mentions this issue: cmd/compile: confusing error if composite literal field is a method

@golang golang locked and limited conversation to collaborators Feb 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

5 participants