Skip to content

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

Closed

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.

Activity

  1. self-assigned this
    on Nov 20, 2025
  2. mrkfrmn commented on Nov 20, 2025

    @mrkfrmn
    ContributorAuthor
  3. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Nov 20, 2025
  4. gopherbot commented on Nov 20, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/722161 mentions this issue: go/types, types2: replace setDefType with boundaries

  5. gopherbot commented on Nov 25, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/724140 mentions this issue: go/types, types2: replace setDefType with pending type check

  6. added a commit that references this issue on Nov 27, 2025
    3531ac2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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