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
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@griesemer
Copy link
Contributor

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

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

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
@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label 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

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

@golang golang locked and limited conversation to collaborators Oct 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants