Source file src/cmd/compile/internal/types/type_test.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package types
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  
    13  	"cmd/internal/src"
    14  )
    15  
    16  // testTypeName is a minimal types.Object for NewNamed in tests.
    17  type testTypeName struct {
    18  	sym *Sym
    19  }
    20  
    21  func (o *testTypeName) Sym() *Sym     { return o.sym }
    22  func (o *testTypeName) Pos() src.XPos { return src.NoXPos }
    23  func (o *testTypeName) Type() *Type   { return nil }
    24  
    25  // If the printer mistakenly switches to fmtTypeID while formatting an
    26  // unexported interface method name, the method's signature is then
    27  // printed in fmtTypeID as well, so package-local names use Pkg.Prefix
    28  // (import path) instead of Pkg.Name. Regression test for that mistake.
    29  func TestFormatInterfaceUnexportedMethodUsesGoModeForSignature(t *testing.T) {
    30  	oldLocal := LocalPkg
    31  	oldPtr, oldReg, oldMax := PtrSize, RegSize, MaxWidth
    32  	t.Cleanup(func() {
    33  		LocalPkg = oldLocal
    34  		PtrSize, RegSize, MaxWidth = oldPtr, oldReg, oldMax
    35  	})
    36  	LocalPkg = NewPkg("main", "main")
    37  	PtrSize = 8
    38  	RegSize = 8
    39  	MaxWidth = 1 << 50
    40  
    41  	lib := NewPkg("example.com/lib", "lib")
    42  	namedT := NewNamed(&testTypeName{sym: lib.Lookup("T")})
    43  	// Underlying must not rely on Types[…]; InitTypes is not run in this package's tests.
    44  	namedT.SetUnderlying(NewStruct(nil))
    45  
    46  	sig := NewSignature(nil, nil, []*Field{NewField(src.NoXPos, nil, namedT)})
    47  	iface := NewInterface([]*Field{NewField(src.NoXPos, LocalPkg.Lookup("unexported"), sig)})
    48  	CalcSize(iface) // populate interface method set for printing (expandiface)
    49  
    50  	var buf bytes.Buffer
    51  	fmt.Fprintf(&buf, "%v", iface)
    52  	got := buf.String()
    53  	// Go syntax should qualify T with the package name, not the linker prefix / path.
    54  	const want = "lib.T"
    55  	if !strings.Contains(got, want) {
    56  		t.Fatalf("formatted type missing %q:\n%s", want, got)
    57  	}
    58  	// Stale fmtTypeID mode used Prefix ("example.com/lib") instead of Name ("lib").
    59  	if strings.Contains(got, "example.com/lib.T") {
    60  		t.Fatalf("formatted type should not use import path to qualify T (found example.com/lib.T):\n%s", got)
    61  	}
    62  }
    63  
    64  func TestSSACompare(t *testing.T) {
    65  	a := []*Type{
    66  		TypeInvalid,
    67  		TypeMem,
    68  		TypeFlags,
    69  		TypeVoid,
    70  		TypeInt128,
    71  	}
    72  	for _, x := range a {
    73  		for _, y := range a {
    74  			c := x.Compare(y)
    75  			if x == y && c != CMPeq || x != y && c == CMPeq {
    76  				t.Errorf("%s compare %s == %d\n", x.extra, y.extra, c)
    77  			}
    78  		}
    79  	}
    80  }
    81  

View as plain text