Source file test/abi/method_wrapper_notinheap.go

     1  // run
     2  
     3  // Copyright 2026 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  	"reflect"
    13  	"runtime/cgo"
    14  )
    15  
    16  type E struct {
    17  	x int
    18  }
    19  
    20  func (e *E) M() int {
    21  	return e.x
    22  }
    23  
    24  type T struct {
    25  	E
    26  }
    27  
    28  type NotInHeapT struct {
    29  	E
    30  	_ cgo.Incomplete
    31  }
    32  
    33  var (
    34  	x = struct {
    35  		E
    36  		_ cgo.Incomplete
    37  	}{E: E{x: 7}}
    38  	tFn          = (*T).M
    39  	notInHeapTFn = (*NotInHeapT).M
    40  )
    41  
    42  type I interface {
    43  	M() int
    44  }
    45  
    46  //go:noinline
    47  func call(i I) int {
    48  	return i.M()
    49  }
    50  
    51  func main() {
    52  	if got := call(&x); got != 7 {
    53  		panic(got)
    54  	}
    55  	if reflect.ValueOf(tFn).Pointer() == reflect.ValueOf(notInHeapTFn).Pointer() {
    56  		panic("pointer and scalar receiver wrappers were shared")
    57  	}
    58  }
    59  

View as plain text