Skip to content

Commit 4f26202

Browse files
committed
cmd/compile/internal/types2: fix type set printing and add test
Change-Id: I44ca1f889b041467d5febacaf6037cfd75859175 Reviewed-on: https://go-review.googlesource.com/c/go/+/344873 Trust: Robert Griesemer <[email protected]> Trust: Dan Scales <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent 0ac64f6 commit 4f26202

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

src/cmd/compile/internal/types2/typeset.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,24 @@ func (s *_TypeSet) String() string {
7777
var buf bytes.Buffer
7878
buf.WriteByte('{')
7979
if s.comparable {
80-
buf.WriteString(" comparable")
80+
buf.WriteString("comparable")
8181
if hasMethods || hasTerms {
82-
buf.WriteByte(';')
82+
buf.WriteString("; ")
8383
}
8484
}
8585
for i, m := range s.methods {
8686
if i > 0 {
87-
buf.WriteByte(';')
87+
buf.WriteString("; ")
8888
}
89-
buf.WriteByte(' ')
9089
buf.WriteString(m.String())
9190
}
9291
if hasMethods && hasTerms {
93-
buf.WriteByte(';')
92+
buf.WriteString("; ")
9493
}
9594
if hasTerms {
9695
buf.WriteString(s.terms.String())
9796
}
98-
buf.WriteString(" }") // there was at least one method or term
99-
97+
buf.WriteString("}")
10098
return buf.String()
10199
}
102100

src/cmd/compile/internal/types2/typeset_test.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,77 @@
44

55
package types2
66

7-
import "testing"
7+
import (
8+
"cmd/compile/internal/syntax"
9+
"strings"
10+
"testing"
11+
)
812

913
func TestInvalidTypeSet(t *testing.T) {
1014
if !invalidTypeSet.IsEmpty() {
1115
t.Error("invalidTypeSet is not empty")
1216
}
1317
}
1418

19+
func TestTypeSetString(t *testing.T) {
20+
for body, want := range map[string]string{
21+
"{}": "𝓤",
22+
"{int}": "{int}",
23+
"{~int}": "{~int}",
24+
"{int|string}": "{int ∪ string}",
25+
"{int; string}": "∅",
26+
27+
"{comparable}": "{comparable}",
28+
"{comparable; int}": "{comparable; int}",
29+
"{~int; comparable}": "{comparable; ~int}",
30+
"{int|string; comparable}": "{comparable; int ∪ string}",
31+
"{comparable; int; string}": "∅",
32+
33+
"{m()}": "{func (p.T).m()}",
34+
"{m1(); m2() int }": "{func (p.T).m1(); func (p.T).m2() int}",
35+
"{error}": "{func (error).Error() string}",
36+
"{m(); comparable}": "{comparable; func (p.T).m()}",
37+
"{m1(); comparable; m2() int }": "{comparable; func (p.T).m1(); func (p.T).m2() int}",
38+
"{comparable; error}": "{comparable; func (error).Error() string}",
39+
40+
"{m(); comparable; int|float32|string}": "{comparable; func (p.T).m(); int ∪ float32 ∪ string}",
41+
"{m1(); int; m2(); comparable }": "{comparable; func (p.T).m1(); func (p.T).m2(); int}",
42+
43+
"{E}; type E interface{}": "𝓤",
44+
"{E}; type E interface{int;string}": "∅",
45+
"{E}; type E interface{comparable}": "{comparable}",
46+
} {
47+
// parse
48+
errh := func(error) {} // dummy error handler so that parsing continues in presence of errors
49+
src := "package p; type T interface" + body
50+
file, err := syntax.Parse(nil, strings.NewReader(src), errh, nil, syntax.AllowGenerics)
51+
if err != nil {
52+
t.Fatalf("%s: %v (invalid test case)", body, err)
53+
}
54+
55+
// type check
56+
var conf Config
57+
pkg, err := conf.Check(file.PkgName.Value, []*syntax.File{file}, nil)
58+
if err != nil {
59+
t.Fatalf("%s: %v (invalid test case)", body, err)
60+
}
61+
62+
// lookup T
63+
obj := pkg.scope.Lookup("T")
64+
if obj == nil {
65+
t.Fatalf("%s: T not found (invalid test case)", body)
66+
}
67+
T, ok := under(obj.Type()).(*Interface)
68+
if !ok {
69+
t.Fatalf("%s: %v is not an interface (invalid test case)", body, obj)
70+
}
71+
72+
// verify test case
73+
got := T.typeSet().String()
74+
if got != want {
75+
t.Errorf("%s: got %s; want %s", body, got, want)
76+
}
77+
}
78+
}
79+
1580
// TODO(gri) add more tests

0 commit comments

Comments
 (0)