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: unsafe.Offsetof returns incorrect value in embedded struct with type parameters #53137

Closed
kazu opened this issue May 29, 2022 · 7 comments
Labels
generics Issue is related to generics NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kazu
Copy link

kazu commented May 29, 2022

I checked in 1.18.2 / go1.19-52f68efa45.

https://go.dev/play/p/nSbR4HtjOSv

@kazu kazu changed the title unsafe.Offsetof() return invalid value in embedded struct with type parameters. unsafe.Offsetof() return not-correct value in embedded struct with type parameters. May 29, 2022
@kazu
Copy link
Author

kazu commented May 30, 2022

@ianlancetaylor ianlancetaylor changed the title unsafe.Offsetof() return not-correct value in embedded struct with type parameters. cmd/compile: unsafe.Offsetof returns incorrect value in embedded struct with type parameters May 30, 2022
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented May 30, 2022

CC @randall77 @mdempsky

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 30, 2022
@ianlancetaylor ianlancetaylor added this to the Go1.19 milestone May 30, 2022
@cuonglm
Copy link
Member

cuonglm commented May 30, 2022

It seems that when instantiating generic function, all implicit dot operations are resolved, so unsafe.Offsetof(d.B) becomes unsafe.Offsetof(d.Embedded.B) at the time we call evalunsafe. Thus the base selector is d.Embedded instead of d.

Maybe we just need to clobber any implicit dot, something like this should work:

// Remember base of selector to find it back after dot insertion.
// Since r->left may be mutated by typechecking, check it explicitly
// first to track it correctly.
sel.X = Expr(sel.X)
sbase := sel.X

var clobberBase func(n ir.Node) ir.Node
clobberBase = func(n ir.Node) ir.Node {
	if x, ok := n.(*ir.SelectorExpr); ok && x.Implicit() {
		return clobberBase(x.X)
	}
	return n
}
sbase = clobberBase(sbase)

@gopherbot
Copy link

gopherbot commented May 30, 2022

Change https://go.dev/cl/409355 mentions this issue: cmd/compile: fix wrong unsafe.Offsetof evaluation inside generic function

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. generics Issue is related to generics and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 30, 2022
@cuonglm
Copy link
Member

cuonglm commented May 31, 2022

Should we backport this cc @ianlancetaylor @randall77 ?

@randall77
Copy link
Contributor

randall77 commented May 31, 2022

@gopherbot please open a backport issue for 1.18.

@gopherbot
Copy link

gopherbot commented May 31, 2022

Backport issue(s) opened: #53159 (for 1.18).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generics Issue is related to generics NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants