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: type checking fails in cycle involving recursive struct and unsafe.Sizeof #14620

Open
rsc opened this issue Mar 3, 2016 · 8 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Mar 3, 2016

package p

import "unsafe"

type hdr struct {
    next *msg
}

type msg struct {
    hdr
    pad [1024-hdrsize]uint8
}

const hdrsize = unsafe.Sizeof(hdr{})

This should type check but does not:

$ go tool compile /tmp/x.go
/tmp/x.go:14: invalid type for composite literal: hdr
/tmp/x.go:14: invalid expression unsafe.Sizeof(hdr literal)

Looks like the compiler gets confused by the not-quite-cycle in the definition.

http://play.golang.org/p/Hbmv1j_UrR

/cc @RLH

@rsc rsc added this to the Go1.7 milestone Mar 3, 2016
@davmaz
Copy link

davmaz commented Mar 11, 2016

Could this also be the case (when using cgo):
phySize := unsafe.Sizeof(C.struct_ad9361_rf_phy)
giving a compile time error:
invalid expression unsafe.Sizeof(_Ctype_struct_ad9361_rf_phy)

@ianlancetaylor
Copy link
Contributor

@davmaz No, the problem there is simply that unsafe.Sizeof takes a value, not a type. It's not the same as the C sizeof operation.

In general, for the Go project, please ask questions in a forum rather than on the issue tracker. See https://golang.org/wiki/Questions. Thanks.

@mdempsky
Copy link
Member

The problem is that when typechecking a pointer to a named type like *msg, cmd/compile recurses and tries to completely typecheck msg. Instead, we should just do the minimum to get a *Type value for msg (in this case a TFORW type) so we can construct the *msg pointer type. Later when we visit msg's type declaration, we can finish typechecking it.

A workaround is to move the hdr type declaration after the msg type declaration.

@rsc
Copy link
Contributor Author

rsc commented May 17, 2016

Quite possibly Robert's front end revamp will take care of this. Maybe not worth worrying about before then.

@rsc rsc modified the milestones: Go1.8, Go1.7 May 17, 2016
@griesemer
Copy link
Contributor

FWIW, gotype accepts this code w/o problems. Since there's a work-around per @mdempsky I don't think there's any urgency here.

@griesemer griesemer self-assigned this May 18, 2016
@griesemer griesemer modified the milestones: Go1.8Maybe, Go1.8 May 18, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 10, 2016
@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Oct 20, 2016
@josharian
Copy link
Contributor

Reproduced at tip (00263a8) on May 10, 2017.

Still seems low priority; punting to 1.10.

@josharian josharian modified the milestones: Go1.10, Go1.9 May 11, 2017
@griesemer griesemer modified the milestones: Go1.10, Go1.11 Nov 29, 2017
@griesemer griesemer modified the milestones: Go1.11, Go1.12 Jun 5, 2018
@griesemer griesemer modified the milestones: Go1.12, Go1.13 Oct 24, 2018
@griesemer griesemer modified the milestones: Go1.13, Go1.14 May 6, 2019
@griesemer griesemer modified the milestones: Go1.14, Unplanned Aug 28, 2019
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@cuonglm
Copy link
Member

cuonglm commented Oct 4, 2023

FWIW, gotype accepts this code w/o problems. Since there's a work-around per @mdempsky I don't think there's any urgency here.

FYI, both go/types and cmd/compile now reports an error as of go1.21 and tip:

$ go tool compile p.go
p.go:5:6: invalid recursive type hdr
	p.go:5:6: hdr refers to
	p.go:9:6: msg refers to
	p.go:14:7: hdrsize refers to
	p.go:5:6: hdr
$ gotype p.go
p.go:5:6: invalid recursive type hdr
p.go:5:6: 	hdr refers to
p.go:9:6: 	msg refers to
p.go:14:7: 	hdrsize refers to
p.go:5:6: 	hdr

@griesemer griesemer self-assigned this Oct 11, 2023
@griesemer griesemer modified the milestones: Unplanned, Backlog Oct 11, 2023
@mdempsky
Copy link
Member

Related #13890.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

9 participants