blob: 728eceb7f1e6b9cd755def6ae347d34724eee1a0 [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Rob Pike549a6002009-04-18 17:21:00 -07002
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 Pike83976e32012-02-19 14:28:53 +11007// Test that incorrect short declarations and redeclarations are detected.
8// Does not compile.
Rob Pike549a6002009-04-18 17:21:00 -07009
10package main
11
Russ Coxf2b5a072011-01-19 23:09:00 -050012func f1() int { return 1 }
13func f2() (float32, int) { return 1, 2 }
14func f3() (float32, int, string) { return 1, 2, "3" }
Rob Pike549a6002009-04-18 17:21:00 -070015
16func main() {
17 {
18 // simple redeclaration
Rob Pike4f61fc92010-09-04 10:36:13 +100019 i := f1()
Russ Coxf2b5a072011-01-19 23:09:00 -050020 i := f1() // ERROR "redeclared|no new"
Rob Pike4f61fc92010-09-04 10:36:13 +100021 _ = i
Robert Griesemer0dd5be42009-04-20 15:23:21 -070022 }
Rob Pike549a6002009-04-18 17:21:00 -070023 {
24 // change of type for f
Rob Pike4f61fc92010-09-04 10:36:13 +100025 i, f, s := f3()
Russ Coxf2b5a072011-01-19 23:09:00 -050026 f, g, t := f3() // ERROR "redeclared|cannot assign|incompatible"
Rob Pike4f61fc92010-09-04 10:36:13 +100027 _, _, _, _, _ = i, f, s, g, t
Rob Pike549a6002009-04-18 17:21:00 -070028 }
29 {
30 // change of type for i
Rob Pike4f61fc92010-09-04 10:36:13 +100031 i, f, s := f3()
Russ Coxf2b5a072011-01-19 23:09:00 -050032 j, i, t := f3() // ERROR "redeclared|cannot assign|incompatible"
Rob Pike4f61fc92010-09-04 10:36:13 +100033 _, _, _, _, _ = i, f, s, j, t
Rob Pike549a6002009-04-18 17:21:00 -070034 }
35 {
36 // no new variables
Rob Pike4f61fc92010-09-04 10:36:13 +100037 i, f, s := f3()
Russ Coxf2b5a072011-01-19 23:09:00 -050038 i, f := f2() // ERROR "redeclared|no new"
Rob Pike4f61fc92010-09-04 10:36:13 +100039 _, _, _ = i, f, s
Rob Pike549a6002009-04-18 17:21:00 -070040 }
41 {
Daniel Morsingdd166b92012-07-29 22:24:19 -040042 // multiline no new variables
43 i := f1
Rémy Oudompheng9844e4c2012-10-07 21:52:57 +020044 i := func() int { // ERROR "redeclared|no new|incompatible"
45 return 0
Daniel Morsingdd166b92012-07-29 22:24:19 -040046 }
Ian Lance Taylor6ed800c2012-09-28 08:30:30 -070047 _ = i
Daniel Morsingdd166b92012-07-29 22:24:19 -040048 }
49 {
Rob Pike549a6002009-04-18 17:21:00 -070050 // single redeclaration
Rob Pike4f61fc92010-09-04 10:36:13 +100051 i, f, s := f3()
Russ Coxa5d7c1f2011-08-16 11:14:26 -040052 i := 1 // ERROR "redeclared|no new|incompatible"
Rob Pike4f61fc92010-09-04 10:36:13 +100053 _, _, _ = i, f, s
Rob Pike549a6002009-04-18 17:21:00 -070054 }
Russ Coxf2b5a072011-01-19 23:09:00 -050055 // double redeclaration
Rob Pike549a6002009-04-18 17:21:00 -070056 {
Rob Pike4f61fc92010-09-04 10:36:13 +100057 i, f, s := f3()
Russ Coxf2b5a072011-01-19 23:09:00 -050058 i, f := f2() // ERROR "redeclared|no new"
Rob Pike4f61fc92010-09-04 10:36:13 +100059 _, _, _ = i, f, s
Rob Pike549a6002009-04-18 17:21:00 -070060 }
61 {
62 // triple redeclaration
Rob Pike4f61fc92010-09-04 10:36:13 +100063 i, f, s := f3()
Russ Coxf2b5a072011-01-19 23:09:00 -050064 i, f, s := f3() // ERROR "redeclared|no new"
Rob Pike4f61fc92010-09-04 10:36:13 +100065 _, _, _ = i, f, s
Rob Pike549a6002009-04-18 17:21:00 -070066 }
67}