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 compiler errors for bad method name on named struct literal #38745

Open
willfaught opened this issue Apr 29, 2020 · 6 comments

Comments

@willfaught
Copy link
Contributor

@willfaught willfaught commented Apr 29, 2020

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

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/Will/Library/Caches/go-build"
GOENV="/Users/Will/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/Will/Developer/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.2_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/Will/Developer/turbine/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bx/qk0phsxd265fqj512dnnpg080000gn/T/go-build769661676=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.14.2 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.14.2
uname -v: Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.14.6
BuildVersion:	18G87
lldb --version: lldb-1100.0.30.12
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)

What did you do?

https://play.golang.org/p/ms1UAARp88o

package main

import "fmt"

type T struct{}
type R struct{}

func F() (*R, error) {
	return T{}.M()
}

What did you expect to see?

./prog.go:9:12: T.M undefined (type T has no field or method M)

What did you see instead?

./prog.go:9:2: not enough arguments to return
./prog.go:9:12: T literal.M undefined (type T has no field or method M)

It's confusing that the actionable error is listed second. The first error seems to be happening because it's assuming that M (which the compiler should know that it knows nothing about) returns only one result, but the underlying result of that is the second error. Only the second error should be reported.

Also, T literal.M is confusing syntax/wording. T.M seems clear enough.

@jayconrod jayconrod changed the title cmd/go: confusing compiler errors for bad method name on named struct literal cmd/compile: confusing compiler errors for bad method name on named struct literal Apr 29, 2020
@andybons
Copy link
Member

@andybons andybons commented Apr 29, 2020

@andybons andybons added this to the Unplanned milestone Apr 29, 2020
@griesemer
Copy link
Contributor

@griesemer griesemer commented Apr 30, 2020

I agree that instead of T literal.M reporting T.M would be better.

But I don't see a problem with the two error messages here: There are indeed two different errors: T.M doesn't exist, and there is only one return value instead of two.

Maybe in the actual code, the actual function that was supposed to be called here has two result values, but the compiler cannot know that. In general, in a case such as this, we do want both errors.

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Apr 30, 2020

I don't think we should change T literal.M to T.M. T literal is an expression, whereas T is a type. I think we risk making other error messages confusing if we don't keep them unambiguous. E.g., T.M looks like a method expression, whereas here we're actually talking about a field selector or method value.

I'd support changing it to something like (T literal).M or T{…}.M if that's clearer.

I think one error would make sense though. We only emit one error (undefined: g) for:

package p
func f() (int, int) { return g() }

But either way, we should probably be consistent.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Apr 30, 2020

Fair enough. T{…}.M seems pretty nice, actually.

@willfaught
Copy link
Contributor Author

@willfaught willfaught commented Apr 30, 2020

and there is only one return value instead of two.

I would argue that the compiler can't know that. Funcs can have multiple results, so until the identifier is defined, it can't know that there's a result count mismatch. It would be like reporting errors for var x int = y, where y is undefined, that (1) y isn't signed, and (2) y is undefined. Only the second error is useful because the first isn't knowable, and the first is possibly wrong.

For:

func g() (int, int) { return h() }

where h is undefined, the compiler reports only:

./prog.go:12:9: undefined: h

instead of:

./prog.go:12:2: not enough arguments to return
./prog.go:12:9: undefined: h

For:

var x, y = f()

where f is undefined, the compiler reports only:

./prog.go:8:13: undefined: f

instead of:

./prog.go:8:2: not enough arguments to return // or whatever the right wording is for assignment
./prog.go:8:13: undefined: f

It seems like all these scenarios should result in the same error set.


It looks like the compiler is trying to show the actual problematic syntax along with the type error explanation.

func f(...int) T { return T{} }

func F() (*R, error) {
	return f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10).M()
}

results in:

./prog.go:13:2: not enough arguments to return
./prog.go:13:72: f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10).M undefined (type T has no field or method M)

To be consistent with func calls, at least, it should be T{a: x, b: y, c: z}.M (or whatever the full struct literal expression in the program is).

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Apr 30, 2020

To be consistent with func calls, at least, it should be T{a: x, b: y, c: z}.M (or whatever the full struct literal expression in the program is).

Agreed we should try to be more consistent here between function calls and composite literals, but I think the preferable direction would be towards reporting f(…).M undefined for your example, as the error here is unrelated to f's arguments. Reporting the full argument list just distracts from seeing the actual issue, since it'll be truncated in a narrow window.

Ideally, we'd have some clever way of knowing what context we can reasonably trim, and a smart threshold for when trimming is appropriate (e.g., no point trimming f(1) to f(…)). But I don't think that should distract from pragmatic improvements like changing T literal to T{…}.

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
4 participants
You can’t perform that action at this time.