]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[dev.typealias] cmd/compile: recognize type aliases but complain for now (not yet...
authorRobert Griesemer <gri@golang.org>
Sat, 17 Dec 2016 00:28:30 +0000 (16:28 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 10 Jan 2017 00:10:11 +0000 (00:10 +0000)
Added test file.

For #18130.

Change-Id: Ifcfd7cd1acf9dd6a2f4f3d85979d232bb6b8c6b1
Reviewed-on: https://go-review.googlesource.com/34988
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/noder.go
src/cmd/compile/internal/syntax/nodes.go
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/printer.go
src/cmd/compile/internal/syntax/printer_test.go
test/alias2.go [new file with mode: 0644]

index a501cb67b6f7fc4111d8559492b2008786d597d3..3f6fe20b6b8bb9e11b626988c86a0d88a17716f3 100644 (file)
@@ -185,6 +185,10 @@ func (p *noder) constDecl(decl *syntax.ConstDecl) []*Node {
 }
 
 func (p *noder) typeDecl(decl *syntax.TypeDecl) *Node {
+       if decl.Alias {
+               yyerror("type alias declarations unimplemented")
+       }
+
        name := typedcl0(p.name(decl.Name))
        name.Name.Param.Pragma = Pragma(decl.Pragma)
 
index fadba84bce2a69c17c82f7edb4107fbd7b5cac2f..34524e5c09dade5313c9d8731c6a74202871eee3 100644 (file)
@@ -74,6 +74,7 @@ type (
        // Name Type
        TypeDecl struct {
                Name   *Name
+               Alias  bool
                Type   Expr
                Group  *Group // nil means not part of a group
                Pragma Pragma
index 121dfb75e5fe4946d9b666c8695c63344d155006..1185507238040dc48ca710541f29d77c37402a46 100644 (file)
@@ -325,7 +325,7 @@ func (p *parser) constDecl(group *Group) Decl {
        return d
 }
 
-// TypeSpec = identifier Type .
+// TypeSpec = identifier [ "=" ] Type .
 func (p *parser) typeDecl(group *Group) Decl {
        if trace {
                defer p.trace("typeDecl")()
@@ -335,6 +335,7 @@ func (p *parser) typeDecl(group *Group) Decl {
        d.init(p)
 
        d.Name = p.name()
+       d.Alias = p.got(_Assign)
        d.Type = p.tryType()
        if d.Type == nil {
                p.syntax_error("in type declaration")
index 0cacf1e5d496015cade1116ba0b50918ab2cf868..43876a25c20f098afbc7e36103c6e974e94d5ae3 100644 (file)
@@ -619,7 +619,11 @@ func (p *printer) printRawNode(n Node) {
                if n.Group == nil {
                        p.print(_Type, blank)
                }
-               p.print(n.Name, blank, n.Type)
+               p.print(n.Name, blank)
+               if n.Alias {
+                       p.print(_Assign, blank)
+               }
+               p.print(n.Type)
 
        case *VarDecl:
                if n.Group == nil {
index 5c0fc776a1c85df200760655212b440c8e1cb1b4..a9969e610a1d6dc86c5f0407af80cdb1efe506b8 100644 (file)
@@ -22,3 +22,20 @@ func TestPrint(t *testing.T) {
        Fprint(os.Stdout, ast, true)
        fmt.Println()
 }
+
+func TestPrintString(t *testing.T) {
+       for _, want := range []string{
+               "package p",
+               "package p; type _ = int; type T1 = struct{}; type ( _ = *struct{}; T2 = float32 )",
+               // TODO(gri) expand
+       } {
+               ast, err := ParseBytes([]byte(want), nil, nil, 0)
+               if err != nil {
+                       t.Error(err)
+                       continue
+               }
+               if got := String(ast); got != want {
+                       t.Errorf("%q: got %q", want, got)
+               }
+       }
+}
diff --git a/test/alias2.go b/test/alias2.go
new file mode 100644 (file)
index 0000000..25df7c2
--- /dev/null
@@ -0,0 +1,58 @@
+// 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.
+
+// The compiler doesn't implement type aliases yet,
+// so for now we get the same error (unimplemented)
+// everywhere, OR-ed into the ERROR checks.
+// TODO(gri) remove the need for "unimplemented"
+
+package p
+
+import (
+       "reflect"
+       . "reflect"
+)
+
+// Valid type alias declarations.
+
+type _ = int           // ERROR "unimplemented"
+type _ = struct{}      // ERROR "unimplemented"
+type _ = reflect.Value // ERROR "unimplemented"
+type _ = Value         // ERROR "unimplemented"
+
+type (
+       a1 = int           // ERROR "unimplemented"
+       a2 = struct{}      // ERROR "unimplemented"
+       a3 = reflect.Value // ERROR "unimplemented"
+       a4 = Value         // ERROR "unimplemented"
+)
+
+func _() {
+       type _ = int           // ERROR "unimplemented"
+       type _ = struct{}      // ERROR "unimplemented"
+       type _ = reflect.Value // ERROR "unimplemented"
+       type _ = Value         // ERROR "unimplemented"
+
+       type (
+               a1 = int           // ERROR "unimplemented"
+               a2 = struct{}      // ERROR "unimplemented"
+               a3 = reflect.Value // ERROR "unimplemented"
+               a4 = Value         // ERROR "unimplemented"
+       )
+}
+
+// Invalid type alias declarations.
+
+type _ = reflect.ValueOf // ERROR "reflect.ValueOf is not a type|unimplemented"
+
+type b1 = struct{} // ERROR "unimplemented"
+func (b1) m()      {} // disabled ERROR "invalid receiver type"
+
+// TODO(gri) expand
+// It appears that type-checking exits after some more severe errors, so we may
+// need more test files.