diff options
author | Keith Randall <khr@golang.org> | 2022-03-18 10:46:15 -0700 |
---|---|---|
committer | Cherry Mui <cherryyz@google.com> | 2022-04-04 19:03:49 +0000 |
commit | f4d55662d6f390ab853b121caddd91eff2692634 (patch) | |
tree | d8e0c9578b35f83a1720541ff3a0a2537805726d | |
parent | 290883a55989c14f4fb4c0f7e2cf079fc0f31a3d (diff) | |
download | go-f4d55662d6f390ab853b121caddd91eff2692634.tar.gz go-f4d55662d6f390ab853b121caddd91eff2692634.zip |
[release-branch.go1.18] cmd/compile: pointers to notinheap types need their own shape
They should not share a shape with regular pointers. We could coalesce
multiple pointer-to-not-in-heap types, but doesn't seem worth it - just
make them fully stenciled.
Fixes #51741
Change-Id: Ie8158177226fbc46a798e71c51897a82f15153df
Reviewed-on: https://go-review.googlesource.com/c/go/+/393895
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
(cherry picked from commit fcf6afb82dc1e9f80a6260467026adc11d5c9529)
Reviewed-on: https://go-review.googlesource.com/c/go/+/393935
-rw-r--r-- | src/cmd/compile/internal/typecheck/subr.go | 2 | ||||
-rw-r--r-- | test/typeparam/issue51733.go | 32 |
2 files changed, 33 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/typecheck/subr.go b/src/cmd/compile/internal/typecheck/subr.go index 5147ebbd2c..0d43855c44 100644 --- a/src/cmd/compile/internal/typecheck/subr.go +++ b/src/cmd/compile/internal/typecheck/subr.go @@ -1536,7 +1536,7 @@ func Shapify(t *types.Type, index int, tparam *types.Type) *types.Type { // Note: pointers to arrays are special because of slice-to-array-pointer // conversions. See issue 49295. if u.Kind() == types.TPTR && u.Elem().Kind() != types.TARRAY && - tparam.Bound().StructuralType() == nil { + tparam.Bound().StructuralType() == nil && !u.Elem().NotInHeap() { u = types.Types[types.TUINT8].PtrTo() } diff --git a/test/typeparam/issue51733.go b/test/typeparam/issue51733.go new file mode 100644 index 0000000000..14f6db2a87 --- /dev/null +++ b/test/typeparam/issue51733.go @@ -0,0 +1,32 @@ +// run -gcflags=-G=3 + +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "log" + "unsafe" +) + +//go:notinheap +type S struct{} + +func main() { + p := (*S)(unsafe.Pointer(uintptr(0x8000))) + var v any = p + p2 := v.(*S) + if p != p2 { + log.Fatalf("%p != %p", unsafe.Pointer(p), unsafe.Pointer(p2)) + } + p2 = typeAssert[*S](v) + if p != p2 { + log.Fatalf("%p != %p from typeAssert", unsafe.Pointer(p), unsafe.Pointer(p2)) + } +} + +func typeAssert[T any](v any) T { + return v.(T) +} |