1// errorcheck 2 3// Copyright 2016 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7// Test basic restrictions on type aliases. 8 9package p 10 11import ( 12 "reflect" 13 . "reflect" 14) 15 16type T0 struct{} 17 18// Valid type alias declarations. 19 20type _ = T0 21type _ = int 22type _ = struct{} 23type _ = reflect.Value 24type _ = Value 25 26type ( 27 A0 = T0 28 A1 = int 29 A2 = struct{} 30 A3 = reflect.Value 31 A4 = Value 32 A5 = Value 33 34 N0 A0 35) 36 37// Methods can be declared on the original named type and the alias. 38func (T0) m1() {} // GCCGO_ERROR "previous" 39func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|T0\.m1 already declared|redefinition of .m1." 40func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1." 41func (A0) m1() {} // ERROR "T0\.m1 already declared|redefinition of .m1." 42func (A0) m2() {} 43 44// Type aliases and the original type name can be used interchangeably. 45var _ A0 = T0{} 46var _ T0 = A0{} 47 48// But aliases and original types cannot be used with new types based on them. 49var _ N0 = T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration" 50var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type A0\) as N0 value in variable declaration" 51 52var _ A5 = Value{} 53 54var _ interface { 55 m1() 56 m2() 57} = T0{} 58 59var _ interface { 60 m1() 61 m2() 62} = A0{} 63 64func _() { 65 type _ = T0 66 type _ = int 67 type _ = struct{} 68 type _ = reflect.Value 69 type _ = Value 70 71 type ( 72 A0 = T0 73 A1 = int 74 A2 = struct{} 75 A3 = reflect.Value 76 A4 = Value 77 A5 Value 78 79 N0 A0 80 ) 81 82 var _ A0 = T0{} 83 var _ T0 = A0{} 84 85 var _ N0 = T0{} // ERROR "cannot use T0{} \(value of type T0\) as N0 value in variable declaration" 86 var _ N0 = A0{} // ERROR "cannot use A0{} \(value of type A0\) as N0 value in variable declaration" 87 88 var _ A5 = Value{} // ERROR "cannot use Value{} \(value of type reflect\.Value\) as A5 value in variable declaration" 89} 90 91// Invalid type alias declarations. 92 93type _ = reflect.ValueOf // ERROR "reflect.ValueOf .*is not a type|expected type" 94 95func (A1) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type" 96func (A2) m() {} // ERROR "invalid receiver type" 97func (A3) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type" 98func (A4) m() {} // ERROR "cannot define new methods on non-local type|may not define methods on non-local type" 99 100type B1 = struct{} 101 102func (B1) m() {} // ERROR "invalid receiver type" 103 104// TODO(gri) expand 105