| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
//go:build cgo |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
import ( |
| 12 |
"log" |
| 13 |
"runtime/cgo" |
| 14 |
"unsafe" |
| 15 |
) |
| 16 |
|
| 17 |
type S struct{ _ cgo.Incomplete } |
| 18 |
|
| 19 |
func main() { |
| 20 |
p := (*S)(unsafe.Pointer(uintptr(0x8000))) |
| 21 |
var v any = p |
| 22 |
p2 := v.(*S) |
| 23 |
if p != p2 { |
| 24 |
log.Fatalf("%p != %p", unsafe.Pointer(p), unsafe.Pointer(p2)) |
| 25 |
} |
| 26 |
p2 = typeAssert[*S](v) |
| 27 |
if p != p2 { |
| 28 |
log.Fatalf("%p != %p from typeAssert", unsafe.Pointer(p), unsafe.Pointer(p2)) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func typeAssert[T any](v any) T { |
| 33 |
return v.(T) |
| 34 |
} |