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: incorrect 'has no field or method' error #10977

Closed
ncw opened this issue May 28, 2015 · 3 comments
Closed

cmd/compile: incorrect 'has no field or method' error #10977

ncw opened this issue May 28, 2015 · 3 comments
Milestone

Comments

@ncw
Copy link
Contributor

ncw commented May 28, 2015

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

The same problem exists on

go version go1.4.2 linux/amd64
go version devel +5ee5528 Thu May 28 12:04:35 2015 +0000 linux/amd64
  • What operating system and processor architecture are you using?

Linux amd64, ubuntu 14.04

  • What did you do?
git clone https://gist.github.com/883d01d0274dda5e32d1.git bug
cd bug
go build
  • What did you expect to see?

No error

  • What did you see instead?
./e.go:4: TypeType.New undefined (type *Type has no field or method New)

If you look in t.go you can see that method is defined.

If you concatenate e.go and t.go and compile (remove duplicate package line) then it compiles fine.

Here is the Gist

e.go

package foo

var (
    Base = TypeType.New()
    // To trigger the problem
    //  * Need Base as argument - it is fine if you substitute with nil
    //  * Need the x, _ = form of call
    Something, _ = NewSomething(Base)
)

func NewSomething(t *Type) (*Type, error) {
    return nil, nil
}

t.go

package foo

type Type struct{}

var TypeType *Type = &Type{}

func (t *Type) New() *Type {
    return &Type{}
}
@rsc rsc added this to the Go1.5 milestone May 28, 2015
@rsc rsc changed the title compiler: incorrect method undefined error cmd/compiler: incorrect 'has no field or method' error May 28, 2015
@josharian
Copy link
Contributor

This reproduces back to Go 1.2, and maybe further.

It reproduces with a single file:

package foo

type T struct{}

var (
    t = T{}
    u = t.New()
)

func x(T) (int, int) { return 0, 0 }

var _, _ = x(u)

func (T) New() T { return T{} }

If you switch the order of the last two lines, then it compiles.

@josharian josharian changed the title cmd/compiler: incorrect 'has no field or method' error cmd/compile: incorrect 'has no field or method' error Jun 4, 2015
@Shugyousha
Copy link
Contributor

This does work though.

package main

type T struct{}

var (
    t = T{}
    u = t.New()
)

func x(T) (int, int) { return 0, 0 }

func main() {
        var _, _ = x(u)
}

func (T) New() T { return T{} }

This may be expected though because the main function definition gets processed after all the other statements (does it?).

@gopherbot
Copy link

CL https://golang.org/cl/10780 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants