Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/internal/runtime/z_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AssertIndexRange(b bool) {

func AssertDivideByZero(b bool) {
if b {
panic(errorString("integer divide by zero").Error())
panic(errorString("integer divide by zero"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion ssa/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
return b.Advance(args[0], args[1])
case "Sizeof":
// instance of generic function
return b.Prog.Val(int(b.Prog.SizeOf(args[0].Type)))
return b.Prog.IntVal(b.Prog.SizeOf(args[0].Type), b.Prog.Uintptr())
case "Alignof":
// instance of generic function
return b.Prog.Val(int(b.Prog.td.ABITypeAlignment(args[0].ll)))
Expand Down
78 changes: 78 additions & 0 deletions test/go/generic_unsafe_sizeof_arithmetic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package gotest

import (
"strings"
"testing"
"unsafe"
)

func genericUnsafeSizeofShift[T any]() int64 {
return 1 << unsafe.Sizeof(*new(T))
}

func genericUnsafeSizeofDiv[T any]() uintptr {
return 1 / unsafe.Sizeof(*new(T))
}

func genericUnsafeSizeofAdd[T any]() int64 {
return 1<<63 - 1 + int64(unsafe.Sizeof(*new(T)))
}

func genericUnsafeSizeofAny[T any]() any {
return unsafe.Sizeof(*new(T))
}

func TestGenericUnsafeSizeofArithmetic(t *testing.T) {
const minInt64 = -1 << 63

tests := []struct {
name string
got int64
want int64
}{
{name: "shift 62", got: genericUnsafeSizeofShift[[62]byte](), want: 1 << 62},
{name: "shift 63", got: genericUnsafeSizeofShift[[63]byte](), want: minInt64},
{name: "shift 64", got: genericUnsafeSizeofShift[[64]byte](), want: 0},
{name: "shift 100", got: genericUnsafeSizeofShift[[100]byte](), want: 0},
{name: "shift large", got: genericUnsafeSizeofShift[[1e6]byte](), want: 0},
}
for _, tt := range tests {
if tt.got != tt.want {
t.Fatalf("%s = %d, want %d", tt.name, tt.got, tt.want)
}
}

if got := genericUnsafeSizeofAdd[[1]byte](); got != minInt64 {
t.Fatalf("add overflow = %d, want %d", got, minInt64)
}
if got := genericUnsafeSizeofAny[[1]byte](); got != uintptr(1) {
t.Fatalf("Sizeof boxed value = %v (%T), want uintptr(1)", got, got)
}

expectGenericUnsafeSizeofDivideByZero(t, func() {
_ = genericUnsafeSizeofDiv[[0]byte]()
})
}

func expectGenericUnsafeSizeofDivideByZero(t *testing.T, f func()) {
t.Helper()
defer func() {
err := recover()
if err == nil {
t.Fatal("divide by zero did not panic")
}
runtimeErr, ok := err.(interface{ RuntimeError() })
if !ok {
t.Fatalf("panic type = %T, want runtime.Error", err)
}
_ = runtimeErr
msgErr, ok := err.(error)
if !ok {
t.Fatalf("panic type = %T, want error", err)
}
if got := msgErr.Error(); !strings.Contains(got, "divide by zero") {
t.Fatalf("panic = %q, want divide by zero", got)
}
}()
f()
}
8 changes: 0 additions & 8 deletions test/goroot/xfail.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3664,10 +3664,6 @@ xfails:
directive: run
case: fixedbugs/issue53635.go
reason: current main goroot run failure on darwin/arm64
- platform: darwin/arm64
directive: run
case: fixedbugs/issue60601.go
reason: current main goroot run failure on darwin/arm64
- version: go1.24
platform: darwin/arm64
directive: run
Expand Down Expand Up @@ -3862,10 +3858,6 @@ xfails:
directive: run
case: fixedbugs/issue53635.go
reason: current main goroot run failure on linux/amd64
- platform: linux/amd64
directive: run
case: fixedbugs/issue60601.go
reason: current main goroot run failure on linux/amd64
- version: go1.24
platform: linux/amd64
directive: run
Expand Down
Loading