]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: simplify type alias handling for export
authorMatthew Dempsky <mdempsky@google.com>
Mon, 17 May 2021 18:40:02 +0000 (11:40 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 18 May 2021 21:18:56 +0000 (21:18 +0000)
Currently the exporter uses types.IsDotAlias(n.Sym()) to recognize
that n is a type alias, but IsDotAlias is actually meant for
recognizing aliases introduced by dot imports. Translated to go/types,
the current logic amounts recognizing type aliases as if by:

var n *types.TypeName
typ, ok := n.Pkg().Scope().Lookup(n.Name()).Type().(*types.Named)
isAlias := !ok || typ.Obj().Pkg() != n.Pkg() || typ.Obj().Name() != n.Name()

But we can instead just check n.Alias() (eqv. n.IsAlias() in
go/types). In addition to being much simpler, this is also actually
correct for recognizing function-scoped type declarations (though we
don't currently support those anyway, nor would they go through this
exact code path).

To avoid possible future misuse of IsDotAlias, this CL also inlines
its trivial definition into its only call site.

Passes toolstash -cmp, also w/ -gcflags=all=-G=3.

Change-Id: I7c6283f4b58d5311aa683f8229bbf62f8bab2ff9
Reviewed-on: https://go-review.googlesource.com/c/go/+/320613
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
src/cmd/compile/internal/noder/decl.go
src/cmd/compile/internal/noder/import.go
src/cmd/compile/internal/noder/object.go
src/cmd/compile/internal/typecheck/iexport.go
src/cmd/compile/internal/typecheck/typecheck.go
src/cmd/compile/internal/types/pkg.go

index 4ca2eb4740a4916e171d2cfe278a38c9b472e9f1..3e55437afabace888058e3c5ac66e6719ca73d35 100644 (file)
@@ -104,13 +104,7 @@ func (g *irgen) typeDecl(out *ir.Nodes, decl *syntax.TypeDecl) {
        if decl.Alias {
                name, _ := g.def(decl.Name)
                g.pragmaFlags(decl.Pragma, 0)
-
-               // TODO(mdempsky): This matches how typecheckdef marks aliases for
-               // export, but this won't generalize to exporting function-scoped
-               // type aliases. We should maybe just use n.Alias() instead.
-               if ir.CurFunc == nil {
-                       name.Sym().Def = ir.TypeNode(name.Type())
-               }
+               assert(name.Alias()) // should be set by irgen.obj
 
                out.Append(ir.NewDecl(g.pos(decl), ir.ODCLTYPE, name))
                return
index 701e9001c859ea8d2819b0e2850a1604e8e0c978..c4a57806ebf9330760e466cb812ba16c35ce9f7c 100644 (file)
@@ -431,7 +431,7 @@ func clearImports() {
                        s.Def = nil
                        continue
                }
-               if types.IsDotAlias(s) {
+               if s.Def != nil && s.Def.Sym() != s {
                        // throw away top-level name left over
                        // from previous import . "x"
                        // We'll report errors after type checking in CheckDotImports.
index 82cce1ace0fcff81856f1751498f009c2d5fa84b..7af2fe6715ec477461e9cc26eed4a3f707bbed2b 100644 (file)
@@ -101,6 +101,7 @@ func (g *irgen) obj(obj types2.Object) *ir.Name {
        case *types2.TypeName:
                if obj.IsAlias() {
                        name = g.objCommon(pos, ir.OTYPE, g.sym(obj), class, g.typ(obj.Type()))
+                       name.SetAlias(true)
                } else {
                        name = ir.NewDeclNameAt(pos, ir.OTYPE, g.sym(obj))
                        g.objFinish(name, class, types.NewNamed(name))
index 64d68ef62550d8a4b5c2d3046336e48c46c8352c..3538c4d5a66a87b3ff80665c5018b46ec52ae676 100644 (file)
@@ -479,7 +479,7 @@ func (p *iexporter) doDecl(n *ir.Name) {
                w.constExt(n)
 
        case ir.OTYPE:
-               if types.IsDotAlias(n.Sym()) {
+               if n.Alias() {
                        // Alias.
                        w.tag('A')
                        w.pos(n.Pos())
index 95f7b50259fde3193d53ef33c83b6b74e275040e..9868c2d9a928a7756c716ff1da12277210a0cfa7 100644 (file)
@@ -1889,11 +1889,6 @@ func typecheckdef(n *ir.Name) {
                                        n.SetDiag(true)
                                        goto ret
                                }
-                               // For package-level type aliases, set n.Sym.Def so we can identify
-                               // it as a type alias during export. See also #31959.
-                               if n.Curfn == nil {
-                                       n.Sym().Def = n.Ntype
-                               }
                        }
                        break
                }
index a6d2e2007b0424d4927ef5cda15fb67d94d27186..f63a357f0d092921835d7b31896777780a0eabcd 100644 (file)
@@ -137,7 +137,3 @@ func CleanroomDo(f func()) {
        f()
        pkgMap = saved
 }
-
-func IsDotAlias(sym *Sym) bool {
-       return sym.Def != nil && sym.Def.Sym() != sym
-}