Source file test/fixedbugs/issue81696.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  package main
     8  
     9  import "fmt"
    10  
    11  var trace []int
    12  
    13  func f(x int) int {
    14  	trace = append(trace, x)
    15  	return x
    16  }
    17  
    18  func wantTrace(what string, want ...int) {
    19  	if len(trace) != len(want) {
    20  		fmt.Printf("%s: evaluation order %v, want %v\n", what, trace, want)
    21  		panic(what)
    22  	}
    23  	for i, x := range want {
    24  		if trace[i] != x {
    25  			fmt.Printf("%s: evaluation order %v, want %v\n", what, trace, want)
    26  			panic(what)
    27  		}
    28  	}
    29  	trace = trace[:0]
    30  }
    31  
    32  func wantShape(what string, s []int, length, capacity int) {
    33  	if len(s) != length || cap(s) != capacity {
    34  		fmt.Printf("%s: len %d cap %d, want len %d cap %d\n", what, len(s), cap(s), length, capacity)
    35  		panic(what)
    36  	}
    37  }
    38  
    39  func main() {
    40  	// The length is evaluated before the capacity, even though the
    41  	// capacity is what gets rewritten.
    42  	s := make([]int, f(1), f(3))
    43  	wantTrace("make", 1, 3)
    44  	wantShape("make", s, 1, 3)
    45  
    46  	// Same, for a capacity whose value comes from the length of a slice
    47  	// literal rather than from a constant.
    48  	s = make([]int, f(2), len([]int{0, 0, 0, 0}))
    49  	wantTrace("make with len of literal", 2)
    50  	wantShape("make with len of literal", s, 2, 4)
    51  
    52  	// A two-operand make rewrites the length itself, which stays first.
    53  	s = make([]int, f(4))
    54  	wantTrace("make without cap", 4)
    55  	wantShape("make without cap", s, 4, 4)
    56  }
    57  

View as plain text