diff --git a/lang/types/type.go b/lang/types/type.go index 4bc4893e..b473f18f 100644 --- a/lang/types/type.go +++ b/lang/types/type.go @@ -553,8 +553,11 @@ func (obj *Type) String() string { if t == nil { 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 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. 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 } diff --git a/lang/types/type_test.go b/lang/types/type_test.go index 3e92eeaf..4ffc9d76 100644 --- a/lang/types/type_test.go +++ b/lang/types/type_test.go @@ -501,7 +501,7 @@ func TestType1(t *testing.T) { Kind: KindFloat, }, }, - "func(str) bool": { + "func(a0 str) bool": { Kind: KindFunc, // key names are arbitrary... Map: map[string]*Type{ @@ -516,7 +516,7 @@ func TestType1(t *testing.T) { Kind: KindBool, }, }, - "func(str, int) bool": { + "func(hello str, answer int) bool": { Kind: KindFunc, // key names are arbitrary... Map: map[string]*Type{ @@ -535,7 +535,7 @@ func TestType1(t *testing.T) { Kind: KindBool, }, }, - "func(str, []int, float) bool": { + "func(a0 str, a1 []int, a2 float) bool": { Kind: KindFunc, // key names are arbitrary... Map: map[string]*Type{ @@ -561,7 +561,7 @@ func TestType1(t *testing.T) { Kind: KindBool, }, }, - "func(map{str: int}) bool": { + "func(answer map{str: int}) bool": { Kind: KindFunc, // key names are arbitrary... Map: map[string]*Type{ @@ -582,7 +582,7 @@ func TestType1(t *testing.T) { Kind: KindBool, }, }, - "func(bool, map{str: int}) bool": { + "func(hello bool, answer map{str: int}) bool": { Kind: KindFunc, // key names are arbitrary... Map: map[string]*Type{ @@ -607,7 +607,7 @@ func TestType1(t *testing.T) { Kind: KindBool, }, }, - "func(struct{a str; bb int}) bool": { + "func(answer struct{a str; bb int}) bool": { Kind: KindFunc, // key names are arbitrary... Map: map[string]*Type{ @@ -634,7 +634,7 @@ func TestType1(t *testing.T) { Kind: KindBool, }, }, - "func(bool, struct{a str; bb int}) bool": { + "func(hello bool, answer struct{a str; bb int}) bool": { Kind: KindFunc, // key names are arbitrary... 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) { // TODO: implement testing of the TypeOf function // TODO: implement testing TypeOf for struct field name mappings