Source file test/fixedbugs/issue76744.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  // Converting a constant to a type parameter and then to an interface used
     8  // to point into runtime.staticuint64s, at the last byte of an element on a
     9  // big-endian platform. Reading the value back out was then a load from an
    10  // odd offset off the symbol, which the s390x linker cannot encode.
    11  //
    12  // CL 786920 fixed this incidentally, by making a constant converted to a
    13  // type parameter non-constant, as the spec requires.
    14  
    15  package main
    16  
    17  type I interface{ bool | float64 }
    18  
    19  type G[T I] interface{ Val() T }
    20  
    21  type BoolLit bool
    22  
    23  func (b BoolLit) Val() bool { return bool(b) }
    24  
    25  type F64Lit float64
    26  
    27  func (f F64Lit) Val() float64 { return float64(f) }
    28  
    29  func newLit[T I](val T) any {
    30  	switch v := any(val).(type) {
    31  	case bool:
    32  		return BoolLit(v)
    33  	case float64:
    34  		return F64Lit(v)
    35  	}
    36  	panic("unreachable")
    37  }
    38  
    39  func f[T I]() T {
    40  	var z T
    41  	return newLit(z).(G[T]).Val()
    42  }
    43  
    44  func main() {
    45  	if got := f[bool](); got != false {
    46  		panic("unexpected result")
    47  	}
    48  	if got := f[float64](); got != 0 {
    49  		panic("unexpected result")
    50  	}
    51  }
    52  

View as plain text