Skip to content

cmd/compile: confusing compiler errors for bad method name on named struct literal #38745

Closed

Description

@willfaught

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.

Activity

  1. 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[/+] on Apr 29, 2020
  2. andybons commented on Apr 29, 2020

    @andybons
    Member
  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Apr 29, 2020
  4. added this to the Unplanned milestone on Apr 29, 2020
  5. griesemer commented on Apr 30, 2020

    @griesemer
    Contributor

    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.

  6. mdempsky commented on Apr 30, 2020

    @mdempsky
    Contributor

    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.

  7. griesemer commented on Apr 30, 2020

    @griesemer
    Contributor

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

  8. willfaught commented on Apr 30, 2020

    @willfaught
    ContributorAuthor

    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).

  9. mdempsky commented on Apr 30, 2020

    @mdempsky
    Contributor

    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{…}.

  10. gopherbot commented on Sep 9, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/253677 mentions this issue: cmd/compile: use clearer error message for stuct literal

  11. mdempsky commented on Sep 10, 2020

    @mdempsky
    Contributor

    Rob and Minux don't like …: https://groups.google.com/g/golang-dev/c/faGp_C-zViw

    I also notice that go doc heap uses interface{} and interface{ ... }. So maybe it's best to use regular dots for consistency.

    (To follow gofmt style though, we should use T{...} for composite literals, and not T{ ... }.)

  12. gopherbot commented on Sep 10, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/253678 mentions this issue: cmd/compile: don't report not enough args error if call is undefined

  13. locked and limited conversation to collaborators on Sep 12, 2021
  14. added a commit that references this issue on Apr 29, 2024
    806f478
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions