Skip to content

Commit

Permalink
go/types: revert user-visible changes related to aliases
Browse files Browse the repository at this point in the history
Reason: Decision to back out current alias implementation.
For #16339 (comment).

Change-Id: Ie04f24e529db2d29c5dd2e36413f5f37f628df39
Reviewed-on: https://go-review.googlesource.com/32819
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
  • Loading branch information
griesemer committed Nov 4, 2016
1 parent 2c6949e commit 039e60c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 38 deletions.
6 changes: 5 additions & 1 deletion src/go/internal/gcimporter/bimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,11 @@ func (p *importer) obj(tag int) {
if pkg, name := p.qualifiedName(); pkg != nil {
orig = pkg.Scope().Lookup(name)
}
p.declare(types.NewAlias(pos, p.pkgList[0], name, orig))
// Alias-related code. Keep for now.
_ = pos
_ = name
_ = orig
// p.declare(types.NewAlias(pos, p.pkgList[0], name, orig))

default:
errorf("unexpected object tag %d", tag)
Expand Down
6 changes: 3 additions & 3 deletions src/go/types/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
"go/parser"
"go/token"
"internal/testenv"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -1299,6 +1296,8 @@ func f(x int) { y := x; print(y) }
}
}

// Alias-related code. Keep for now.
/*
func TestAliases(t *testing.T) {
testenv.MustHaveGoBuild(t)
Expand Down Expand Up @@ -1447,3 +1446,4 @@ var _ = Implements(nil, nil)
t.Errorf("missing aliases: %v", defs)
}
}
*/
2 changes: 1 addition & 1 deletion src/go/types/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var tests = [][]string{
{"testdata/const1.src"},
{"testdata/constdecl.src"},
{"testdata/vardecl.src"},
{"testdata/aliasdecl.src"},
//{"testdata/aliasdecl.src"},
{"testdata/expr0.src"},
{"testdata/expr1.src"},
{"testdata/expr2.src"},
Expand Down
13 changes: 7 additions & 6 deletions src/go/types/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ func (check *Checker) objDecl(obj Object, def *Named, path []*TypeName) {
case *Func:
// functions may be recursive - no need to track dependencies
check.funcDecl(obj, d)
case *Alias:
// aliases cannot be recursive - no need to track dependencies
check.aliasDecl(obj, d)
// Alias-related code. Keep for now.
// case *Alias:
// // aliases cannot be recursive - no need to track dependencies
// check.aliasDecl(obj, d)
default:
unreachable()
}
Expand Down Expand Up @@ -337,17 +338,17 @@ func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
// but it may be nil.
func original(obj Object) Object {
// an alias stands for the original object; use that one instead
if alias, _ := obj.(*Alias); alias != nil {
if alias, _ := obj.(*disabledAlias); alias != nil {
obj = alias.orig
// aliases always refer to non-alias originals
if _, ok := obj.(*Alias); ok {
if _, ok := obj.(*disabledAlias); ok {
panic("original is an alias")
}
}
return obj
}

func (check *Checker) aliasDecl(obj *Alias, decl *declInfo) {
func (check *Checker) aliasDecl(obj *disabledAlias, decl *declInfo) {
assert(obj.typ == nil)

// alias declarations cannot use iota
Expand Down
31 changes: 16 additions & 15 deletions src/go/types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,26 @@ func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope }
func (*Func) isDependency() {} // a function may be a dependency of an initialization expression

// An Alias represents a declared alias.
type Alias struct {
type disabledAlias struct {
object
orig Object // aliased constant, type, variable, or function; never an alias
kind token.Token // token.CONST, token.TYPE, token.VAR, or token.FUNC (only needed during resolve phase)
}

func NewAlias(pos token.Pos, pkg *Package, name string, orig Object) *Alias {
func disabledNewAlias(pos token.Pos, pkg *Package, name string, orig Object) *disabledAlias {
var typ Type = Typ[Invalid]
if orig != nil {
typ = orig.Type()
}
// No need to set a valid Alias.kind - that field is only used during identifier
// resolution (1st type-checker pass). We could store the field outside but it's
// easier to keep it here.
return &Alias{object{nil, pos, pkg, name, typ, 0, token.NoPos}, orig, token.ILLEGAL}
return &disabledAlias{object{nil, pos, pkg, name, typ, 0, token.NoPos}, orig, token.ILLEGAL}
}

// Orig returns the aliased object, or nil if there was an error.
// The returned object is never an Alias.
func (obj *Alias) Orig() Object { return obj.orig }
func (obj *disabledAlias) disabledOrig() Object { return obj.orig }

// A Label represents a declared label.
type Label struct {
Expand Down Expand Up @@ -295,8 +295,9 @@ func writeObject(buf *bytes.Buffer, obj Object, qf Qualifier) {
}
return

case *Alias:
buf.WriteString("alias")
// Alias-related code. Keep for now.
// case *Alias:
// buf.WriteString("alias")

case *Label:
buf.WriteString("label")
Expand Down Expand Up @@ -352,15 +353,15 @@ func ObjectString(obj Object, qf Qualifier) string {
return buf.String()
}

func (obj *PkgName) String() string { return ObjectString(obj, nil) }
func (obj *Const) String() string { return ObjectString(obj, nil) }
func (obj *TypeName) String() string { return ObjectString(obj, nil) }
func (obj *Var) String() string { return ObjectString(obj, nil) }
func (obj *Func) String() string { return ObjectString(obj, nil) }
func (obj *Alias) String() string { return ObjectString(obj, nil) }
func (obj *Label) String() string { return ObjectString(obj, nil) }
func (obj *Builtin) String() string { return ObjectString(obj, nil) }
func (obj *Nil) String() string { return ObjectString(obj, nil) }
func (obj *PkgName) String() string { return ObjectString(obj, nil) }
func (obj *Const) String() string { return ObjectString(obj, nil) }
func (obj *TypeName) String() string { return ObjectString(obj, nil) }
func (obj *Var) String() string { return ObjectString(obj, nil) }
func (obj *Func) String() string { return ObjectString(obj, nil) }
func (obj *disabledAlias) String() string { return ObjectString(obj, nil) }
func (obj *Label) String() string { return ObjectString(obj, nil) }
func (obj *Builtin) String() string { return ObjectString(obj, nil) }
func (obj *Nil) String() string { return ObjectString(obj, nil) }

func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) {
if f.typ != nil {
Expand Down
11 changes: 6 additions & 5 deletions src/go/types/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,12 @@ func (check *Checker) collectObjects() {
check.declare(fileScope, nil, obj, token.NoPos)
}

case *ast.AliasSpec:
obj := NewAlias(s.Name.Pos(), pkg, s.Name.Name, nil)
obj.typ = nil // unresolved
obj.kind = d.Tok
check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, init: s.Orig})
// Alias-related code. Keep for now.
// case *ast.AliasSpec:
// obj := NewAlias(s.Name.Pos(), pkg, s.Name.Name, nil)
// obj.typ = nil // unresolved
// obj.kind = d.Tok
// check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, init: s.Orig})

case *ast.ValueSpec:
switch d.Tok {
Expand Down
15 changes: 8 additions & 7 deletions src/go/types/typexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, path []*TypeNa
delete(check.unusedDotImports[scope], pkg)
}

// Alias-related code. Keep for now.
// An alias stands for the original object; use that one instead.
// TODO(gri) We should be able to factor out the Typ[Invalid] test.
if alias, _ := obj.(*Alias); alias != nil {
obj = original(obj)
if obj == nil || typ == Typ[Invalid] {
return
}
assert(typ == obj.Type())
}
// if alias, _ := obj.(*Alias); alias != nil {
// obj = original(obj)
// if obj == nil || typ == Typ[Invalid] {
// return
// }
// assert(typ == obj.Type())
// }

switch obj := obj.(type) {
case *PkgName:
Expand Down

0 comments on commit 039e60c

Please sign in to comment.