Skip to content

go/types, types2: incorrect result from unsafe.Sizeof #76383

@mrkfrmn

Description

@mrkfrmn

Consider:

package p

import "unsafe"

var v any = 42

type T struct {
    f [unsafe.Sizeof(v.(T))]int
}

This yields:

testdata/manual.go:14:6  :  -- checking type T (objPath = )
testdata/manual.go:14:8  :  .  -- type struct{f [unsafe.Sizeof(v.(T))]int}
testdata/manual.go:15:4  :  .  .  -- type [unsafe.Sizeof(v.(T))]int
testdata/manual.go:15:18 :  .  .  .  -- expr unsafe.Sizeof(v.(T))
testdata/manual.go:15:11 :  .  .  .  .  -- expr unsafe.Sizeof
testdata/manual.go:15:11 :  .  .  .  .  => unsafe.Sizeof (built-in)
testdata/manual.go:15:20 :  .  .  .  .  -- expr v.(T)
testdata/manual.go:15:19 :  .  .  .  .  .  -- expr v
testdata/manual.go:12:5  :  .  .  .  .  .  .  -- checking var v (objPath = T)
testdata/manual.go:12:7  :  .  .  .  .  .  .  .  -- type any
testdata/manual.go:12:7  :  .  .  .  .  .  .  .  => any (under = any) // *Alias
testdata/manual.go:12:13 :  .  .  .  .  .  .  .  -- expr 42
testdata/manual.go:12:13 :  .  .  .  .  .  .  .  => 42 (untyped int constant)
testdata/manual.go:12:5  :  .  .  .  .  .  .  => var v any
testdata/manual.go:15:19 :  .  .  .  .  .  => v (variable of interface type any)
testdata/manual.go:15:22 :  .  .  .  .  .  -- type T
testdata/manual.go:14:6  :  .  .  .  .  .  .  ## cycle detected: objPath = T->T (len = 1)
testdata/manual.go:14:6  :  .  .  .  .  .  .  ## cycle contains: 0 values, 1 type definitions
testdata/manual.go:14:6  :  .  .  .  .  .  .  => cycle is valid
testdata/manual.go:15:22 :  .  .  .  .  .  => T (under = <nil>) // *Named
testdata/manual.go:15:20 :  .  .  .  .  => v.(T) (comma, ok expression of struct type T)
testdata/manual.go:15:18 :  .  .  .  => unsafe.Sizeof(v.(T)) (constant 0 of type uintptr)
testdata/manual.go:15:26 :  .  .  .  -- type int
testdata/manual.go:15:26 :  .  .  .  => int // *Basic
testdata/manual.go:15:4  :  .  .  => [0]int // *Array
testdata/manual.go:14:8  :  .  => struct{f [0]int} // *Struct
testdata/manual.go:14:6  :  => type T struct{f [0]int}

Note that unsafe.Sizeof(v.(T))) evaluates to 0, which seems wrong.

Without reporting an error. It seems this should be a type-checking error, as a value of type T is being constructed before T is fully-typed.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions