Skip to content

Commit

Permalink
[dev.typeparams] cmd/compile/internal/types2: adjust tests, enable Te…
Browse files Browse the repository at this point in the history
…stdata tests

Types2 uses a different test runner and has fewer/better
errors in some cases (error messages match the compiler).
Adjust the tests and enable them.

Change-Id: I74877f54a81a3918a80774452cef5bcaad8a98e6
Reviewed-on: https://go-review.googlesource.com/c/go/+/263631
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
griesemer committed Oct 21, 2020
1 parent befc62a commit 1d07306
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 56 deletions.
3 changes: 1 addition & 2 deletions src/cmd/compile/internal/types2/check_test.go
Expand Up @@ -221,8 +221,7 @@ func TestCheck(t *testing.T) {
checkFiles(t, strings.Split(*testFiles, " "), 0, testing.Verbose())
}

// TODO(gri) Enable once we have added the testdata tests.
// func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, 75, "testdata") } // TODO(gri) narrow column tolerance
func TestTestdata(t *testing.T) { DefPredeclaredTestFuncs(); testDir(t, 75, "testdata") } // TODO(gri) narrow column tolerance
func TestExamples(t *testing.T) { testDir(t, 0, "examples") }
func TestFixedbugs(t *testing.T) { testDir(t, 0, "fixedbugs") }

