Source file test/weak.go

     1  // errorcheck
     2  
     3  // Copyright 2025 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  // Test weak pointers.
     8  
     9  package p
    10  
    11  import (
    12  	"runtime"
    13  	"weak"
    14  )
    15  
    16  // Adapted from example in https://github.com/golang/go/issues/67552#issuecomment-2639661220
    17  func conversion() {
    18  	p := "hello"
    19  	a := weak.Make(&p)
    20  	b := (weak.Pointer[*byte])(a) // ERROR "cannot convert a \(variable of struct type weak\.Pointer\[string\]\) to type weak.Pointer\[\*byte\]"
    21  	c := b.Value()
    22  	println(**c)
    23  	runtime.KeepAlive(p)
    24  }
    25  

View as plain text