Skip to content

cmd/compile: type checking fails in cycle involving recursive struct and unsafe.Sizeof #14620

Open
@rsc

Description

@rsc

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

Activity

added this to the Go1.7 milestone on Mar 3, 2016
davmaz

davmaz commented on Mar 11, 2016

@davmaz

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

ianlancetaylor commented on Mar 11, 2016

@ianlancetaylor
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

mdempsky commented on Apr 18, 2016

@mdempsky
Contributor

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

rsc commented on May 17, 2016

@rsc
ContributorAuthor

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

modified the milestones: Go1.8, Go1.7 on May 17, 2016
griesemer

griesemer commented on May 18, 2016

@griesemer
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.

self-assigned this
on May 18, 2016
modified the milestones: Go1.8Maybe, Go1.8 on May 18, 2016
added
NeedsFixThe path to resolution is known, but the work has not been done.
on Oct 10, 2016
modified the milestones: Go1.9, Go1.8Maybe on Oct 20, 2016
josharian

josharian commented on May 11, 2017

@josharian
Contributor

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

Still seems low priority; punting to 1.10.

22 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Todo

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @mdempsky@josharian@rsc@quentinmit@davmaz

      Issue actions

        cmd/compile: type checking fails in cycle involving recursive struct and unsafe.Sizeof · Issue #14620 · golang/go