Skip to content

Commit 3e11940

Browse files
committed
[dev.typealias] cmd/compile: recognize type aliases but complain for now (not yet supported)
Added test file. For #18130. Change-Id: Ifcfd7cd1acf9dd6a2f4f3d85979d232bb6b8c6b1 Reviewed-on: https://go-review.googlesource.com/34988 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent e0a05c2 commit 3e11940

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

src/cmd/compile/internal/gc/noder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ func (p *noder) constDecl(decl *syntax.ConstDecl) []*Node {
185185
}
186186

187187
func (p *noder) typeDecl(decl *syntax.TypeDecl) *Node {
188+
if decl.Alias {
189+
yyerror("type alias declarations unimplemented")
190+
}
191+
188192
name := typedcl0(p.name(decl.Name))
189193
name.Name.Param.Pragma = Pragma(decl.Pragma)
190194

src/cmd/compile/internal/syntax/nodes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type (
7474
// Name Type
7575
TypeDecl struct {
7676
Name *Name
77+
Alias bool
7778
Type Expr
7879
Group *Group // nil means not part of a group
7980
Pragma Pragma

src/cmd/compile/internal/syntax/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (p *parser) constDecl(group *Group) Decl {
325325
return d
326326
}
327327

328-
// TypeSpec = identifier Type .
328+
// TypeSpec = identifier [ "=" ] Type .
329329
func (p *parser) typeDecl(group *Group) Decl {
330330
if trace {
331331
defer p.trace("typeDecl")()
@@ -335,6 +335,7 @@ func (p *parser) typeDecl(group *Group) Decl {
335335
d.init(p)
336336

337337
d.Name = p.name()
338+
d.Alias = p.got(_Assign)
338339
d.Type = p.tryType()
339340
if d.Type == nil {
340341
p.syntax_error("in type declaration")

src/cmd/compile/internal/syntax/printer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,11 @@ func (p *printer) printRawNode(n Node) {
619619
if n.Group == nil {
620620
p.print(_Type, blank)
621621
}
622-
p.print(n.Name, blank, n.Type)
622+
p.print(n.Name, blank)
623+
if n.Alias {
624+
p.print(_Assign, blank)
625+
}
626+
p.print(n.Type)
623627

624628
case *VarDecl:
625629
if n.Group == nil {

src/cmd/compile/internal/syntax/printer_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ func TestPrint(t *testing.T) {
2222
Fprint(os.Stdout, ast, true)
2323
fmt.Println()
2424
}
25+
26+
func TestPrintString(t *testing.T) {
27+
for _, want := range []string{
28+
"package p",
29+
"package p; type _ = int; type T1 = struct{}; type ( _ = *struct{}; T2 = float32 )",
30+
// TODO(gri) expand
31+
} {
32+
ast, err := ParseBytes([]byte(want), nil, nil, 0)
33+
if err != nil {
34+
t.Error(err)
35+
continue
36+
}
37+
if got := String(ast); got != want {
38+
t.Errorf("%q: got %q", want, got)
39+
}
40+
}
41+
}

test/alias2.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// errorcheck
2+
3+
// Copyright 2016 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Test basic restrictions on type aliases.
8+
9+
// The compiler doesn't implement type aliases yet,
10+
// so for now we get the same error (unimplemented)
11+
// everywhere, OR-ed into the ERROR checks.
12+
// TODO(gri) remove the need for "unimplemented"
13+
14+
package p
15+
16+
import (
17+
"reflect"
18+
. "reflect"
19+
)
20+
21+
// Valid type alias declarations.
22+
23+
type _ = int // ERROR "unimplemented"
24+
type _ = struct{} // ERROR "unimplemented"
25+
type _ = reflect.Value // ERROR "unimplemented"
26+
type _ = Value // ERROR "unimplemented"
27+
28+
type (
29+
a1 = int // ERROR "unimplemented"
30+
a2 = struct{} // ERROR "unimplemented"
31+
a3 = reflect.Value // ERROR "unimplemented"
32+
a4 = Value // ERROR "unimplemented"
33+
)
34+
35+
func _() {
36+
type _ = int // ERROR "unimplemented"
37+
type _ = struct{} // ERROR "unimplemented"
38+
type _ = reflect.Value // ERROR "unimplemented"
39+
type _ = Value // ERROR "unimplemented"
40+
41+
type (
42+
a1 = int // ERROR "unimplemented"
43+
a2 = struct{} // ERROR "unimplemented"
44+
a3 = reflect.Value // ERROR "unimplemented"
45+
a4 = Value // ERROR "unimplemented"
46+
)
47+
}
48+
49+
// Invalid type alias declarations.
50+
51+
type _ = reflect.ValueOf // ERROR "reflect.ValueOf is not a type|unimplemented"
52+
53+
type b1 = struct{} // ERROR "unimplemented"
54+
func (b1) m() {} // disabled ERROR "invalid receiver type"
55+
56+
// TODO(gri) expand
57+
// It appears that type-checking exits after some more severe errors, so we may
58+
// need more test files.

0 commit comments

Comments
 (0)