]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typealias] go/ast, go/parser, go/printer, go/types: initial type alias support
authorRobert Griesemer <gri@golang.org>
Fri, 16 Dec 2016 23:10:07 +0000 (15:10 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 9 Jan 2017 23:43:12 +0000 (23:43 +0000)
Parsing and printing support for type aliases complete.
go/types recognizes them an issues an "unimplemented" error for now.

For #18130.

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

src/go/ast/ast.go
src/go/parser/parser.go
src/go/parser/short_test.go
src/go/printer/nodes.go
src/go/printer/testdata/declarations.golden
src/go/printer/testdata/declarations.input
src/go/types/decl.go
src/go/types/resolver.go
src/go/types/testdata/decls0.src

index a197b5a5bfcdb9e253a9b906b9370f44acbe2cc5..2ecc48b741fb174e941d46a9159db35b9cfdb5ef 100644 (file)
@@ -848,6 +848,7 @@ type (
        TypeSpec struct {
                Doc     *CommentGroup // associated documentation; or nil
                Name    *Ident        // type name
+               Assign  token.Pos     // position of '=', if any
                Type    Expr          // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
                Comment *CommentGroup // line comments; or nil
        }
index d3ef7db31eafbfe3c0f35007ce92e536bfebc8be..40c4a3e58d9cd0dce6b2ab781f6c424a70caa74b 100644 (file)
@@ -2327,7 +2327,10 @@ func (p *parser) parseTypeSpec(doc *ast.CommentGroup, _ token.Token, _ int) ast.
        // (Global identifiers are resolved in a separate phase after parsing.)
        spec := &ast.TypeSpec{Doc: doc, Name: ident}
        p.declare(spec, nil, p.topScope, ast.Typ, ident)
-
+       if p.tok == token.ASSIGN {
+               spec.Assign = p.pos
+               p.next()
+       }
        spec.Type = p.parseType()
        p.expectSemi() // call before accessing p.linecomment
        spec.Comment = p.lineComment
index cdd343ea3c1fd6df8c67772c24eaba6e8544725c..6f8ef6b0f775d98c918510fd4c878f28a11f64cb 100644 (file)
@@ -46,6 +46,8 @@ var valids = []string{
        `package p; const (x = 0; y; z)`, // issue 9639
        `package p; var _ = map[P]int{P{}:0, {}:1}`,
        `package p; var _ = map[*P]int{&P{}:0, {}:1}`,
+       `package p; type T = int`,
+       `package p; type (T = p.T; _ = struct{}; x = *T)`,
 }
 
 func TestValid(t *testing.T) {
index 11f26d45ea39429afb6eed2f4ee03588812dbaf5..5a408cd5710e580419569776f9043e9d0ea1bd93 100644 (file)
@@ -1445,6 +1445,9 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
                } else {
                        p.print(vtab)
                }
+               if s.Assign.IsValid() {
+                       p.print(token.ASSIGN, blank)
+               }
                p.expr(s.Type)
                p.setComment(s.Comment)
 
index 82f5e0f9147be69128d1a0183bfb058d65443b55..d4ea545658cee9a633baab8b00b9b4b2cb1c88d7 100644 (file)
@@ -985,3 +985,18 @@ func _(struct {
        x       int
        y       int
 })     // no extra comma between } and )
+
+// alias declarations
+
+type c0 struct{}
+type c1 = C
+type c2 = struct{ x int }
+type c3 = p.C
+type (
+       s       struct{}
+       a       = A
+       b       = A
+       c       = foo
+       d       = interface{}
+       ddd     = p.Foo
+)
index a0a3783b846fbed661713caee409bdc41ec1afef..50386eb8d5deb7d84571f735794cb8cfd41a72fe 100644 (file)
@@ -999,3 +999,18 @@ func _(struct {
        x int
        y int
 }) // no extra comma between } and )
+
+// alias declarations
+
+type c0 struct{}
+type c1 = C
+type c2 = struct{ x int}
+type c3 = p.C
+type (
+       s struct{}
+       a = A
+       b = A
+       c = foo
+       d = interface{}
+       ddd = p.Foo
+)
\ No newline at end of file
index dced7a6d6dc9163f0e161576728dba0082e77a06..2472aa3434c3f0bd74e3de1c392eff0663a76437 100644 (file)
@@ -534,6 +534,9 @@ func (check *Checker) declStmt(decl ast.Decl) {
                                }
 
                        case *ast.TypeSpec:
+                               if s.Assign.IsValid() {
+                                       check.errorf(s.Assign, "type alias declarations not yet implemented")
+                               }
                                obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
                                // spec: "The scope of a type identifier declared inside a function
                                // begins at the identifier in the TypeSpec and ends at the end of
index 046e147456da875782f79307eb3947e596f1d7f4..d37f93de4512b238e1096aa73ef8015bb4a5d582 100644 (file)
@@ -346,6 +346,9 @@ func (check *Checker) collectObjects() {
                                                }
 
                                        case *ast.TypeSpec:
+                                               if s.Assign.IsValid() {
+                                                       check.errorf(s.Assign, "type alias declarations not yet implemented")
+                                               }
                                                obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
                                                check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, typ: s.Type})
 
index d4df386b1322b4765614de8b3eeb66354e10e337..3ed1b976e5d2e2fc998d036059ae8337a8fecf1f 100644 (file)
@@ -208,3 +208,11 @@ func (BlankT) _() {}
 func (BlankT) _(int) {}
 func (BlankT) _() int { return 0 }
 func (BlankT) _(int) int { return 0}
+
+// type alias declarations
+// TODO(gri) complete this
+type (
+       __ = /* ERROR not yet implemented */ int
+       a0 = /* ERROR not yet implemented */ int
+       a1 = /* ERROR not yet implemented */ struct{}
+)