Skip to content

Commit

Permalink
cmd/compile: more error position tests for the typechecker
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ALTree committed Apr 24, 2017
1 parent 26536b2 commit 1737aef
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 2 deletions.
20 changes: 20 additions & 0 deletions test/append1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// errorcheck

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Verify that append arguments requirements are enforced by the
// compiler.

package main

func main() {

s := make([]int, 8)

_ = append() // ERROR "missing arguments to append"
_ = append(s...) // ERROR "cannot use ... on first argument"
_ = append(s, 2, s...) // ERROR "too many arguments to append"

}
5 changes: 5 additions & 0 deletions test/chan/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func main() {
cr = cs // ERROR "illegal types|incompatible|cannot"
cs = cr // ERROR "illegal types|incompatible|cannot"

var n int
<-n // ERROR "receive from non-chan"
n <- 2 // ERROR "send to non-chan"

c <- 0 // ok
<-c // ok
x, ok := <-c // ok
Expand Down Expand Up @@ -62,4 +66,5 @@ func main() {
close(c)
close(cs)
close(cr) // ERROR "receive"
close(n) // ERROR "invalid operation.*non-chan type"
}
14 changes: 14 additions & 0 deletions test/cmplx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ var (
C128 Complex128
)

func F1() int {
return 1
}

func F3() (int, int, int) {
return 1, 2, 3
}

func main() {
// ok
c64 = complex(f32, f32)
Expand All @@ -41,6 +49,11 @@ func main() {
_ = complex(f64, F64) // ERROR "complex"
_ = complex(F64, f64) // ERROR "complex"

_ = complex(F1()) // ERROR "expects two arguments.*returns 1"
_ = complex(F3()) // ERROR "expects two arguments.*returns 3"

_ = complex() // ERROR "missing argument"

c128 = complex(f32, f32) // ERROR "cannot use"
c64 = complex(f64, f64) // ERROR "cannot use"

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

C64 = complex(f32, f32) // ERROR "cannot use"
C128 = complex(f64, f64) // ERROR "cannot use"

}
9 changes: 9 additions & 0 deletions test/complit1.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var (
_ = m[0][:] // ERROR "slice of unaddressable value"
_ = f()[:] // ERROR "slice of unaddressable value"

_ = 301[:] // ERROR "cannot slice"
_ = 3.1[:] // ERROR "cannot slice"
_ = true[:] // ERROR "cannot slice"

// these are okay because they are slicing a pointer to an array
_ = (&[3]int{1, 2, 3})[:]
_ = mp[0][:]
Expand All @@ -35,10 +39,15 @@ type T struct {
next *T
}

type TP *T
type Ti int

var (
_ = &T{0, 0, "", nil} // ok
_ = &T{i: 0, f: 0, s: "", next: {}} // ERROR "missing type in composite literal|omit types within composite literal"
_ = &T{0, 0, "", {}} // ERROR "missing type in composite literal|omit types within composite literal"
_ = TP{i: 0, f: 0, s: "", next: {}} // ERROR "invalid pointer type"
_ = &Ti{} // ERROR "invalid pointer type"
)

type M map[T]T
Expand Down
27 changes: 27 additions & 0 deletions test/copy1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// errorcheck

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Verify that copy arguments requirements are enforced by the
// compiler.

package main

func main() {

si := make([]int, 8)
sf := make([]float64, 8)

_ = copy() // ERROR "missing arguments"
_ = copy(1, 2, 3) // ERROR "too many arguments"

_ = copy(si, "hi") // ERROR "have different element types.*int.*string"
_ = copy(si, sf) // ERROR "have different element types.*int.*float64"

_ = copy(1, 2) // ERROR "must be slices; have int, int"
_ = copy(1, si) // ERROR "first argument to copy should be"
_ = copy(si, 2) // ERROR "second argument to copy should be"

}
4 changes: 4 additions & 0 deletions test/ddd1.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var (
_ = funny([]T{}) // ok because []T{} is a T; passes []T{[]T{}}
)

func Foo(n int) {}

func bad(args ...int) {
print(1, 2, args...) // ERROR "[.][.][.]"
println(args...) // ERROR "[.][.][.]"
Expand All @@ -58,4 +60,6 @@ func bad(args ...int) {
_ = unsafe.Sizeof(x...) // ERROR "[.][.][.]"
_ = [...]byte("foo") // ERROR "[.][.][.]"
_ = [...][...]int{{1,2,3},{4,5,6}} // ERROR "[.][.][.]"

Foo(x...) // ERROR "invalid use of [.][.][.] in call"
}
1 change: 1 addition & 0 deletions test/initializerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var a2 = S { Y: 3, Z: 2, Y: 3 } // ERROR "duplicate"
var a3 = T { S{}, 2, 3, 4, 5, 6 } // ERROR "convert|too many"
var a4 = [5]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } // ERROR "index|too many"
var a5 = []byte { x: 2 } // ERROR "index"
var a6 = []byte{1: 1, 2: 2, 1: 3} // ERROR "duplicate index"

var ok1 = S { } // should be ok
var ok2 = T { S: ok1 } // should be ok
Expand Down
5 changes: 5 additions & 0 deletions test/interface/explicit.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func main() {

e = E(t) // ok
t = T(e) // ERROR "need explicit|need type assertion|incompatible"

// cannot type-assert non-interfaces
f := 2.0
_ = f.(int) // ERROR "non-interface type"

}

type M interface {
Expand Down
19 changes: 19 additions & 0 deletions test/makenew.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// errorcheck

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Verify that make and new arguments requirements are enforced by the
// compiler.

package main

func main() {
_ = make() // ERROR "missing argument"
_ = make(int) // ERROR "cannot make type"
_ = make([]int) // ERROR "missing len argument"

_ = new() // ERROR "missing argument"
_ = new(int, 2) // ERROR "too many arguments"
}
10 changes: 8 additions & 2 deletions test/map1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

package main

func main() {}

type v bool

var (
Expand Down Expand Up @@ -60,3 +58,11 @@ type T5 *int
type T6 struct { F T5 }
type T7 *T4
type T8 struct { F *T7 }

func main() {
m := make(map[int]int)
delete() // ERROR "missing arguments"
delete(m) // ERROR "missing second \(key\) argument"
delete(m, 2, 3) // ERROR "too many arguments"
delete(1, m) // ERROR "first argument to delete must be map"
}
16 changes: 16 additions & 0 deletions test/recover5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// errorcheck

// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Verify that recover arguments requirements are enforced by the
// compiler.

package main

func main() {
_ = recover() // OK
_ = recover(1) // ERROR "too many arguments"
_ = recover(1, 2) // ERROR "too many arguments"
}
6 changes: 6 additions & 0 deletions test/shift1.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func _() {
w int64 = 1.0 << 33 // 1.0<<33 is a constant shift expression
_, _, _, _, _, _, _, _, _, _ = j, k, m, n, o, u, u1, u2, v, w
)

// non constants arguments trigger a different path
f2 := 1.2
s2 := "hi"
_ = f2 << 2 // ERROR "shift of type float64"
_ = s2 << 2 // ERROR "shift of type string"
}

// shifts in comparisons w/ untyped operands
Expand Down

0 comments on commit 1737aef

Please sign in to comment.