modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/ddd1.go (about)

     1  // errorcheck
     2  
     3  // Copyright 2010 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  // Verify that illegal uses of ... are detected.
     8  // Does not compile.
     9  
    10  package main
    11  
    12  import "unsafe"
    13  
    14  func sum(args ...int) int { return 0 }
    15  
    16  var (
    17  	_ = sum(1, 2, 3)
    18  	_ = sum()
    19  	_ = sum(1.0, 2.0)
    20  	_ = sum(1.5)      // ERROR "integer"
    21  	_ = sum("hello")  // ERROR ".hello. .type untyped string. as type int|incompatible"
    22  	_ = sum([]int{1}) // ERROR "\[\]int literal.*as type int|incompatible"
    23  )
    24  
    25  func sum3(int, int, int) int { return 0 }
    26  func tuple() (int, int, int) { return 1, 2, 3 }
    27  
    28  var (
    29  	_ = sum(tuple())
    30  	_ = sum(tuple()...) // ERROR "multiple-value"
    31  	_ = sum3(tuple())
    32  	_ = sum3(tuple()...) // ERROR "multiple-value" "not enough"
    33  )
    34  
    35  type T []T
    36  
    37  func funny(args ...T) int { return 0 }
    38  
    39  var (
    40  	_ = funny(nil)
    41  	_ = funny(nil, nil)
    42  	_ = funny([]T{}) // ok because []T{} is a T; passes []T{[]T{}}
    43  )
    44  
    45  func Foo(n int) {}
    46  
    47  func bad(args ...int) {
    48  	print(1, 2, args...)	// ERROR "[.][.][.]"
    49  	println(args...)	// ERROR "[.][.][.]"
    50  	ch := make(chan int)
    51  	close(ch...)	// ERROR "[.][.][.]"
    52  	_ = len(args...)	// ERROR "[.][.][.]"
    53  	_ = new(int...)	// ERROR "[.][.][.]"
    54  	n := 10
    55  	_ = make([]byte, n...)	// ERROR "[.][.][.]"
    56  	_ = make([]byte, 10 ...)	// ERROR "[.][.][.]"
    57  	var x int
    58  	_ = unsafe.Pointer(&x...)	// ERROR "[.][.][.]"
    59  	_ = unsafe.Sizeof(x...)	// ERROR "[.][.][.]"
    60  	_ = [...]byte("foo") // ERROR "[.][.][.]"
    61  	_ = [...][...]int{{1,2,3},{4,5,6}}	// ERROR "[.][.][.]"
    62  
    63  	Foo(x...) // ERROR "invalid use of [.][.][.] in call"
    64  }