1
0
mirror of https://github.com/golang/go synced 2024-11-16 23:04:44 -07:00

go/types, types2: use | rather than ∪ when printing term lists

With this change, the termlist String() function prints termlists
in the usual Go notation and thus we can use it in error reporting.
Preparation for fixing #40350.

For #40350.

Change-Id: Ia28318841305de234a71af3146ce0c59f5e601a5
Reviewed-on: https://go-review.googlesource.com/c/go/+/410894
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2022-06-07 10:33:01 -07:00
parent 346698eea7
commit d4fb93be87
6 changed files with 148 additions and 148 deletions

View File

@ -25,7 +25,7 @@ func (xl termlist) String() string {
var buf bytes.Buffer
for i, x := range xl {
if i > 0 {
buf.WriteString(" ")
buf.WriteString(" | ")
}
buf.WriteString(x.String())
}

View File

@ -12,7 +12,7 @@ import (
// maketl makes a term list from a string of the term list.
func maketl(s string) termlist {
s = strings.ReplaceAll(s, " ", "")
names := strings.Split(s, "")
names := strings.Split(s, "|")
r := make(termlist, len(names))
for i, n := range names {
r[i] = testTerm(n)
@ -33,10 +33,10 @@ func TestTermlistString(t *testing.T) {
"int",
"~int",
"myInt",
"∅ ∅",
"𝓤 𝓤",
"∅ 𝓤 int",
"∅ 𝓤 int myInt",
"∅ | ∅",
"𝓤 | 𝓤",
"∅ | 𝓤 | int",
"∅ | 𝓤 | int | myInt",
} {
if got := maketl(want).String(); got != want {
t.Errorf("(%v).String() == %v", want, got)
@ -47,12 +47,12 @@ func TestTermlistString(t *testing.T) {
func TestTermlistIsEmpty(t *testing.T) {
for test, want := range map[string]bool{
"∅": true,
"∅ ∅": true,
"∅ 𝓤": false,
"∅ myInt": false,
"∅ | ∅": true,
"∅ | ∅ | 𝓤": false,
"∅ | ∅ | myInt": false,
"𝓤": false,
"𝓤 int": false,
"𝓤 myInt ∅": false,
"𝓤 | int": false,
"𝓤 | myInt | ∅": false,
} {
xl := maketl(test)
got := xl.isEmpty()
@ -65,13 +65,13 @@ func TestTermlistIsEmpty(t *testing.T) {
func TestTermlistIsAll(t *testing.T) {
for test, want := range map[string]bool{
"∅": false,
"∅ ∅": false,
"int ~string": false,
"~int myInt": false,
"∅ 𝓤": true,
"∅ | ∅": false,
"int | ~string": false,
"~int | myInt": false,
"∅ | ∅ | 𝓤": true,
"𝓤": true,
"𝓤 int": true,
"myInt 𝓤": true,
"𝓤 | int": true,
"myInt | 𝓤": true,
} {
xl := maketl(test)
got := xl.isAll()
@ -86,17 +86,17 @@ func TestTermlistNorm(t *testing.T) {
xl, want string
}{
{"∅", "∅"},
{"∅ ∅", "∅"},
{"∅ int", "int"},
{"∅ myInt", "myInt"},
{"𝓤 int", "𝓤"},
{"𝓤 myInt", "𝓤"},
{"int myInt", "int myInt"},
{"~int int", "~int"},
{"~int myInt", "~int"},
{"int ~string int", "int ~string"},
{"~int string 𝓤 ~string int", "𝓤"},
{"~int string myInt ~string int", "~int ~string"},
{"∅ | ∅", "∅"},
{"∅ | int", "int"},
{"∅ | myInt", "myInt"},
{"𝓤 | int", "𝓤"},
{"𝓤 | myInt", "𝓤"},
{"int | myInt", "int | myInt"},
{"~int | int", "~int"},
{"~int | myInt", "~int"},
{"int | ~string | int", "int | ~string"},
{"~int | string | 𝓤 | ~string | int", "𝓤"},
{"~int | string | myInt | ~string | int", "~int | ~string"},
} {
xl := maketl(test.xl)
got := maketl(test.xl).norm()
@ -116,15 +116,15 @@ func TestTermlistUnion(t *testing.T) {
{"∅", "int", "int"},
{"𝓤", "~int", "𝓤"},
{"int", "~int", "~int"},
{"int", "string", "int string"},
{"int", "myInt", "int myInt"},
{"int", "string", "int | string"},
{"int", "myInt", "int | myInt"},
{"~int", "myInt", "~int"},
{"int string", "~string", "int ~string"},
{"~int string", "~string int", "~int ~string"},
{"~int string ∅", "~string int", "~int ~string"},
{"~int myInt ∅", "~string int", "~int ~string"},
{"~int string 𝓤", "~string int", "𝓤"},
{"~int string myInt", "~string int", "~int ~string"},
{"int | string", "~string", "int | ~string"},
{"~int | string", "~string | int", "~int | ~string"},
{"~int | string | ∅", "~string | int", "~int | ~string"},
{"~int | myInt | ∅", "~string | int", "~int | ~string"},
{"~int | string | 𝓤", "~string | int", "𝓤"},
{"~int | string | myInt", "~string | int", "~int | ~string"},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)
@ -150,12 +150,12 @@ func TestTermlistIntersect(t *testing.T) {
{"int", "string", "∅"},
{"int", "myInt", "∅"},
{"~int", "myInt", "myInt"},
{"int string", "~string", "string"},
{"~int string", "~string int", "int string"},
{"~int string ∅", "~string int", "int string"},
{"~int myInt ∅", "~string int", "int"},
{"~int string 𝓤", "~string int", "int ~string"},
{"~int string myInt", "~string int", "int string"},
{"int | string", "~string", "string"},
{"~int | string", "~string | int", "int | string"},
{"~int | string | ∅", "~string | int", "int | string"},
{"~int | myInt | ∅", "~string | int", "int"},
{"~int | string | 𝓤", "~string | int", "int | ~string"},
{"~int | string | myInt", "~string | int", "int | string"},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)
@ -174,12 +174,12 @@ func TestTermlistEqual(t *testing.T) {
{"∅", "∅", true},
{"∅", "𝓤", false},
{"𝓤", "𝓤", true},
{"𝓤 int", "𝓤", true},
{"𝓤 int", "string 𝓤", true},
{"𝓤 myInt", "string 𝓤", true},
{"int ~string", "string int", false},
{"~int string", "string myInt", false},
{"int ~string ∅", "string int ~string", true},
{"𝓤 | int", "𝓤", true},
{"𝓤 | int", "string | 𝓤", true},
{"𝓤 | myInt", "string | 𝓤", true},
{"int | ~string", "string | int", false},
{"~int | string", "string | myInt", false},
{"int | ~string | ∅", "string | int | ~string", true},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)
@ -201,11 +201,11 @@ func TestTermlistIncludes(t *testing.T) {
{"int", "string", false},
{"~int", "string", false},
{"~int", "myInt", true},
{"int string", "string", true},
{"~int string", "int", true},
{"~int string", "myInt", true},
{"~int myInt ∅", "myInt", true},
{"myInt 𝓤", "int", true},
{"int | string", "string", true},
{"~int | string", "int", true},
{"~int | string", "myInt", true},
{"~int | myInt | ∅", "myInt", true},
{"myInt | ∅ | 𝓤", "int", true},
} {
xl := maketl(test.xl)
yl := testTerm(test.typ).typ
@ -236,12 +236,12 @@ func TestTermlistSupersetOf(t *testing.T) {
{"myInt", "~int", false},
{"int", "string", false},
{"~int", "string", false},
{"int string", "string", true},
{"int string", "~string", false},
{"~int string", "int", true},
{"~int string", "myInt", true},
{"~int string ∅", "string", true},
{"~string 𝓤", "myInt", true},
{"int | string", "string", true},
{"int | string", "~string", false},
{"~int | string", "int", true},
{"~int | string", "myInt", true},
{"~int | string | ∅", "string", true},
{"~string | ∅ | 𝓤", "myInt", true},
} {
xl := maketl(test.xl)
y := testTerm(test.typ)
@ -261,18 +261,18 @@ func TestTermlistSubsetOf(t *testing.T) {
{"∅", "𝓤", true},
{"𝓤", "∅", false},
{"𝓤", "𝓤", true},
{"int", "int string", true},
{"~int", "int string", false},
{"~int", "myInt string", false},
{"myInt", "~int string", true},
{"~int", "string string int ~int", true},
{"myInt", "string string ~int", true},
{"int string", "string", false},
{"int string", "string int", true},
{"int ~string", "string int", false},
{"myInt ~string", "string int 𝓤", true},
{"int ~string", "string int string", false},
{"int myInt", "string ~int string", true},
{"int", "int | string", true},
{"~int", "int | string", false},
{"~int", "myInt | string", false},
{"myInt", "~int | string", true},
{"~int", "string | string | int | ~int", true},
{"myInt", "string | string | ~int", true},
{"int | string", "string", false},
{"int | string", "string | int", true},
{"int | ~string", "string | int", false},
{"myInt | ~string", "string | int | 𝓤", true},
{"int | ~string", "string | int | ∅ | string", false},
{"int | myInt", "string | ~int | ∅ | string", true},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)

View File

@ -21,13 +21,13 @@ func TestTypeSetString(t *testing.T) {
"{}": "𝓤",
"{int}": "{int}",
"{~int}": "{~int}",
"{int|string}": "{int string}",
"{int|string}": "{int | string}",
"{int; string}": "∅",
"{comparable}": "{comparable}",
"{comparable; int}": "{int}",
"{~int; comparable}": "{~int}",
"{int|string; comparable}": "{int string}",
"{int|string; comparable}": "{int | string}",
"{comparable; int; string}": "∅",
"{m()}": "{func (p.T).m()}",
@ -37,7 +37,7 @@ func TestTypeSetString(t *testing.T) {
"{m1(); comparable; m2() int }": "{comparable; func (p.T).m1(); func (p.T).m2() int}",
"{comparable; error}": "{comparable; func (error).Error() string}",
"{m(); comparable; int|float32|string}": "{func (p.T).m(); int float32 string}",
"{m(); comparable; int|float32|string}": "{func (p.T).m(); int | float32 | string}",
"{m1(); int; m2(); comparable }": "{func (p.T).m1(); func (p.T).m2(); int}",
"{E}; type E interface{}": "𝓤",

View File

@ -25,7 +25,7 @@ func (xl termlist) String() string {
var buf bytes.Buffer
for i, x := range xl {
if i > 0 {
buf.WriteString(" ")
buf.WriteString(" | ")
}
buf.WriteString(x.String())
}

View File

@ -12,7 +12,7 @@ import (
// maketl makes a term list from a string of the term list.
func maketl(s string) termlist {
s = strings.ReplaceAll(s, " ", "")
names := strings.Split(s, "")
names := strings.Split(s, "|")
r := make(termlist, len(names))
for i, n := range names {
r[i] = testTerm(n)
@ -33,10 +33,10 @@ func TestTermlistString(t *testing.T) {
"int",
"~int",
"myInt",
"∅ ∅",
"𝓤 𝓤",
"∅ 𝓤 int",
"∅ 𝓤 int myInt",
"∅ | ∅",
"𝓤 | 𝓤",
"∅ | 𝓤 | int",
"∅ | 𝓤 | int | myInt",
} {
if got := maketl(want).String(); got != want {
t.Errorf("(%v).String() == %v", want, got)
@ -47,12 +47,12 @@ func TestTermlistString(t *testing.T) {
func TestTermlistIsEmpty(t *testing.T) {
for test, want := range map[string]bool{
"∅": true,
"∅ ∅": true,
"∅ 𝓤": false,
"∅ myInt": false,
"∅ | ∅": true,
"∅ | ∅ | 𝓤": false,
"∅ | ∅ | myInt": false,
"𝓤": false,
"𝓤 int": false,
"𝓤 myInt ∅": false,
"𝓤 | int": false,
"𝓤 | myInt | ∅": false,
} {
xl := maketl(test)
got := xl.isEmpty()
@ -65,13 +65,13 @@ func TestTermlistIsEmpty(t *testing.T) {
func TestTermlistIsAll(t *testing.T) {
for test, want := range map[string]bool{
"∅": false,
"∅ ∅": false,
"int ~string": false,
"~int myInt": false,
"∅ 𝓤": true,
"∅ | ∅": false,
"int | ~string": false,
"~int | myInt": false,
"∅ | ∅ | 𝓤": true,
"𝓤": true,
"𝓤 int": true,
"myInt 𝓤": true,
"𝓤 | int": true,
"myInt | 𝓤": true,
} {
xl := maketl(test)
got := xl.isAll()
@ -86,17 +86,17 @@ func TestTermlistNorm(t *testing.T) {
xl, want string
}{
{"∅", "∅"},
{"∅ ∅", "∅"},
{"∅ int", "int"},
{"∅ myInt", "myInt"},
{"𝓤 int", "𝓤"},
{"𝓤 myInt", "𝓤"},
{"int myInt", "int myInt"},
{"~int int", "~int"},
{"~int myInt", "~int"},
{"int ~string int", "int ~string"},
{"~int string 𝓤 ~string int", "𝓤"},
{"~int string myInt ~string int", "~int ~string"},
{"∅ | ∅", "∅"},
{"∅ | int", "int"},
{"∅ | myInt", "myInt"},
{"𝓤 | int", "𝓤"},
{"𝓤 | myInt", "𝓤"},
{"int | myInt", "int | myInt"},
{"~int | int", "~int"},
{"~int | myInt", "~int"},
{"int | ~string | int", "int | ~string"},
{"~int | string | 𝓤 | ~string | int", "𝓤"},
{"~int | string | myInt | ~string | int", "~int | ~string"},
} {
xl := maketl(test.xl)
got := maketl(test.xl).norm()
@ -116,15 +116,15 @@ func TestTermlistUnion(t *testing.T) {
{"∅", "int", "int"},
{"𝓤", "~int", "𝓤"},
{"int", "~int", "~int"},
{"int", "string", "int string"},
{"int", "myInt", "int myInt"},
{"int", "string", "int | string"},
{"int", "myInt", "int | myInt"},
{"~int", "myInt", "~int"},
{"int string", "~string", "int ~string"},
{"~int string", "~string int", "~int ~string"},
{"~int string ∅", "~string int", "~int ~string"},
{"~int myInt ∅", "~string int", "~int ~string"},
{"~int string 𝓤", "~string int", "𝓤"},
{"~int string myInt", "~string int", "~int ~string"},
{"int | string", "~string", "int | ~string"},
{"~int | string", "~string | int", "~int | ~string"},
{"~int | string | ∅", "~string | int", "~int | ~string"},
{"~int | myInt | ∅", "~string | int", "~int | ~string"},
{"~int | string | 𝓤", "~string | int", "𝓤"},
{"~int | string | myInt", "~string | int", "~int | ~string"},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)
@ -150,12 +150,12 @@ func TestTermlistIntersect(t *testing.T) {
{"int", "string", "∅"},
{"int", "myInt", "∅"},
{"~int", "myInt", "myInt"},
{"int string", "~string", "string"},
{"~int string", "~string int", "int string"},
{"~int string ∅", "~string int", "int string"},
{"~int myInt ∅", "~string int", "int"},
{"~int string 𝓤", "~string int", "int ~string"},
{"~int string myInt", "~string int", "int string"},
{"int | string", "~string", "string"},
{"~int | string", "~string | int", "int | string"},
{"~int | string | ∅", "~string | int", "int | string"},
{"~int | myInt | ∅", "~string | int", "int"},
{"~int | string | 𝓤", "~string | int", "int | ~string"},
{"~int | string | myInt", "~string | int", "int | string"},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)
@ -174,12 +174,12 @@ func TestTermlistEqual(t *testing.T) {
{"∅", "∅", true},
{"∅", "𝓤", false},
{"𝓤", "𝓤", true},
{"𝓤 int", "𝓤", true},
{"𝓤 int", "string 𝓤", true},
{"𝓤 myInt", "string 𝓤", true},
{"int ~string", "string int", false},
{"~int string", "string myInt", false},
{"int ~string ∅", "string int ~string", true},
{"𝓤 | int", "𝓤", true},
{"𝓤 | int", "string | 𝓤", true},
{"𝓤 | myInt", "string | 𝓤", true},
{"int | ~string", "string | int", false},
{"~int | string", "string | myInt", false},
{"int | ~string | ∅", "string | int | ~string", true},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)
@ -201,11 +201,11 @@ func TestTermlistIncludes(t *testing.T) {
{"int", "string", false},
{"~int", "string", false},
{"~int", "myInt", true},
{"int string", "string", true},
{"~int string", "int", true},
{"~int string", "myInt", true},
{"~int myInt ∅", "myInt", true},
{"myInt 𝓤", "int", true},
{"int | string", "string", true},
{"~int | string", "int", true},
{"~int | string", "myInt", true},
{"~int | myInt | ∅", "myInt", true},
{"myInt | ∅ | 𝓤", "int", true},
} {
xl := maketl(test.xl)
yl := testTerm(test.typ).typ
@ -236,12 +236,12 @@ func TestTermlistSupersetOf(t *testing.T) {
{"myInt", "~int", false},
{"int", "string", false},
{"~int", "string", false},
{"int string", "string", true},
{"int string", "~string", false},
{"~int string", "int", true},
{"~int string", "myInt", true},
{"~int string ∅", "string", true},
{"~string 𝓤", "myInt", true},
{"int | string", "string", true},
{"int | string", "~string", false},
{"~int | string", "int", true},
{"~int | string", "myInt", true},
{"~int | string | ∅", "string", true},
{"~string | ∅ | 𝓤", "myInt", true},
} {
xl := maketl(test.xl)
y := testTerm(test.typ)
@ -261,18 +261,18 @@ func TestTermlistSubsetOf(t *testing.T) {
{"∅", "𝓤", true},
{"𝓤", "∅", false},
{"𝓤", "𝓤", true},
{"int", "int string", true},
{"~int", "int string", false},
{"~int", "myInt string", false},
{"myInt", "~int string", true},
{"~int", "string string int ~int", true},
{"myInt", "string string ~int", true},
{"int string", "string", false},
{"int string", "string int", true},
{"int ~string", "string int", false},
{"myInt ~string", "string int 𝓤", true},
{"int ~string", "string int string", false},
{"int myInt", "string ~int string", true},
{"int", "int | string", true},
{"~int", "int | string", false},
{"~int", "myInt | string", false},
{"myInt", "~int | string", true},
{"~int", "string | string | int | ~int", true},
{"myInt", "string | string | ~int", true},
{"int | string", "string", false},
{"int | string", "string | int", true},
{"int | ~string", "string | int", false},
{"myInt | ~string", "string | int | 𝓤", true},
{"int | ~string", "string | int | ∅ | string", false},
{"int | myInt", "string | ~int | ∅ | string", true},
} {
xl := maketl(test.xl)
yl := maketl(test.yl)

View File

@ -22,13 +22,13 @@ func TestTypeSetString(t *testing.T) {
"{}": "𝓤",
"{int}": "{int}",
"{~int}": "{~int}",
"{int|string}": "{int string}",
"{int|string}": "{int | string}",
"{int; string}": "∅",
"{comparable}": "{comparable}",
"{comparable; int}": "{int}",
"{~int; comparable}": "{~int}",
"{int|string; comparable}": "{int string}",
"{int|string; comparable}": "{int | string}",
"{comparable; int; string}": "∅",
"{m()}": "{func (p.T).m()}",
@ -38,7 +38,7 @@ func TestTypeSetString(t *testing.T) {
"{m1(); comparable; m2() int }": "{comparable; func (p.T).m1(); func (p.T).m2() int}",
"{comparable; error}": "{comparable; func (error).Error() string}",
"{m(); comparable; int|float32|string}": "{func (p.T).m(); int float32 string}",
"{m(); comparable; int|float32|string}": "{func (p.T).m(); int | float32 | string}",
"{m1(); int; m2(); comparable }": "{func (p.T).m1(); func (p.T).m2(); int}",
"{E}; type E interface{}": "𝓤",