Source file test/fixedbugs/issue37716.go

     1  // run
     2  
     3  // Copyright 2020 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  package main
     8  
     9  import "reflect"
    10  
    11  // complicated enough to require a compile-generated hash function
    12  type K struct {
    13  	a, b int32 // these get merged by the compiler into a single field, something typehash doesn't do
    14  	c    float64
    15  }
    16  
    17  func main() {
    18  	k := K{a: 1, b: 2, c: 3}
    19  
    20  	// Make a reflect map.
    21  	m := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(K{}), reflect.TypeOf(true)))
    22  	m.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(true))
    23  
    24  	// The binary must not contain the type map[K]bool anywhere, or reflect.MapOf
    25  	// will use that type instead of making a new one. So use an equivalent named type.
    26  	type M map[K]bool
    27  	var x M
    28  	reflect.ValueOf(&x).Elem().Set(m)
    29  	if !x[k] {
    30  		panic("key not found")
    31  	}
    32  }
    33  

View as plain text