Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 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 | // Test that incorrect short declarations and redeclarations are detected. |
| 8 | // Does not compile. |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 9 | |
| 10 | package main |
| 11 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 12 | func f1() int { return 1 } |
| 13 | func f2() (float32, int) { return 1, 2 } |
| 14 | func f3() (float32, int, string) { return 1, 2, "3" } |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 15 | |
| 16 | func main() { |
| 17 | { |
| 18 | // simple redeclaration |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 19 | i := f1() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 20 | i := f1() // ERROR "redeclared|no new" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 21 | _ = i |
Robert Griesemer | 0dd5be4 | 2009-04-20 15:23:21 -0700 | [diff] [blame] | 22 | } |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 23 | { |
| 24 | // change of type for f |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 25 | i, f, s := f3() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 26 | f, g, t := f3() // ERROR "redeclared|cannot assign|incompatible" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 27 | _, _, _, _, _ = i, f, s, g, t |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 28 | } |
| 29 | { |
| 30 | // change of type for i |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 31 | i, f, s := f3() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 32 | j, i, t := f3() // ERROR "redeclared|cannot assign|incompatible" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 33 | _, _, _, _, _ = i, f, s, j, t |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 34 | } |
| 35 | { |
| 36 | // no new variables |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 37 | i, f, s := f3() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 38 | i, f := f2() // ERROR "redeclared|no new" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 39 | _, _, _ = i, f, s |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 40 | } |
| 41 | { |
Daniel Morsing | dd166b9 | 2012-07-29 22:24:19 -0400 | [diff] [blame] | 42 | // multiline no new variables |
| 43 | i := f1 |
Rémy Oudompheng | 9844e4c | 2012-10-07 21:52:57 +0200 | [diff] [blame] | 44 | i := func() int { // ERROR "redeclared|no new|incompatible" |
| 45 | return 0 |
Daniel Morsing | dd166b9 | 2012-07-29 22:24:19 -0400 | [diff] [blame] | 46 | } |
Ian Lance Taylor | 6ed800c | 2012-09-28 08:30:30 -0700 | [diff] [blame] | 47 | _ = i |
Daniel Morsing | dd166b9 | 2012-07-29 22:24:19 -0400 | [diff] [blame] | 48 | } |
| 49 | { |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 50 | // single redeclaration |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 51 | i, f, s := f3() |
Russ Cox | a5d7c1f | 2011-08-16 11:14:26 -0400 | [diff] [blame] | 52 | i := 1 // ERROR "redeclared|no new|incompatible" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 53 | _, _, _ = i, f, s |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 54 | } |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 55 | // double redeclaration |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 56 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 57 | i, f, s := f3() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 58 | i, f := f2() // ERROR "redeclared|no new" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 59 | _, _, _ = i, f, s |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 60 | } |
| 61 | { |
| 62 | // triple redeclaration |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 63 | i, f, s := f3() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 64 | i, f, s := f3() // ERROR "redeclared|no new" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 65 | _, _, _ = i, f, s |
Rob Pike | 549a600 | 2009-04-18 17:21:00 -0700 | [diff] [blame] | 66 | } |
| 67 | } |