Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 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 | |
Rob Pike | 83976e3 | 2012-02-19 14:28:53 +1100 | [diff] [blame] | 7 | // Verify that illegal uses of ... are detected. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 10 | package main |
| 11 | |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 12 | import "unsafe" |
| 13 | |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 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" |
Luuk van Dijk | e14d1d7 | 2011-12-14 17:34:35 +0100 | [diff] [blame] | 21 | _ = sum("hello") // ERROR ".hello. .type string. as type int|incompatible" |
Luuk van Dijk | b536adb | 2011-10-08 19:37:06 +0200 | [diff] [blame] | 22 | _ = sum([]int{1}) // ERROR "\[\]int literal.*as type int|incompatible" |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
Rémy Oudompheng | 656b192 | 2012-07-13 08:05:41 +0200 | [diff] [blame] | 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()) |
Ian Lance Taylor | 6ed800c | 2012-09-28 08:30:30 -0700 | [diff] [blame] | 30 | _ = sum(tuple()...) // ERROR "multiple-value|[.][.][.]" |
Rémy Oudompheng | 656b192 | 2012-07-13 08:05:41 +0200 | [diff] [blame] | 31 | _ = sum3(tuple()) |
Ian Lance Taylor | 6ed800c | 2012-09-28 08:30:30 -0700 | [diff] [blame] | 32 | _ = sum3(tuple()...) // ERROR "multiple-value|[.][.][.]" "not enough" |
Rémy Oudompheng | 656b192 | 2012-07-13 08:05:41 +0200 | [diff] [blame] | 33 | ) |
| 34 | |
Russ Cox | 68796b0 | 2010-02-01 00:25:59 -0800 | [diff] [blame] | 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 | ) |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 44 | |
| 45 | func 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 Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 51 | _ = 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 Martin | 5b62ba1 | 2011-05-31 15:41:47 -0400 | [diff] [blame] | 59 | _ = [...]byte("foo") // ERROR "[.][.][.]" |
Russ Cox | bf899be | 2011-07-26 00:52:02 -0400 | [diff] [blame] | 60 | _ = [...][...]int{{1,2,3},{4,5,6}} // ERROR "[.][.][.]" |
Russ Cox | 75dd8fd | 2010-09-24 11:55:30 -0400 | [diff] [blame] | 61 | } |
Russ Cox | bf899be | 2011-07-26 00:52:02 -0400 | [diff] [blame] | 62 | |