Repository navigation
Closed
Closed
Copy link
Labels
FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Activity
[-]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[/+]andybons commentedon Apr 29, 2020
@randall77 @griesemer @josharian @mdempsky @martisch
griesemer commentedon Apr 30, 2020
I agree that instead of
T literal.MreportingT.Mwould be better.But I don't see a problem with the two error messages here: There are indeed two different errors:
T.Mdoesn'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 commentedon Apr 30, 2020
I don't think we should change
T literal.MtoT.M.T literalis an expression, whereasTis a type. I think we risk making other error messages confusing if we don't keep them unambiguous. E.g.,T.Mlooks 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).MorT{…}.Mif that's clearer.I think one error would make sense though. We only emit one error (
undefined: g) for:But either way, we should probably be consistent.
griesemer commentedon Apr 30, 2020
Fair enough.
T{…}.Mseems pretty nice, actually.willfaught commentedon Apr 30, 2020
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, whereyis undefined, that (1)yisn't signed, and (2)yis undefined. Only the second error is useful because the first isn't knowable, and the first is possibly wrong.For:
where
his undefined, the compiler reports only:instead of:
For:
where
fis undefined, the compiler reports only:instead of:
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.
results in:
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 commentedon Apr 30, 2020
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 undefinedfor 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)tof(…)). But I don't think that should distract from pragmatic improvements like changingT literaltoT{…}.gopherbot commentedon Sep 9, 2020
Change https://golang.org/cl/253677 mentions this issue:
cmd/compile: use clearer error message for stuct literalmdempsky commentedon Sep 10, 2020
Rob and Minux don't like
…: https://groups.google.com/g/golang-dev/c/faGp_C-zViwI also notice that
go doc heapusesinterface{}andinterface{ ... }. So maybe it's best to use regular dots for consistency.(To follow gofmt style though, we should use
T{...}for composite literals, and notT{ ... }.)gopherbot commentedon Sep 10, 2020
Change https://golang.org/cl/253678 mentions this issue:
cmd/compile: don't report not enough args error if call is undefinedcmd/compile: use clearer error message for stuct literal
cmd/compile: don't report not enough args error if call is undefined