Skip to content

Commit 1737aef

Browse files
committed
cmd/compile: more error position tests for the typechecker
This change adds line position tests for several yyerror calls in the typechecker that are currently not tested in any way. Untested yyerror calls were found by replacing them with yerrorl(src.NoXPos, ...) (thus destroying position information in the error), and then running the test suite. No failures means no test coverage for the relevant yyerror call. For #19683 Change-Id: Iedb3d2f02141b332e9bfa76dbf5ae930ad2fddc3 Reviewed-on: https://go-review.googlesource.com/41477 Run-TryBot: Alberto Donizetti <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 26536b2 commit 1737aef

File tree

12 files changed

+134
-2
lines changed

12 files changed

+134
-2
lines changed

test/append1.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// errorcheck
2+
3+
// Copyright 2017 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 append arguments requirements are enforced by the
8+
// compiler.
9+
10+
package main
11+
12+
func main() {
13+
14+
s := make([]int, 8)
15+
16+
_ = append() // ERROR "missing arguments to append"
17+
_ = append(s...) // ERROR "cannot use ... on first argument"
18+
_ = append(s, 2, s...) // ERROR "too many arguments to append"
19+
20+
}

test/chan/perm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func main() {
2424
cr = cs // ERROR "illegal types|incompatible|cannot"
2525
cs = cr // ERROR "illegal types|incompatible|cannot"
2626

27+
var n int
28+
<-n // ERROR "receive from non-chan"
29+
n <- 2 // ERROR "send to non-chan"
30+
2731
c <- 0 // ok
2832
<-c // ok
2933
x, ok := <-c // ok
@@ -62,4 +66,5 @@ func main() {
6266
close(c)
6367
close(cs)
6468
close(cr) // ERROR "receive"
69+
close(n) // ERROR "invalid operation.*non-chan type"
6570
}

test/cmplx.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ var (
2828
C128 Complex128
2929
)
3030

31+
func F1() int {
32+
return 1
33+
}
34+
35+
func F3() (int, int, int) {
36+
return 1, 2, 3
37+
}
38+
3139
func main() {
3240
// ok
3341
c64 = complex(f32, f32)
@@ -41,6 +49,11 @@ func main() {
4149
_ = complex(f64, F64) // ERROR "complex"
4250
_ = complex(F64, f64) // ERROR "complex"
4351

52+
_ = complex(F1()) // ERROR "expects two arguments.*returns 1"
53+
_ = complex(F3()) // ERROR "expects two arguments.*returns 3"
54+
55+
_ = complex() // ERROR "missing argument"
56+
4457
c128 = complex(f32, f32) // ERROR "cannot use"
4558
c64 = complex(f64, f64) // ERROR "cannot use"
4659

@@ -51,4 +64,5 @@ func main() {
5164

5265
C64 = complex(f32, f32) // ERROR "cannot use"
5366
C128 = complex(f64, f64) // ERROR "cannot use"
67+
5468
}

test/complit1.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ var (
2222
_ = m[0][:] // ERROR "slice of unaddressable value"
2323
_ = f()[:] // ERROR "slice of unaddressable value"
2424

25+
_ = 301[:] // ERROR "cannot slice"
26+
_ = 3.1[:] // ERROR "cannot slice"
27+
_ = true[:] // ERROR "cannot slice"
28+
2529
// these are okay because they are slicing a pointer to an array
2630
_ = (&[3]int{1, 2, 3})[:]
2731
_ = mp[0][:]
@@ -35,10 +39,15 @@ type T struct {
3539
next *T
3640
}
3741

42+
type TP *T
43+
type Ti int
44+
3845
var (
3946
_ = &T{0, 0, "", nil} // ok
4047
_ = &T{i: 0, f: 0, s: "", next: {}} // ERROR "missing type in composite literal|omit types within composite literal"
4148
_ = &T{0, 0, "", {}} // ERROR "missing type in composite literal|omit types within composite literal"
49+
_ = TP{i: 0, f: 0, s: "", next: {}} // ERROR "invalid pointer type"
50+
_ = &Ti{} // ERROR "invalid pointer type"
4251
)
4352

4453
type M map[T]T

test/copy1.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// errorcheck
2+
3+
// Copyright 2017 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 copy arguments requirements are enforced by the
8+
// compiler.
9+
10+
package main
11+
12+
func main() {
13+
14+
si := make([]int, 8)
15+
sf := make([]float64, 8)
16+
17+
_ = copy() // ERROR "missing arguments"
18+
_ = copy(1, 2, 3) // ERROR "too many arguments"
19+
20+
_ = copy(si, "hi") // ERROR "have different element types.*int.*string"
21+
_ = copy(si, sf) // ERROR "have different element types.*int.*float64"
22+
23+
_ = copy(1, 2) // ERROR "must be slices; have int, int"
24+
_ = copy(1, si) // ERROR "first argument to copy should be"
25+
_ = copy(si, 2) // ERROR "second argument to copy should be"
26+
27+
}

test/ddd1.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var (
4242
_ = funny([]T{}) // ok because []T{} is a T; passes []T{[]T{}}
4343
)
4444

45+
func Foo(n int) {}
46+
4547
func bad(args ...int) {
4648
print(1, 2, args...) // ERROR "[.][.][.]"
4749
println(args...) // ERROR "[.][.][.]"
@@ -58,4 +60,6 @@ func bad(args ...int) {
5860
_ = unsafe.Sizeof(x...) // ERROR "[.][.][.]"
5961
_ = [...]byte("foo") // ERROR "[.][.][.]"
6062
_ = [...][...]int{{1,2,3},{4,5,6}} // ERROR "[.][.][.]"
63+
64+
Foo(x...) // ERROR "invalid use of [.][.][.] in call"
6165
}

test/initializerr.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var a2 = S { Y: 3, Z: 2, Y: 3 } // ERROR "duplicate"
2323
var a3 = T { S{}, 2, 3, 4, 5, 6 } // ERROR "convert|too many"
2424
var a4 = [5]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // ERROR "index|too many"
2525
var a5 = []byte { x: 2 } // ERROR "index"
26+
var a6 = []byte{1: 1, 2: 2, 1: 3} // ERROR "duplicate index"
2627

2728
var ok1 = S { } // should be ok
2829
var ok2 = T { S: ok1 } // should be ok

test/interface/explicit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ func main() {
5454

5555
e = E(t) // ok
5656
t = T(e) // ERROR "need explicit|need type assertion|incompatible"
57+
58+
// cannot type-assert non-interfaces
59+
f := 2.0
60+
_ = f.(int) // ERROR "non-interface type"
61+
5762
}
5863

5964
type M interface {

test/makenew.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// errorcheck
2+
3+
// Copyright 2017 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 make and new arguments requirements are enforced by the
8+
// compiler.
9+
10+
package main
11+
12+
func main() {
13+
_ = make() // ERROR "missing argument"
14+
_ = make(int) // ERROR "cannot make type"
15+
_ = make([]int) // ERROR "missing len argument"
16+
17+
_ = new() // ERROR "missing argument"
18+
_ = new(int, 2) // ERROR "too many arguments"
19+
}

test/map1.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
package main
1111

12-
func main() {}
13-
1412
type v bool
1513

1614
var (
@@ -60,3 +58,11 @@ type T5 *int
6058
type T6 struct { F T5 }
6159
type T7 *T4
6260
type T8 struct { F *T7 }
61+
62+
func main() {
63+
m := make(map[int]int)
64+
delete() // ERROR "missing arguments"
65+
delete(m) // ERROR "missing second \(key\) argument"
66+
delete(m, 2, 3) // ERROR "too many arguments"
67+
delete(1, m) // ERROR "first argument to delete must be map"
68+
}

test/recover5.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// errorcheck
2+
3+
// Copyright 2017 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 recover arguments requirements are enforced by the
8+
// compiler.
9+
10+
package main
11+
12+
func main() {
13+
_ = recover() // OK
14+
_ = recover(1) // ERROR "too many arguments"
15+
_ = recover(1, 2) // ERROR "too many arguments"
16+
}

test/shift1.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ func _() {
6868
w int64 = 1.0 << 33 // 1.0<<33 is a constant shift expression
6969
_, _, _, _, _, _, _, _, _, _ = j, k, m, n, o, u, u1, u2, v, w
7070
)
71+
72+
// non constants arguments trigger a different path
73+
f2 := 1.2
74+
s2 := "hi"
75+
_ = f2 << 2 // ERROR "shift of type float64"
76+
_ = s2 << 2 // ERROR "shift of type string"
7177
}
7278

7379
// shifts in comparisons w/ untyped operands

0 commit comments

Comments
 (0)