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: selector expression resolves incorrectly for defined pointer types #21934

Closed
griesemer opened this issue Sep 19, 2017 · 3 comments
Closed
Labels
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 19, 2017

This is a follow up on #21738. In https://play.golang.org/p/1EnY2XsjDU:

package main

type E struct{ f int }
type T struct{ E }

func (*T) f() int { return 0 }

type P *T

func main() {
	var x P
	_ = x.f
	_ = (*x).f

	_ = (x.f)() // ERROR: cannot call non-function x.E.f (type int)
	_ = ((*x).f)()
}

x.f is valid only if (*x).f is a valid selector and f resolves to a field (rule 3 in https://tip.golang.org/ref/spec#Selectors. Specifically, x.f and (*x).f denote the same field.

However, cmd/compile appears to refer to the embedded field f in x.f and to the method f in (*x).f.

The selector resolution is inconsistent here. Furthermore, x.f should be disallowed per rule 3 mentioned above. It appears that cmd/compile uses different lookup rules depending on use case: If the type of x is a named pointer type, it ignores methods.

go/types correctly rejects x.f.

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Sep 19, 2017

Implementation detail: cmd/compile doesn't find the method f because it's in *T's method set, whereas x has type P with an empty method set.

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Nov 29, 2017

Issue was present in Go 1.9. Bumping to Go 1.11.

@mdempsky mdempsky modified the milestones: Go1.10, Go1.11 Nov 29, 2017
@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 30, 2018
@griesemer griesemer modified the milestones: Go1.12, Go1.13 Dec 11, 2018
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@gopherbot
Copy link

@gopherbot gopherbot commented Sep 29, 2019

Change https://golang.org/cl/197561 mentions this issue: cmd/compile: lookup methods of base type for named pointer type

@gopherbot gopherbot closed this in 0471417 Oct 4, 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.