Source file test/typeparam/issue48424.go

     1  // run
     2  
     3  // Copyright 2021 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  // Smoke test for constraint literals with elided interface
     8  // per issue #48424.
     9  
    10  package main
    11  
    12  func identity[T int](x T) T {
    13  	return x
    14  }
    15  
    16  func min[T int | string](x, y T) T {
    17  	if x < y {
    18  		return x
    19  	}
    20  	return y
    21  }
    22  
    23  func max[T ~int | ~float64](x, y T) T {
    24  	if x > y {
    25  		return x
    26  	}
    27  	return y
    28  }
    29  
    30  func main() {
    31  	if identity(1) != 1 {
    32  		panic("identity(1) failed")
    33  	}
    34  
    35  	if min(2, 3) != 2 {
    36  		panic("min(2, 3) failed")
    37  	}
    38  
    39  	if min("foo", "bar") != "bar" {
    40  		panic(`min("foo", "bar") failed`)
    41  	}
    42  
    43  	if max(2, 3) != 3 {
    44  		panic("max(2, 3) failed")
    45  	}
    46  }
    47  
    48  // Some random type parameter lists with elided interfaces.
    49  
    50  type (
    51  	_[T struct{}]                     struct{}
    52  	_[M map[K]V, K comparable, V any] struct{}
    53  	_[_ interface{} | int]            struct{}
    54  )
    55  

View as plain text