|
4 | 4 |
|
5 | 5 | package aliasdecl
|
6 | 6 |
|
7 |
| -import "math" |
| 7 | +import ( |
| 8 | + "flag" |
| 9 | + "fmt" // use at most once (to test "imported but not used" error) |
| 10 | + "go/build" |
| 11 | + . "go/build" |
| 12 | + "io" |
| 13 | + "math" |
| 14 | + "unsafe" |
| 15 | +) |
8 | 16 |
|
9 |
| -const _ = math.Pi |
10 |
| -const c /* ERROR "cannot handle alias declarations yet" */ => math.Pi |
| 17 | +// helper |
| 18 | +var before struct { |
| 19 | + f int |
| 20 | +} |
| 21 | + |
| 22 | +// aliases must refer to package-qualified identifiers |
| 23 | +type _ => _ /* ERROR "_ is not a package-qualified identifier" */ |
| 24 | +type t1 => _ /* ERROR "_ is not a package-qualified identifier" */ |
| 25 | + |
| 26 | +const _ => iota /* ERROR "iota is not a package-qualified identifier" */ |
| 27 | +type _ => int /* ERROR "int is not a package-qualified identifier" */ |
| 28 | + |
| 29 | +const c => iota /* ERROR "iota is not a package-qualified identifier" */ |
| 30 | +type t2 => int /* ERROR "int is not a package-qualified identifier" */ |
| 31 | + |
| 32 | +// dot-imported identifiers are not qualified identifiers |
| 33 | +// TODO(gri) fix error printing - should not print a qualified identifier... |
| 34 | +var _ => Default /* ERROR "Default is not a package-qualified identifier" */ |
| 35 | + |
| 36 | +// qualified identifiers must start with a package |
| 37 | +var _ => before /* ERROR "before.f is not a package-qualified identifier" */ .f |
| 38 | +func _ => before /* ERROR "before.f is not a package-qualified identifier" */ .f |
| 39 | +var _ => after /* ERROR "after.m is not a package-qualified identifier" */ .m |
| 40 | +func _ => after /* ERROR "after.m is not a package-qualified identifier" */ .m |
| 41 | + |
| 42 | +var v1 => before /* ERROR "before.f is not a package-qualified identifier" */ .f |
| 43 | +func f1 => before /* ERROR "before.f is not a package-qualified identifier" */ .f |
| 44 | +var v2 => after /* ERROR "after.m is not a package-qualified identifier" */ .m |
| 45 | +func f2 => after /* ERROR "after.m is not a package-qualified identifier" */ .m |
| 46 | + |
| 47 | +// TODO(gri) fix error printing - should print correct qualified identifier... |
| 48 | +var _ => Default /* ERROR "Default.ARCH is not a package-qualified identifier" */ .ARCH |
| 49 | +var _ Context // use dot-imported package go/build |
| 50 | + |
| 51 | +// aliases may not refer to package unsafe |
| 52 | +type ptr => unsafe /* ERROR "refers to package unsafe" */ .Pointer |
| 53 | +func size => unsafe /* ERROR "refers to package unsafe" */ .Sizeof |
| 54 | + |
| 55 | +// aliases must refer to entities of the same kind |
| 56 | +const _ => math.Pi |
| 57 | +const pi => math.Pi |
| 58 | +const pi1 => math /* ERROR "math.Sin.* is not a constant" */ .Sin |
| 59 | + |
| 60 | +type _ => io.Writer |
| 61 | +type writer => io.Writer |
| 62 | +type writer1 => math /* ERROR "math.Sin.* is not a type" */ .Sin |
| 63 | + |
| 64 | +var _ => build.Default |
| 65 | +var def => build.Default |
| 66 | +var def1 => build /* ERROR "build.Import.* is not a variable" */ .Import |
| 67 | + |
| 68 | +func _ => math.Sin |
| 69 | +func sin => math.Sin |
| 70 | +func sin1 => math /* ERROR "math.Pi.* is not a function" */ .Pi |
| 71 | + |
| 72 | +// using an incorrectly declared alias should not lead to more errors |
| 73 | +const _ = pi1 |
| 74 | +type _ writer1 |
| 75 | +var _ def1 = 0 |
| 76 | +var _ = sin1 |
| 77 | + |
| 78 | +// aliases may not be called init |
| 79 | +func init /* ERROR "cannot declare init" */ => flag.Parse |
| 80 | +func _ => flag.Parse // use package flag |
| 81 | + |
| 82 | +// alias reference to a package marks package as used |
| 83 | +func _ => fmt.Println |
| 84 | + |
| 85 | +// re-exported aliases |
| 86 | +const Pi => math.Pi |
| 87 | + |
| 88 | +type Writer => io.Writer |
| 89 | + |
| 90 | +var Def => build.Default |
| 91 | + |
| 92 | +func Sin => math.Sin |
| 93 | + |
| 94 | +// const aliases may appear in "iota" context |
| 95 | +// (this verifies a type-checker internal assertion) |
| 96 | +const ( |
| 97 | + _ = iota |
| 98 | + pi2 => math.Pi |
| 99 | +) |
| 100 | + |
| 101 | +// type aliases denote identical types |
| 102 | +type myPackage => build.Package |
| 103 | + |
| 104 | +var pkg myPackage |
| 105 | +var _ build.Package = pkg // valid assignment |
| 106 | +var _ *build.Package = &pkg // valid assignment |
| 107 | + |
| 108 | +// helper |
| 109 | +type after struct{} |
| 110 | + |
| 111 | +func (after) m() {} |
0 commit comments