]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: more error position tests for the typechecker
authorAlberto Donizetti <alb.donizetti@gmail.com>
Sat, 22 Apr 2017 13:28:58 +0000 (15:28 +0200)
committerAlberto Donizetti <alb.donizetti@gmail.com>
Mon, 24 Apr 2017 12:37:49 +0000 (12:37 +0000)
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>
12 files changed:
test/append1.go [new file with mode: 0644]
test/chan/perm.go
test/cmplx.go
test/complit1.go
test/copy1.go [new file with mode: 0644]
test/ddd1.go
test/initializerr.go
test/interface/explicit.go
test/makenew.go [new file with mode: 0644]
test/map1.go
test/recover5.go [new file with mode: 0644]
test/shift1.go

diff --git a/test/append1.go b/test/append1.go
new file mode 100644 (file)
index 0000000..6d42368
--- /dev/null
@@ -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"
+
+}
index 919fa30fbf1410bb8b9a4ebcf4c5fcdebaf91855..13269b431bf0063779d4efd1e134e8ad3d75cf0d 100644 (file)
@@ -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"
 }
index 2d8a6229d62d59126fc26ad477c50a5e988c5798..dedf2bd8d355fbbf9cb6b6d08ff76427df3be4b1 100644 (file)
@@ -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"
+
 }
index 9dde99437670658c62a0c4cb2f5eebd8bbc7f800..83695a9e88c0299e509b6e6ea326ffd58dba89be 100644 (file)
@@ -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
diff --git a/test/copy1.go b/test/copy1.go
new file mode 100644 (file)
index 0000000..1428549
--- /dev/null
@@ -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"
+
+}
index cf6a3a58734144005146eb0b58d48e03177e963c..4284e32137b5ed8a09025ad481dee3c72243888c 100644 (file)
@@ -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"
 }
index ca05414554d3480cabe1ce6a52207e8f72390644..990ab60f96b0065bf6c46d4020242f03e8cdb2f8 100644 (file)
@@ -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
index 3c449b13ada192821dd86a2260151d3190bb6f1c..1fb3b6a05a2c4d91d6a7d027e5a3df635081e9ce 100644 (file)
@@ -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 {
diff --git a/test/makenew.go b/test/makenew.go
new file mode 100644 (file)
index 0000000..058d975
--- /dev/null
@@ -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"
+}
index d3c0a9093b53fd0211fc94bf38cfc916fa5e1ff1..498c2ec45bd88c717d68d37b23dbfc97d3aafb4d 100644 (file)
@@ -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"
+}
\ No newline at end of file
diff --git a/test/recover5.go b/test/recover5.go
new file mode 100644 (file)
index 0000000..0e93f5e
--- /dev/null
@@ -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"
+}
index aeefbc45174d8f1b5fdc8ae38fcae3a6ce176961..c81ee5154dc70fef7927a0670bb595ef3e3a2021 100644 (file)
@@ -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