Source file test/genmeth2.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  // Verify that generic methods work with pointer receivers.
     8  
     9  package main
    10  
    11  import "fmt"
    12  
    13  type T struct {}
    14  
    15  func (t *T) m[P any]() {
    16  	var x P
    17  	if got := fmt.Sprintf("%T", x); got != "int" {
    18  		panic(fmt.Sprintf("got %s, want int", got))
    19  	}
    20  }
    21  
    22  type G[P any] struct {}
    23  
    24  func (g *G[P]) m[Q any]() {
    25  	var p P
    26  	var q Q
    27  	if got := fmt.Sprintf("%T %T", p, q); got != "int bool" {
    28  		panic(fmt.Sprintf("got %s, want int bool", got))
    29  	}
    30  }
    31  
    32  func main() {
    33  	(&T{}).m[int]()
    34  	(&G[int]{}).m[bool]()
    35  }
    36  

View as plain text