Source file test/fixedbugs/issue71252.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  // A multi-value assignment from a call to an instantiated generic
     8  // function must not leave the caller's variable with the callee's shape
     9  // type. Slicing a *[N]byte result used to fail with "bad ptr to array in
    10  // slice go.shape.*uint8".
    11  
    12  package main
    13  
    14  import "fmt"
    15  
    16  func peek[V any](v V) (V, bool) { return v, true }
    17  
    18  type Cache[V any] struct{ v V }
    19  
    20  func (c *Cache[V]) Peek() (V, bool) { return c.v, true }
    21  
    22  type Rec struct{ Data []byte }
    23  
    24  func main() {
    25  	// A generic function returning (V, bool).
    26  	blk, _ := peek(new([4096]byte))
    27  	if got := len(blk[:]); got != 4096 {
    28  		panic(fmt.Sprintf("len(blk[:]) = %d, want 4096", got))
    29  	}
    30  
    31  	// A method of a generic type.
    32  	c := &Cache[*[2048]byte]{v: new([2048]byte)}
    33  	blk2, _ := c.Peek()
    34  	if got := len(blk2[:]); got != 2048 {
    35  		panic(fmt.Sprintf("len(blk2[:]) = %d, want 2048", got))
    36  	}
    37  
    38  	// The slice used inside a composite literal.
    39  	blk3, _ := peek(new([1024]byte))
    40  	if got := len(Rec{Data: blk3[:]}.Data); got != 1024 {
    41  		panic(fmt.Sprintf("len(Rec{...}.Data) = %d, want 1024", got))
    42  	}
    43  }
    44  

View as plain text