Expand Down
10 changes: 5 additions & 5 deletions src/cmd/compile/internal/types2/testdata/builtins.src
Expand Up @@ -25,11 +25,11 @@ func append1() {
_ = append(s, b)
_ = append(s, x /* ERROR cannot use x */ )
_ = append(s, s /* ERROR cannot use s */ )
_ = append(s... /* ERROR can only use ... with matching parameter */ )
_ = append(s, b, s... /* ERROR can only use ... with matching parameter */ )
_ = append(s... ) /* ERROR not enough arguments */
_ = append(s, b, s /* ERROR too many arguments */ ... )
_ = append(s, 1, 2, 3)
_ = append(s, 1, 2, 3, x /* ERROR cannot use x */ , 5, 6, 6)
_ = append(s, 1, 2, s... /* ERROR can only use ... with matching parameter */ )
_ = append(s, 1, 2 /* ERROR too many arguments */ , s... )
_ = append([]interface{}(nil), 1, 2, "foo", x, 3.1425, false)

type S []byte
Expand Down Expand Up @@ -482,7 +482,7 @@ func make1() {
}

func make2() {
f1 /* ERROR not used */ := func() (x []int) { return }
f1 := func() (x []int) { return }
_ = make(f0 /* ERROR not a type */ ())
_ = make(f1 /* ERROR not a type */ ())
}
Expand All @@ -502,7 +502,7 @@ func new1() {
}

func new2() {
f1 /* ERROR not used */ := func() (x []int) { return }
f1 := func() (x []int) { return }
_ = new(f0 /* ERROR not a type */ ())
_ = new(f1 /* ERROR not a type */ ())
}
Expand Down
19 changes: 8 additions & 11 deletions src/cmd/compile/internal/types2/testdata/constdecl.src
Expand Up @@ -21,20 +21,17 @@ func _() {
}

// Identifier and expression arity must match.
// The first error message is produced by the parser.
// In a real-world scenario, the type-checker would not be run
// in this case and the 2nd error message would not appear.
const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
const _ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */

const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
const _ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */

const (
_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
_ /* ERROR "missing init expr for _" */
_ = 1, 2 /* ERROR "extra init expr 2" */

_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
_ /* ERROR "missing init expr for _" */ int
_ int = 1, 2 /* ERROR "extra init expr 2" */
)

Expand All @@ -55,17 +52,17 @@ const (
)

func _() {
const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
const _ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */

const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
const _ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */

const (
_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
_ /* ERROR "missing init expr for _" */
_ = 1, 2 /* ERROR "extra init expr 2" */

_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
_ /* ERROR "missing init expr for _" */ int
_ int = 1, 2 /* ERROR "extra init expr 2" */
)

Expand Down
10 changes: 5 additions & 5 deletions src/cmd/compile/internal/types2/testdata/decls0.src
Expand Up @@ -49,7 +49,7 @@ func _() { var init int; _ = init }

// invalid array types
type (
iA0 [... /* ERROR "invalid use of '...'" */ ]byte
iA0 [... /* ERROR "invalid use of \[...\] array" */ ]byte
// The error message below could be better. At the moment
// we believe an integer that is too large is not an integer.
// But at least we get an error.
Expand Down Expand Up @@ -185,10 +185,10 @@ func f2(x *f2 /* ERROR "not a type" */ ) {}
func f3() (x f3 /* ERROR "not a type" */ ) { return }
func f4() (x *f4 /* ERROR "not a type" */ ) { return }

func (S0) m1 /* ERROR illegal cycle */ (x S0 /* ERROR value .* is not a type */ .m1) {}
func (S0) m2 /* ERROR illegal cycle */ (x *S0 /* ERROR value .* is not a type */ .m2) {}
func (S0) m3 /* ERROR illegal cycle */ () (x S0 /* ERROR value .* is not a type */ .m3) { return }
func (S0) m4 /* ERROR illegal cycle */ () (x *S0 /* ERROR value .* is not a type */ .m4) { return }
func (S0) m1(x S0 /* ERROR value .* is not a type */ .m1) {}
func (S0) m2(x *S0 /* ERROR value .* is not a type */ .m2) {}
func (S0) m3() (x S0 /* ERROR value .* is not a type */ .m3) { return }
func (S0) m4() (x *S0 /* ERROR value .* is not a type */ .m4) { return }

// interfaces may not have any blank methods
type BlankI interface {
Expand Down
111 changes: 111 additions & 0 deletions src/cmd/compile/internal/types2/testdata/decls2/decls2a.src
@@ -0,0 +1,111 @@
// Copyright 2012 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.

// method declarations

package decls2

import "time"
import "unsafe"

// T1 declared before its methods.
type T1 struct{
f int
}

func (T1) m() {}
func (T1) m /* ERROR "already declared" */ () {}
func (x *T1) f /* ERROR "field and method" */ () {}

// Conflict between embedded field and method name,
// with the embedded field being a basic type.
type T1b struct {
int
}

func (T1b) int /* ERROR "field and method" */ () {}

type T1c struct {
time.Time
}

func (T1c) Time /* ERROR "field and method" */ () int { return 0 }

// Disabled for now: LookupFieldOrMethod will find Pointer even though
// it's double-declared (it would cost extra in the common case to verify
// this). But the MethodSet computation will not find it due to the name
// collision caused by the double-declaration, leading to an internal
// inconsistency while we are verifying one computation against the other.
// var _ = T1c{}.Pointer

// T2's method declared before the type.
func (*T2) f /* ERROR "field and method" */ () {}

type T2 struct {
f int
}

// Methods declared without a declared type.
func (undeclared /* ERROR "undeclared" */) m() {}
func (x *undeclared /* ERROR "undeclared" */) m() {}

func (pi /* ERROR "not a type" */) m1() {}
func (x pi /* ERROR "not a type" */) m2() {}
func (x *pi /* ERROR "not a type" */ ) m3() {}

// Blank types.
type _ struct { m int }
type _ struct { m int }

func (_ /* ERROR "cannot use _" */) m() {}
func m(_ /* ERROR "cannot use _" */) {}

// Methods with receiver base type declared in another file.
func (T3) m1() {}
func (*T3) m2() {}
func (x T3) m3() {}
func (x *T3) f /* ERROR "field and method" */ () {}

// Methods of non-struct type.
type T4 func()

func (self T4) m() func() { return self }

// Methods associated with an interface.
type T5 interface {
m() int
}

func (T5 /* ERROR "invalid receiver" */ ) m1() {}
func (T5 /* ERROR "invalid receiver" */ ) m2() {}

// Methods associated with a named pointer type.
type ptr *int
func (ptr /* ERROR "invalid receiver" */ ) _() {}
func (* /* ERROR "invalid receiver" */ ptr) _() {}

// Methods with zero or multiple receivers.
func ( /* ERROR "no receiver" */ ) _() {}
func (T3, * /* ERROR "multiple receivers" */ T3) _() {}
func (T3, T3, T3 /* ERROR "multiple receivers" */ ) _() {}
func (a, b /* ERROR "multiple receivers" */ T3) _() {}
func (a, b, c /* ERROR "multiple receivers" */ T3) _() {}

// Methods associated with non-local or unnamed types.
func (int /* ERROR "invalid receiver" */ ) m() {}
func ([ /* ERROR "invalid receiver" */ ]int) m() {}
func (time /* ERROR "invalid receiver" */ .Time) m() {}
func (* /* ERROR "invalid receiver" */ time.Time) m() {}
func (x /* ERROR "invalid receiver" */ interface{}) m() {}

// Unsafe.Pointer is treated like a pointer when used as receiver type.
type UP unsafe.Pointer
func (UP /* ERROR "invalid" */ ) m1() {}
func (* /* ERROR "invalid" */ UP) m2() {}

// Double declarations across package files
const c_double = 0
type t_double int
var v_double int
func f_double() {}
75 changes: 75 additions & 0 deletions src/cmd/compile/internal/types2/testdata/decls2/decls2b.src
@@ -0,0 +1,75 @@
// Copyright 2012 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.

// method declarations

package decls2

import "io"

const pi = 3.1415

func (T1) m /* ERROR "already declared" */ () {}
func (T2) m(io.Writer) {}

type T3 struct {
f *T3
}

type T6 struct {
x int
}

func (t *T6) m1() int {
return t.x
}

func f() {
var t *T6
t.m1()
}

// Double declarations across package files
const c_double /* ERROR "redeclared" */ = 0
type t_double /* ERROR "redeclared" */ int
var v_double /* ERROR "redeclared" */ int
func f_double /* ERROR "redeclared" */ () {}

// Blank methods need to be type-checked.
// Verify by checking that errors are reported.
func (T /* ERROR "undeclared" */ ) _() {}
func (T1) _(undeclared /* ERROR "undeclared" */ ) {}
func (T1) _() int { return "foo" /* ERROR "cannot convert" */ }

// Methods with undeclared receiver type can still be checked.
// Verify by checking that errors are reported.
func (Foo /* ERROR "undeclared" */ ) m() {}
func (Foo /* ERROR "undeclared" */ ) m(undeclared /* ERROR "undeclared" */ ) {}
func (Foo /* ERROR "undeclared" */ ) m() int { return "foo" /* ERROR "cannot convert" */ }

func (Foo /* ERROR "undeclared" */ ) _() {}
func (Foo /* ERROR "undeclared" */ ) _(undeclared /* ERROR "undeclared" */ ) {}
func (Foo /* ERROR "undeclared" */ ) _() int { return "foo" /* ERROR "cannot convert" */ }

// Receiver declarations are regular parameter lists;
// receiver types may use parentheses, and the list
// may have a trailing comma.
type T7 struct {}

func (T7) m1() {}
func ((T7)) m2() {}
func ((*T7)) m3() {}
func (x *(T7),) m4() {}
func (x (*(T7)),) m5() {}
func (x ((*((T7)))),) m6() {}

// Check that methods with parenthesized receiver are actually present (issue #23130).
var (
_ = T7.m1
_ = T7.m2
_ = (*T7).m3
_ = (*T7).m4
_ = (*T7).m5
_ = (*T7).m6
)

0 comments on commit 1d07306

Please sign in to comment.