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
Closed
Milestone

Comments

@pwaller
Copy link
Contributor

@pwaller 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

This comment has been minimized.

Copy link
Member

@mvdan mvdan commented Jan 21, 2019

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

@beoran

This comment has been minimized.

Copy link

@beoran 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

This comment has been minimized.

Copy link
Member

@mdempsky 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 label Jan 22, 2019
@mdempsky mdempsky added this to the Go1.13 milestone Jan 22, 2019
@mdempsky mdempsky added the Suggested label Jan 22, 2019
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jan 23, 2019

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

@gopherbot gopherbot closed this in 6d781de Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.