2016-12-17 00:28:30 +00:00
|
|
|
// errorcheck
|
|
|
|
|
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
// Test basic restrictions on type aliases.
|
|
|
|
|
|
|
|
package p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
. "reflect"
|
|
|
|
)
|
|
|
|
|
2017-01-11 19:24:35 +00:00
|
|
|
type T0 struct{}
|
|
|
|
|
2016-12-17 00:28:30 +00:00
|
|
|
// Valid type alias declarations.
|
|
|
|
|
2017-01-11 19:24:35 +00:00
|
|
|
type _ = T0
|
|
|
|
type _ = int
|
|
|
|
type _ = struct{}
|
|
|
|
type _ = reflect.Value
|
|
|
|
type _ = Value
|
2016-12-17 00:28:30 +00:00
|
|
|
|
|
|
|
type (
|
2017-01-11 19:24:35 +00:00
|
|
|
A0 = T0
|
|
|
|
A1 = int
|
|
|
|
A2 = struct{}
|
|
|
|
A3 = reflect.Value
|
|
|
|
A4 = Value
|
|
|
|
A5 = Value
|
|
|
|
|
|
|
|
N0 A0
|
2016-12-17 00:28:30 +00:00
|
|
|
)
|
|
|
|
|
2017-01-11 19:24:35 +00:00
|
|
|
// Methods can be declared on the original named type and the alias.
|
2017-01-24 20:43:52 +00:00
|
|
|
func (T0) m1() {} // GCCGO_ERROR "previous"
|
2022-09-27 04:27:20 +00:00
|
|
|
func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 already declared|redefinition of .m1."
|
|
|
|
func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
|
|
|
|
func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1."
|
2017-01-14 01:23:01 +00:00
|
|
|
func (A0) m2() {}
|
2017-01-11 19:24:35 +00:00
|
|
|
|
|
|
|
// Type aliases and the original type name can be used interchangeably.
|
|
|
|
var _ A0 = T0{}
|
|
|
|
var _ T0 = A0{}
|
|
|
|
|
|
|
|
// But aliases and original types cannot be used with new types based on them.
|
2022-09-28 21:13:24 +00:00
|
|
|
var _ N0 = T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration"
|
|
|
|
var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type T0\) as N0 value in variable declaration"
|
2017-01-11 19:24:35 +00:00
|
|
|
|
|
|
|
var _ A5 = Value{}
|
|
|
|
|
|
|
|
var _ interface {
|
|
|
|
m1()
|
|
|
|
m2()
|
|
|
|
} = T0{}
|
|
|
|
|
|
|
|
var _ interface {
|
|
|
|
m1()
|
|
|
|
m2()
|
|
|
|
} = A0{}
|
|
|
|
|
2016-12-17 00:28:30 +00:00
|
|
|
func _() {
|
2017-01-11 19:24:35 +00:00
|
|
|
type _ = T0
|
|
|
|
type _ = int
|
|
|
|
type _ = struct{}
|
|
|
|
type _ = reflect.Value
|
|
|
|
type _ = Value
|
2016-12-17 00:28:30 +00:00
|
|
|
|
|
|
|
type (
|
2017-01-11 19:24:35 +00:00
|
|
|
A0 = T0
|
|
|
|
A1 = int
|
|
|
|
A2 = struct{}
|
|
|
|
A3 = reflect.Value
|
|
|
|
A4 = Value
|
|
|
|
A5 Value
|
|
|
|
|
|
|
|
N0 A0
|
2016-12-17 00:28:30 +00:00
|
|
|
)
|
2017-01-11 19:24:35 +00:00
|
|
|
|
|
|
|
var _ A0 = T0{}
|
|
|
|
var _ T0 = A0{}
|
|
|
|
|
2022-09-28 21:13:24 +00:00
|
|
|
var _ N0 = T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration"
|
|
|
|
var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type T0\) as N0 value in variable declaration"
|
2017-01-11 19:24:35 +00:00
|
|
|
|
2022-09-28 21:13:24 +00:00
|
|
|
var _ A5 = Value{} // ERROR "cannot use Value{} \(value of type reflect\.Value\) as A5 value in variable declaration"
|
2016-12-17 00:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid type alias declarations.
|
|
|
|
|
2020-12-01 05:38:49 +00:00
|
|
|
type _ = reflect.ValueOf // ERROR "reflect.ValueOf .*is not a type|expected type"
|
2017-01-11 19:24:35 +00:00
|
|
|
|
2017-01-24 20:43:52 +00:00
|
|
|
func (A1) m() {} // ERROR "cannot define new methods on non-local type int|may not define methods on non-local type"
|
|
|
|
func (A2) m() {} // ERROR "invalid receiver type"
|
|
|
|
func (A3) m() {} // ERROR "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
|
2018-04-05 01:42:39 +00:00
|
|
|
func (A4) m() {} // ERROR "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
|
2017-01-11 19:24:35 +00:00
|
|
|
|
|
|
|
type B1 = struct{}
|
2016-12-17 00:28:30 +00:00
|
|
|
|
2018-04-05 01:42:39 +00:00
|
|
|
func (B1) m() {} // ERROR "invalid receiver type"
|
2016-12-17 00:28:30 +00:00
|
|
|
|
|
|
|
// TODO(gri) expand
|