2016-12-16 17:28:30 -07: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 12:24:35 -07:00
type T0 struct { }
2016-12-16 17:28:30 -07:00
// Valid type alias declarations.
2017-01-11 12:24:35 -07:00
type _ = T0
type _ = int
type _ = struct { }
type _ = reflect . Value
type _ = Value
2016-12-16 17:28:30 -07:00
type (
2017-01-11 12:24:35 -07:00
A0 = T0
A1 = int
A2 = struct { }
A3 = reflect . Value
A4 = Value
A5 = Value
N0 A0
2016-12-16 17:28:30 -07:00
)
2017-01-11 12:24:35 -07:00
// Methods can be declared on the original named type and the alias.
2017-01-24 13:43:52 -07:00
func ( T0 ) m1 ( ) { } // GCCGO_ERROR "previous"
2020-11-30 22:38:49 -07:00
func ( * T0 ) m1 ( ) { } // ERROR "method redeclared: T0\.m1|T0\.m1 redeclared in this block|redefinition of .m1."
2017-01-23 15:24:24 -07: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-13 18:23:01 -07:00
func ( A0 ) m2 ( ) { }
2017-01-11 12:24:35 -07: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.
2021-11-10 09:41:21 -07:00
var _ N0 = T0 { } // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use T0{} \(value of type T0\) as type N0 in variable declaration"
var _ N0 = A0 { } // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use A0{} \(value of type T0\) as type N0 in variable declaration"
2017-01-11 12:24:35 -07:00
var _ A5 = Value { }
var _ interface {
m1 ( )
m2 ( )
} = T0 { }
var _ interface {
m1 ( )
m2 ( )
} = A0 { }
2016-12-16 17:28:30 -07:00
func _ ( ) {
2017-01-11 12:24:35 -07:00
type _ = T0
type _ = int
type _ = struct { }
type _ = reflect . Value
type _ = Value
2016-12-16 17:28:30 -07:00
type (
2017-01-11 12:24:35 -07:00
A0 = T0
A1 = int
A2 = struct { }
A3 = reflect . Value
A4 = Value
A5 Value
N0 A0
2016-12-16 17:28:30 -07:00
)
2017-01-11 12:24:35 -07:00
var _ A0 = T0 { }
var _ T0 = A0 { }
2021-11-10 09:41:21 -07:00
var _ N0 = T0 { } // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use T0{} \(value of type T0\) as type N0 in variable declaration"
var _ N0 = A0 { } // ERROR "cannot use T0{} \(type T0\) as type N0 in assignment|cannot use A0{} \(value of type T0\) as type N0 in variable declaration"
2017-01-11 12:24:35 -07:00
2021-11-10 09:41:21 -07:00
var _ A5 = Value { } // ERROR "cannot use reflect\.Value{} \(type reflect.Value\) as type A5 in assignment|cannot use Value{} \(value of type reflect.Value\) as type A5 in variable declaration"
2016-12-16 17:28:30 -07:00
}
// Invalid type alias declarations.
2020-11-30 22:38:49 -07:00
type _ = reflect . ValueOf // ERROR "reflect.ValueOf .*is not a type|expected type"
2017-01-11 12:24:35 -07:00
2017-01-24 13:43:52 -07: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-04 19:42:39 -06: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 12:24:35 -07:00
type B1 = struct { }
2016-12-16 17:28:30 -07:00
2018-04-04 19:42:39 -06:00
func ( B1 ) m ( ) { } // ERROR "invalid receiver type"
2016-12-16 17:28:30 -07:00
// TODO(gri) expand