aboutsummaryrefslogtreecommitdiff
path: root/test/alias2.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2016-10-19 15:32:14 -0700
committerRobert Griesemer <gri@golang.org>2016-10-25 00:54:09 +0000
commit1b0cf430dd130ad53e9f43bc04d2ed91bcd87b26 (patch)
treedc5dfeefdfcf0179aba30934460c437a958515a4 /test/alias2.go
parent7e9f420ddfb1ce8882bb715158cdb8b977b93955 (diff)
downloadgo-1b0cf430dd130ad53e9f43bc04d2ed91bcd87b26.tar.gz
go-1b0cf430dd130ad53e9f43bc04d2ed91bcd87b26.zip
cmd/compile: implement package-level aliases (no export yet)
Requires -newparser=1. For #17487. For #16339. Change-Id: I156fb0c0f8a97e8c72dbbfbd7fe821efee12b957 Reviewed-on: https://go-review.googlesource.com/31597 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'test/alias2.go')
-rw-r--r--test/alias2.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/test/alias2.go b/test/alias2.go
new file mode 100644
index 0000000000..b73f81c014
--- /dev/null
+++ b/test/alias2.go
@@ -0,0 +1,96 @@
+// errorcheck -newparser=1
+
+// 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.
+
+package p
+
+import (
+ "fmt" // use at most once (to test "imported but not used" error)
+ "go/build"
+ . "go/build"
+ "io"
+ "math"
+ "unsafe"
+)
+
+// 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"
+
+// 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"
+
+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"
+
+// TODO(gri) fix error printing - should not print a qualified identifier...
+var _ => Default.ARCH // ERROR "build.Default is not a package"
+
+// 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"
+
+func _ => math.Sin
+func sin => math.Sin
+func sin1 => math.Pi // ERROR "math.Pi is not a function"
+
+// alias reference to a package marks package as used
+func _ => fmt.Println
+
+// 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 aliases denote identical types
+type myPackage => build.Package
+
+var pkg myPackage
+var _ build.Package = pkg // valid assignment
+var _ *build.Package = &pkg // valid assignment
+
+// helper
+type after struct{}
+
+func (after) m() {}