]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile: tweaks to match types2
authorMatthew Dempsky <mdempsky@google.com>
Thu, 27 May 2021 02:52:31 +0000 (19:52 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 27 May 2021 22:13:38 +0000 (22:13 +0000)
This CL makes a handful of changes to either bring existing compiler
output consistent with what types2 produces or to make it easier to
reproduce with types2:

1. The position for embedded fields is corrected to the position of
the syntax.Field, rather than the syntax.Type.

2. Methods and embedded types are sorted in export data the same way
that types2 sorts them.

3. Don't write out position information for OLITERALs that don't have
their own position (i.e., references to named constants).

Change-Id: Ic3979215ae9ef280cfbba7b44c236e03fc12a2ef
Reviewed-on: https://go-review.googlesource.com/c/go/+/323209
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>

src/cmd/compile/internal/noder/noder.go
src/cmd/compile/internal/typecheck/iexport.go
src/cmd/compile/internal/types/size.go
src/cmd/compile/internal/types/sort.go

index 06c3b00601f601ea6f335e91db4b635f8ebc3ca8..44385f34fd1c0a9834199cbff5361bde3078eaf2 100644 (file)
@@ -986,6 +986,8 @@ func (p *noder) packname(expr syntax.Expr) *types.Sym {
 }
 
 func (p *noder) embedded(typ syntax.Expr) *ir.Field {
+       pos := p.pos(syntax.StartPos(typ))
+
        op, isStar := typ.(*syntax.Operation)
        if isStar {
                if op.Op != syntax.Mul || op.Y != nil {
@@ -995,11 +997,11 @@ func (p *noder) embedded(typ syntax.Expr) *ir.Field {
        }
 
        sym := p.packname(typ)
-       n := ir.NewField(p.pos(typ), typecheck.Lookup(sym.Name), importName(sym).(ir.Ntype), nil)
+       n := ir.NewField(pos, typecheck.Lookup(sym.Name), importName(sym).(ir.Ntype), nil)
        n.Embedded = true
 
        if isStar {
-               n.Ntype = ir.NewStarExpr(p.pos(op), n.Ntype)
+               n.Ntype = ir.NewStarExpr(pos, n.Ntype)
        }
        return n
 }
index 9c24213176957d780758083b7d5868b1208e063c..e798ce5143522f7fb174dc6b0660644bede8acf0 100644 (file)
@@ -540,9 +540,12 @@ func (p *iexporter) doDecl(n *ir.Name) {
                        break
                }
 
-               ms := t.Methods()
-               w.uint64(uint64(ms.Len()))
-               for _, m := range ms.Slice() {
+               // Sort methods, for consistency with types2.
+               methods := append([]*types.Field(nil), t.Methods().Slice()...)
+               sort.Sort(types.MethodsByName(methods))
+
+               w.uint64(uint64(len(methods)))
+               for _, m := range methods {
                        w.pos(m.Pos)
                        w.selector(m.Sym)
                        w.param(m.Type.Recv())
@@ -550,7 +553,7 @@ func (p *iexporter) doDecl(n *ir.Name) {
                }
 
                w.typeExt(t)
-               for _, m := range ms.Slice() {
+               for _, m := range methods {
                        w.methExt(m)
                }
 
@@ -939,6 +942,12 @@ func (w *exportWriter) doTyp(t *types.Type) {
                        }
                }
 
+               // Sort methods and embedded types, for consistency with types2.
+               // Note: embedded types may be anonymous, and types2 sorts them
+               // with sort.Stable too.
+               sort.Sort(types.MethodsByName(methods))
+               sort.Stable(types.EmbeddedsByName(embeddeds))
+
                w.startType(interfaceType)
                w.setPkg(t.Pkg(), true)
 
@@ -1590,7 +1599,11 @@ func (w *exportWriter) expr(n ir.Node) {
 
        case ir.OLITERAL:
                w.op(ir.OLITERAL)
-               w.pos(n.Pos())
+               if ir.HasUniquePos(n) {
+                       w.pos(n.Pos())
+               } else {
+                       w.pos(src.NoXPos)
+               }
                w.value(n.Type(), n.Val())
 
        case ir.ONAME:
index 7059eff398c008da653740ab68632a188a64714e..e6ca4556b94765d639110509ae762366b78883f3 100644 (file)
@@ -126,10 +126,15 @@ func expandiface(t *Type) {
                // (including broken ones, if any) and add to t's
                // method set.
                for _, t1 := range m.Type.AllMethods().Slice() {
-                       // Use m.Pos rather than t1.Pos to preserve embedding position.
                        f := NewField(m.Pos, t1.Sym, t1.Type)
                        addMethod(f, false)
+
+                       // Clear position after typechecking, for consistency with types2.
+                       f.Pos = src.NoXPos
                }
+
+               // Clear position after typechecking, for consistency with types2.
+               m.Pos = src.NoXPos
        }
 
        sort.Sort(MethodsByName(methods))
index dc59b064153282cd0808a0fd3d2b4d51cd98680b..765c070cd94193c4c37db0f4415d1ee3ea06b031 100644 (file)
@@ -4,11 +4,16 @@
 
 package types
 
-// MethodsByName sorts methods by symbol.
+// MethodsByName sorts methods by name.
 type MethodsByName []*Field
 
-func (x MethodsByName) Len() int { return len(x) }
+func (x MethodsByName) Len() int           { return len(x) }
+func (x MethodsByName) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
+func (x MethodsByName) Less(i, j int) bool { return x[i].Sym.Less(x[j].Sym) }
 
-func (x MethodsByName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
+// EmbeddedsByName sorts embedded types by name.
+type EmbeddedsByName []*Field
 
-func (x MethodsByName) Less(i, j int) bool { return x[i].Sym.Less(x[j].Sym) }
+func (x EmbeddedsByName) Len() int           { return len(x) }
+func (x EmbeddedsByName) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
+func (x EmbeddedsByName) Less(i, j int) bool { return x[i].Type.Sym().Less(x[j].Type.Sym()) }