lang: types: Print the full function args

This makes it easier to see logs, but more importantly copy (well the
copy hack) doesn't erase arg names anymore.
This commit is contained in:
James Shubin
2023-06-25 12:20:31 -04:00
parent 74a21bab1a
commit 13b6b9de60
2 changed files with 27 additions and 9 deletions

View File

@@ -553,8 +553,11 @@ func (obj *Type) String() string {
if t == nil { if t == nil {
panic("malformed func field") panic("malformed func field")
} }
//s[i] = fmt.Sprintf("%s %s", k, t.String()) // strict
s[i] = t.String() // We need to print function arg names for Copy() to use
// the String() hack here and avoid erasing them here!
//s[i] = t.String()
s[i] = fmt.Sprintf("%s %s", k, t.String()) // strict
} }
var out string var out string
if obj.Out != nil { if obj.Out != nil {
@@ -723,6 +726,7 @@ func (obj *Type) Cmp(typ *Type) error {
// Copy copies this type so that inplace modification won't affect the original. // Copy copies this type so that inplace modification won't affect the original.
func (obj *Type) Copy() *Type { func (obj *Type) Copy() *Type {
// String() needs to print function arg names or they'd get erased here!
return NewType(obj.String()) // hack to do this easily return NewType(obj.String()) // hack to do this easily
} }

View File

@@ -501,7 +501,7 @@ func TestType1(t *testing.T) {
Kind: KindFloat, Kind: KindFloat,
}, },
}, },
"func(str) bool": { "func(a0 str) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -516,7 +516,7 @@ func TestType1(t *testing.T) {
Kind: KindBool, Kind: KindBool,
}, },
}, },
"func(str, int) bool": { "func(hello str, answer int) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -535,7 +535,7 @@ func TestType1(t *testing.T) {
Kind: KindBool, Kind: KindBool,
}, },
}, },
"func(str, []int, float) bool": { "func(a0 str, a1 []int, a2 float) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -561,7 +561,7 @@ func TestType1(t *testing.T) {
Kind: KindBool, Kind: KindBool,
}, },
}, },
"func(map{str: int}) bool": { "func(answer map{str: int}) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -582,7 +582,7 @@ func TestType1(t *testing.T) {
Kind: KindBool, Kind: KindBool,
}, },
}, },
"func(bool, map{str: int}) bool": { "func(hello bool, answer map{str: int}) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -607,7 +607,7 @@ func TestType1(t *testing.T) {
Kind: KindBool, Kind: KindBool,
}, },
}, },
"func(struct{a str; bb int}) bool": { "func(answer struct{a str; bb int}) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -634,7 +634,7 @@ func TestType1(t *testing.T) {
Kind: KindBool, Kind: KindBool,
}, },
}, },
"func(bool, struct{a str; bb int}) bool": { "func(hello bool, answer struct{a str; bb int}) bool": {
Kind: KindFunc, Kind: KindFunc,
// key names are arbitrary... // key names are arbitrary...
Map: map[string]*Type{ Map: map[string]*Type{
@@ -1472,6 +1472,20 @@ func TestComplexCmp0(t *testing.T) {
} }
} }
func TestTypeCopy0(t *testing.T) {
typ := NewType("func(arg0 str, arg1 bool) int")
cp := typ.Copy()
if len(cp.Ord) != 2 {
t.Errorf("incorrect ord length after Copy")
}
if cp.Ord[0] != "arg0" {
t.Errorf("incorrect 0th arg name after Copy")
}
if cp.Ord[1] != "arg1" {
t.Errorf("incorrect 1st arg name after Copy")
}
}
func TestTypeOf0(t *testing.T) { func TestTypeOf0(t *testing.T) {
// TODO: implement testing of the TypeOf function // TODO: implement testing of the TypeOf function
// TODO: implement testing TypeOf for struct field name mappings // TODO: implement testing TypeOf for struct field name mappings