Source file test/typeparam/mdempsky/17.go

     1  // run
     2  
     3  // Copyright 2022 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 that implicit conversions of derived types to interface type
     8  // in range loops work correctly.
     9  
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"reflect"
    15  )
    16  
    17  func main() {
    18  	test{"int", "V"}.match(RangeArrayAny[V]())
    19  	test{"int", "V"}.match(RangeArrayIface[V]())
    20  	test{"V"}.match(RangeChanAny[V]())
    21  	test{"V"}.match(RangeChanIface[V]())
    22  	test{"K", "V"}.match(RangeMapAny[K, V]())
    23  	test{"K", "V"}.match(RangeMapIface[K, V]())
    24  	test{"int", "V"}.match(RangeSliceAny[V]())
    25  	test{"int", "V"}.match(RangeSliceIface[V]())
    26  }
    27  
    28  type test []string
    29  
    30  func (t test) match(args ...any) {
    31  	if len(t) != len(args) {
    32  		fmt.Printf("FAIL: want %v values, have %v\n", len(t), len(args))
    33  		return
    34  	}
    35  	for i, want := range t {
    36  		if have := reflect.TypeOf(args[i]).Name(); want != have {
    37  			fmt.Printf("FAIL: %v: want type %v, have %v\n", i, want, have)
    38  		}
    39  	}
    40  }
    41  
    42  type iface interface{ M() int }
    43  
    44  type K int
    45  type V int
    46  
    47  func (K) M() int { return 0 }
    48  func (V) M() int { return 0 }
    49  
    50  func RangeArrayAny[V any]() (k, v any) {
    51  	for k, v = range [...]V{zero[V]()} {
    52  	}
    53  	return
    54  }
    55  
    56  func RangeArrayIface[V iface]() (k any, v iface) {
    57  	for k, v = range [...]V{zero[V]()} {
    58  	}
    59  	return
    60  }
    61  
    62  func RangeChanAny[V any]() (v any) {
    63  	for v = range chanOf(zero[V]()) {
    64  	}
    65  	return
    66  }
    67  
    68  func RangeChanIface[V iface]() (v iface) {
    69  	for v = range chanOf(zero[V]()) {
    70  	}
    71  	return
    72  }
    73  
    74  func RangeMapAny[K comparable, V any]() (k, v any) {
    75  	for k, v = range map[K]V{zero[K](): zero[V]()} {
    76  	}
    77  	return
    78  }
    79  
    80  func RangeMapIface[K interface {
    81  	iface
    82  	comparable
    83  }, V iface]() (k, v iface) {
    84  	for k, v = range map[K]V{zero[K](): zero[V]()} {
    85  	}
    86  	return
    87  }
    88  
    89  func RangeSliceAny[V any]() (k, v any) {
    90  	for k, v = range []V{zero[V]()} {
    91  	}
    92  	return
    93  }
    94  
    95  func RangeSliceIface[V iface]() (k any, v iface) {
    96  	for k, v = range []V{zero[V]()} {
    97  	}
    98  	return
    99  }
   100  
   101  func chanOf[T any](elems ...T) chan T {
   102  	c := make(chan T, len(elems))
   103  	for _, elem := range elems {
   104  		c <- elem
   105  	}
   106  	close(c)
   107  	return c
   108  }
   109  
   110  func zero[T any]() (_ T) { return }
   111  

View as plain text