]> Cypherpunks.ru repositories - gostls13.git/blobdiff - test/alias2.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / alias2.go
index b73f81c014f7159815d1c875e66c9f5113a5f601..2846e5dc31d21e8e455327eb789f9c202eb20053 100644 (file)
-// errorcheck -newparser=1
+// 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 alias declarations.
+// Test basic restrictions on type aliases.
 
 package p
 
 import (
-       "fmt" // use at most once (to test "imported but not used" error)
-       "go/build"
-       . "go/build"
-       "io"
-       "math"
-       "unsafe"
+       "reflect"
+       . "reflect"
 )
 
-// helper
-var before struct {
-       f
-}
-
-// aliases must refer to package-qualified identifiers
-// TODO(gri) should only see one error for declaration below - fix this
-const _ => 0 // ERROR "unexpected literal 0|_ is not a package-qualified identifier"
-
-type _ => _ // ERROR "_ is not a package-qualified identifier"
-type t => _ // ERROR "_ is not a package-qualified identifier"
-
-const _ => iota // ERROR "iota is not a package-qualified identifier"
-type _ => int   // ERROR "int is not a package-qualified identifier"
-
-const c => iota // ERROR "iota is not a package-qualified identifier"
-type t => int   // ERROR "int is not a package-qualified identifier"
-
-// dot-imported identifiers are not qualified identifiers
-// TODO(gri) fix error printing - should not print a qualified identifier...
-var _ => Default // ERROR "build\.Default is not a package-qualified identifier"
+type T0 struct{}
 
-// qualified identifiers must start with a package
-var _ => before.f  // ERROR "before is not a package"
-func _ => before.f // ERROR "before is not a package"
-var _ => after.m   // ERROR "after is not a package"
-func _ => after.m  // ERROR "after is not a package"
+// Valid type alias declarations.
 
-var v => before.f  // ERROR "before is not a package"
-func f => before.f // ERROR "before is not a package"
-var v => after.m   // ERROR "after is not a package"
-func f => after.m  // ERROR "after is not a package"
+type _ = T0
+type _ = int
+type _ = struct{}
+type _ = reflect.Value
+type _ = Value
 
-// TODO(gri) fix error printing - should not print a qualified identifier...
-var _ => Default.ARCH // ERROR "build.Default is not a package"
+type (
+       A0 = T0
+       A1 = int
+       A2 = struct{}
+       A3 = reflect.Value
+       A4 = Value
+       A5 = Value
 
-// aliases may not refer to package unsafe
-type ptr => unsafe.Pointer // ERROR "ptr refers to package unsafe"
-func size => unsafe.Sizeof // ERROR "size refers to package unsafe"
-
-// aliases must refer to entities of the same kind
-const _ => math.Pi
-const pi => math.Pi
-const pi1 => math.Sin // ERROR "math.Sin is not a constant"
-
-type _ => io.Writer
-type writer => io.Writer
-type writer1 => math.Sin // ERROR "math.Sin is not a type"
-
-var _ => build.Default
-var def => build.Default
-var def1 => build.Import // ERROR "build.Import is not a variable"
+       N0 A0
+)
 
-func _ => math.Sin
-func sin => math.Sin
-func sin1 => math.Pi // ERROR "math.Pi is not a function"
+// Methods can be declared on the original named type and the alias.
+func (T0) m1()  {} // GCCGO_ERROR "previous"
+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."
+func (A0) m2()  {}
+
+// 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.
+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"
+
+var _ A5 = Value{}
+
+var _ interface {
+       m1()
+       m2()
+} = T0{}
+
+var _ interface {
+       m1()
+       m2()
+} = A0{}
+
+func _() {
+       type _ = T0
+       type _ = int
+       type _ = struct{}
+       type _ = reflect.Value
+       type _ = Value
+
+       type (
+               A0 = T0
+               A1 = int
+               A2 = struct{}
+               A3 = reflect.Value
+               A4 = Value
+               A5 Value
+
+               N0 A0
+       )
+
+       var _ A0 = T0{}
+       var _ T0 = A0{}
+
+       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"
+
+       var _ A5 = Value{} // ERROR "cannot use Value{} \(value of type reflect\.Value\) as A5 value in variable declaration"
+}
 
-// alias reference to a package marks package as used
-func _ => fmt.Println
+// Invalid type alias declarations.
 
-// TODO(gri) aliased cannot be exported yet - fix this
-const Pi => math.Pi      // ERROR "cannot export alias Pi"
-type Writer => io.Writer // ERROR "cannot export alias Writer"
-var Def => build.Default // ERROR "cannot export alias Def"
-func Sin => math.Sin     // ERROR "cannot export alias Sin"
+type _ = reflect.ValueOf // ERROR "reflect.ValueOf .*is not a type|expected type"
 
-// type aliases denote identical types
-type myPackage => build.Package
+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"
+func (A4) m() {} // ERROR "cannot define new methods on non-local type reflect.Value|may not define methods on non-local type"
 
-var pkg myPackage
-var _ build.Package = pkg   // valid assignment
-var _ *build.Package = &pkg // valid assignment
+type B1 = struct{}
 
-// helper
-type after struct{}
+func (B1) m() {} // ERROR "invalid receiver type"
 
-func (after) m() {}
+// TODO(gri) expand