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"
|
|
|
|
func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|redefinition of .m1."
|
2017-01-23 22:24:24 +00:00
|
|
|
func (A0) m1() {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
|
|
|
|
func (A0) m1() {} // ERROR "T0\.m1 redeclared in this block|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.
|
2017-01-24 20:43:52 +00:00
|
|
|
var _ N0 = T0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
|
|
|
|
var _ N0 = A0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
|
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{}
|
|
|
|
|
2017-01-24 20:43:52 +00:00
|
|
|
var _ N0 = T0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
|
|
|
|
var _ N0 = A0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
|
2017-01-11 19:24:35 +00:00
|
|
|
|
2017-01-24 20:43:52 +00:00
|
|
|
var _ A5 = Value{} // ERROR "cannot use reflect\.Value literal \(type reflect.Value\) as type A5 in assignment|incompatible type"
|
2016-12-17 00:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid type alias declarations.
|
|
|
|
|
2017-01-24 20:43:52 +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
|