mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
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 <alb.donizetti@gmail.com> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
26536b2f32
commit
1737aef270
20
test/append1.go
Normal file
20
test/append1.go
Normal file
@ -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"
|
||||
|
||||
}
|
@ -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
|
||||
@ -62,4 +66,5 @@ func main() {
|
||||
close(c)
|
||||
close(cs)
|
||||
close(cr) // ERROR "receive"
|
||||
close(n) // ERROR "invalid operation.*non-chan type"
|
||||
}
|
||||
|
@ -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)
|
||||
@ -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"
|
||||
|
||||
@ -51,4 +64,5 @@ func main() {
|
||||
|
||||
C64 = complex(f32, f32) // ERROR "cannot use"
|
||||
C128 = complex(f64, f64) // ERROR "cannot use"
|
||||
|
||||
}
|
||||
|
@ -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][:]
|
||||
@ -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
|
||||
|
27
test/copy1.go
Normal file
27
test/copy1.go
Normal file
@ -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"
|
||||
|
||||
}
|
@ -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 "[.][.][.]"
|
||||
@ -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"
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
19
test/makenew.go
Normal file
19
test/makenew.go
Normal file
@ -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
test/map1.go
10
test/map1.go
@ -9,8 +9,6 @@
|
||||
|
||||
package main
|
||||
|
||||
func main() {}
|
||||
|
||||
type v bool
|
||||
|
||||
var (
|
||||
@ -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
test/recover5.go
Normal file
16
test/recover5.go
Normal file
@ -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"
|
||||
}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user