Home | History | Annotate | Download | only in test
      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 
      9 package p
     10 
     11 import (
     12 	"reflect"
     13 	. "reflect"
     14 )
     15 
     16 type T0 struct{}
     17 
     18 // Valid type alias declarations.
     19 
     20 type _ = T0
     21 type _ = int
     22 type _ = struct{}
     23 type _ = reflect.Value
     24 type _ = Value
     25 
     26 type (
     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.
     38 func (T0) m1()  {} // GCCGO_ERROR "previous"
     39 func (*T0) m1() {} // ERROR "method redeclared: T0\.m1|redefinition of .m1."
     40 func (A0) m1()  {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
     41 func (A0) m1()  {} // ERROR "T0\.m1 redeclared in this block|redefinition of .m1."
     42 func (A0) m2()  {}
     43 
     44 // Type aliases and the original type name can be used interchangeably.
     45 var _ A0 = T0{}
     46 var _ T0 = A0{}
     47 
     48 // But aliases and original types cannot be used with new types based on them.
     49 var _ N0 = T0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
     50 var _ N0 = A0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
     51 
     52 var _ A5 = Value{}
     53 
     54 var _ interface {
     55 	m1()
     56 	m2()
     57 } = T0{}
     58 
     59 var _ interface {
     60 	m1()
     61 	m2()
     62 } = A0{}
     63 
     64 func _() {
     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 literal \(type T0\) as type N0 in assignment|incompatible type"
     86 	var _ N0 = A0{} // ERROR "cannot use T0 literal \(type T0\) as type N0 in assignment|incompatible type"
     87 
     88 	var _ A5 = Value{} // ERROR "cannot use reflect\.Value literal \(type reflect.Value\) as type A5 in assignment|incompatible type"
     89 }
     90 
     91 // Invalid type alias declarations.
     92 
     93 type _ = reflect.ValueOf // ERROR "reflect.ValueOf is not a type|expected type"
     94 
     95 func (A1) m() {} // ERROR "cannot define new methods on non-local type int|may not define methods on non-local type"
     96 func (A2) m() {} // ERROR "invalid receiver type"
     97 func (A3) m() {} // ERROR "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
     98 func (A4) m() {} // ERROR "reflect.Value.m redeclared in this block" "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
     99 
    100 type B1 = struct{}
    101 
    102 func (B1) m() {} // ERROR "m redeclared in this block" "invalid receiver type"
    103 
    104 // TODO(gri) expand
    105