blob: 07981af126acfe3544aeb9eaa782263d9094e95d [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox68796b02010-02-01 00:25:59 -08002
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
Rob Pike83976e32012-02-19 14:28:53 +11007// Verify that illegal uses of ... are detected.
8// Does not compile.
9
Russ Cox68796b02010-02-01 00:25:59 -080010package main
11
Russ Cox75dd8fd2010-09-24 11:55:30 -040012import "unsafe"
13
Russ Cox68796b02010-02-01 00:25:59 -080014func sum(args ...int) int { return 0 }
15
16var (
17 _ = sum(1, 2, 3)
18 _ = sum()
19 _ = sum(1.0, 2.0)
20 _ = sum(1.5) // ERROR "integer"
Luuk van Dijke14d1d72011-12-14 17:34:35 +010021 _ = sum("hello") // ERROR ".hello. .type string. as type int|incompatible"
Luuk van Dijkb536adb2011-10-08 19:37:06 +020022 _ = sum([]int{1}) // ERROR "\[\]int literal.*as type int|incompatible"
Russ Cox68796b02010-02-01 00:25:59 -080023)
24
Rémy Oudompheng656b1922012-07-13 08:05:41 +020025func sum3(int, int, int) int { return 0 }
26func tuple() (int, int, int) { return 1, 2, 3 }
27
28var (
29 _ = sum(tuple())
Ian Lance Taylor6ed800c2012-09-28 08:30:30 -070030 _ = sum(tuple()...) // ERROR "multiple-value|[.][.][.]"
Rémy Oudompheng656b1922012-07-13 08:05:41 +020031 _ = sum3(tuple())
Ian Lance Taylor6ed800c2012-09-28 08:30:30 -070032 _ = sum3(tuple()...) // ERROR "multiple-value|[.][.][.]" "not enough"
Rémy Oudompheng656b1922012-07-13 08:05:41 +020033)
34
Russ Cox68796b02010-02-01 00:25:59 -080035type T []T
36
37func funny(args ...T) int { return 0 }
38
39var (
40 _ = funny(nil)
41 _ = funny(nil, nil)
42 _ = funny([]T{}) // ok because []T{} is a T; passes []T{[]T{}}
43)
Russ Cox75dd8fd2010-09-24 11:55:30 -040044
45func bad(args ...int) {
46 print(1, 2, args...) // ERROR "[.][.][.]"
47 println(args...) // ERROR "[.][.][.]"
48 ch := make(chan int)
49 close(ch...) // ERROR "[.][.][.]"
50 _ = len(args...) // ERROR "[.][.][.]"
Russ Cox75dd8fd2010-09-24 11:55:30 -040051 _ = new(int...) // ERROR "[.][.][.]"
52 n := 10
53 _ = make([]byte, n...) // ERROR "[.][.][.]"
54 // TODO(rsc): enable after gofmt bug is fixed
55 // _ = make([]byte, 10 ...) // error "[.][.][.]"
56 var x int
57 _ = unsafe.Pointer(&x...) // ERROR "[.][.][.]"
58 _ = unsafe.Sizeof(x...) // ERROR "[.][.][.]"
Anthony Martin5b62ba12011-05-31 15:41:47 -040059 _ = [...]byte("foo") // ERROR "[.][.][.]"
Russ Coxbf899be2011-07-26 00:52:02 -040060 _ = [...][...]int{{1,2,3},{4,5,6}} // ERROR "[.][.][.]"
Russ Cox75dd8fd2010-09-24 11:55:30 -040061}
Russ Coxbf899be2011-07-26 00:52:02 -040062