Skip to content

Commit 2e5116b

Browse files
committed
[dev.typealias] go/ast, go/parser, go/printer, go/types: initial type alias support
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 <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent ffedff7 commit 2e5116b

File tree

9 files changed

+54
-1
lines changed

9 files changed

+54
-1
lines changed

src/go/ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ type (
848848
TypeSpec struct {
849849
Doc *CommentGroup // associated documentation; or nil
850850
Name *Ident // type name
851+
Assign token.Pos // position of '=', if any
851852
Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
852853
Comment *CommentGroup // line comments; or nil
853854
}

src/go/parser/parser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,10 @@ func (p *parser) parseTypeSpec(doc *ast.CommentGroup, _ token.Token, _ int) ast.
23272327
// (Global identifiers are resolved in a separate phase after parsing.)
23282328
spec := &ast.TypeSpec{Doc: doc, Name: ident}
23292329
p.declare(spec, nil, p.topScope, ast.Typ, ident)
2330-
2330+
if p.tok == token.ASSIGN {
2331+
spec.Assign = p.pos
2332+
p.next()
2333+
}
23312334
spec.Type = p.parseType()
23322335
p.expectSemi() // call before accessing p.linecomment
23332336
spec.Comment = p.lineComment

src/go/parser/short_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ var valids = []string{
4646
`package p; const (x = 0; y; z)`, // issue 9639
4747
`package p; var _ = map[P]int{P{}:0, {}:1}`,
4848
`package p; var _ = map[*P]int{&P{}:0, {}:1}`,
49+
`package p; type T = int`,
50+
`package p; type (T = p.T; _ = struct{}; x = *T)`,
4951
}
5052

5153
func TestValid(t *testing.T) {

src/go/printer/nodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,9 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
14451445
} else {
14461446
p.print(vtab)
14471447
}
1448+
if s.Assign.IsValid() {
1449+
p.print(token.ASSIGN, blank)
1450+
}
14481451
p.expr(s.Type)
14491452
p.setComment(s.Comment)
14501453

src/go/printer/testdata/declarations.golden

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,3 +985,18 @@ func _(struct {
985985
x int
986986
y int
987987
}) // no extra comma between } and )
988+
989+
// alias declarations
990+
991+
type c0 struct{}
992+
type c1 = C
993+
type c2 = struct{ x int }
994+
type c3 = p.C
995+
type (
996+
s struct{}
997+
a = A
998+
b = A
999+
c = foo
1000+
d = interface{}
1001+
ddd = p.Foo
1002+
)

src/go/printer/testdata/declarations.input

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,18 @@ func _(struct {
999999
x int
10001000
y int
10011001
}) // no extra comma between } and )
1002+
1003+
// alias declarations
1004+
1005+
type c0 struct{}
1006+
type c1 = C
1007+
type c2 = struct{ x int}
1008+
type c3 = p.C
1009+
type (
1010+
s struct{}
1011+
a = A
1012+
b = A
1013+
c = foo
1014+
d = interface{}
1015+
ddd = p.Foo
1016+
)

src/go/types/decl.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ func (check *Checker) declStmt(decl ast.Decl) {
534534
}
535535

536536
case *ast.TypeSpec:
537+
if s.Assign.IsValid() {
538+
check.errorf(s.Assign, "type alias declarations not yet implemented")
539+
}
537540
obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
538541
// spec: "The scope of a type identifier declared inside a function
539542
// begins at the identifier in the TypeSpec and ends at the end of

src/go/types/resolver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ func (check *Checker) collectObjects() {
346346
}
347347

348348
case *ast.TypeSpec:
349+
if s.Assign.IsValid() {
350+
check.errorf(s.Assign, "type alias declarations not yet implemented")
351+
}
349352
obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
350353
check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, typ: s.Type})
351354

src/go/types/testdata/decls0.src

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,11 @@ func (BlankT) _() {}
208208
func (BlankT) _(int) {}
209209
func (BlankT) _() int { return 0 }
210210
func (BlankT) _(int) int { return 0}
211+
212+
// type alias declarations
213+
// TODO(gri) complete this
214+
type (
215+
__ = /* ERROR not yet implemented */ int
216+
a0 = /* ERROR not yet implemented */ int
217+
a1 = /* ERROR not yet implemented */ struct{}
218+
)

0 commit comments

Comments
 (0)