]> Cypherpunks.ru repositories - gostls13.git/blob - test/alias2.go
cmd/compile: disallow "init" as alias
[gostls13.git] / test / alias2.go
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 alias declarations.
8
9 package p
10
11 import (
12         "flag"
13         "fmt" // use at most once (to test "imported but not used" error)
14         "go/build"
15         . "go/build"
16         "io"
17         "math"
18         "unsafe"
19 )
20
21 // helper
22 var before struct {
23         f
24 }
25
26 // aliases must refer to package-qualified identifiers
27 // TODO(gri) should only see one error for declaration below - fix this
28 const _ => 0 // ERROR "unexpected literal 0|_ is not a package-qualified identifier"
29
30 type _ => _ // ERROR "_ is not a package-qualified identifier"
31 type t => _ // ERROR "_ is not a package-qualified identifier"
32
33 const _ => iota // ERROR "iota is not a package-qualified identifier"
34 type _ => int   // ERROR "int is not a package-qualified identifier"
35
36 const c => iota // ERROR "iota is not a package-qualified identifier"
37 type t => int   // ERROR "int is not a package-qualified identifier"
38
39 // dot-imported identifiers are not qualified identifiers
40 // TODO(gri) fix error printing - should not print a qualified identifier...
41 var _ => Default // ERROR "build\.Default is not a package-qualified identifier"
42
43 // qualified identifiers must start with a package
44 var _ => before.f  // ERROR "before is not a package"
45 func _ => before.f // ERROR "before is not a package"
46 var _ => after.m   // ERROR "after is not a package"
47 func _ => after.m  // ERROR "after is not a package"
48
49 var v => before.f  // ERROR "before is not a package"
50 func f => before.f // ERROR "before is not a package"
51 var v => after.m   // ERROR "after is not a package"
52 func f => after.m  // ERROR "after is not a package"
53
54 // TODO(gri) fix error printing - should print correct qualified identifier...
55 var _ => Default.ARCH // ERROR "build.Default is not a package"
56
57 // aliases may not refer to package unsafe
58 type ptr => unsafe.Pointer // ERROR "ptr refers to package unsafe"
59 func size => unsafe.Sizeof // ERROR "size refers to package unsafe"
60
61 // aliases must refer to entities of the same kind
62 const _ => math.Pi
63 const pi => math.Pi
64 const pi1 => math.Sin // ERROR "math.Sin is not a constant"
65
66 type _ => io.Writer
67 type writer => io.Writer
68 type writer1 => math.Sin // ERROR "math.Sin is not a type"
69
70 var _ => build.Default
71 var def => build.Default
72 var def1 => build.Import // ERROR "build.Import is not a variable"
73
74 func _ => math.Sin
75 func sin => math.Sin
76 func sin1 => math.Pi // ERROR "math.Pi is not a function"
77
78 // aliases may not be called init
79 func init => flag.Parse // ERROR "cannot declare init"
80
81 // alias reference to a package marks package as used
82 func _ => fmt.Println
83
84 // re-exported aliases
85 const Pi => math.Pi
86
87 type Writer => io.Writer
88
89 var Def => build.Default
90
91 func Sin => math.Sin
92
93 // type aliases denote identical types
94 type myPackage => build.Package
95
96 var pkg myPackage
97 var _ build.Package = pkg   // valid assignment
98 var _ *build.Package = &pkg // valid assignment
99
100 // helper
101 type after struct{}
102
103 func (after) m() {}