Skip to content

Commit d89a556

Browse files
committed
add a regression test for type names and reflect
We were recently altering the logic in reflect.go for type names, which could have broken this kind of valid use of reflection. Add a regression test, which I verified would break before my last change to "simplify" the logic, which actually changed the logic, as xuannv112 correctly pointed out. After thinking about the change in behavior for a little while, I realised that the new behavior is more correct, hence the test.
1 parent 8f72489 commit d89a556

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

testdata/script/reflect.txtar

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ func main() {
140140
// Ensure the types are kept in the binary. Use an anonymous type too.
141141
_ = fmt.Sprintf("%#v", EmbeddingObfuscated{})
142142
_ = fmt.Sprintf("%#v", struct{ExportedLocalObfuscated}{})
143+
144+
// reflection can see all type names, even local ones, so they cannot be obfuscated.
145+
{
146+
type TypeOfNamedField struct { NamedReflectionField int }
147+
type TypeOfEmbeddedField struct { EmbeddedReflectionField int }
148+
type TypeOfParent struct {
149+
ReflectionField TypeOfNamedField
150+
TypeOfEmbeddedField
151+
}
152+
t := reflect.TypeOf(TypeOfParent{})
153+
fmt.Println("TypeOfParent's own name:", t.Name())
154+
namedField, _ := t.FieldByName("ReflectionField")
155+
namedFieldField, _ := namedField.Type.FieldByName("NamedReflectionField")
156+
fmt.Println("TypeOfParent named:",
157+
namedField.Type.Name(),
158+
namedFieldField.Name,
159+
)
160+
embedField, _ := t.FieldByName("TypeOfEmbeddedField")
161+
embedFieldField, _ := embedField.Type.FieldByName("EmbeddedReflectionField")
162+
fmt.Println("TypeOfParent embedded:",
163+
embedField.Type.Name(),
164+
embedFieldField.Name,
165+
)
166+
}
143167
}
144168

145169
type EmbeddingIndirect struct {
@@ -448,3 +472,6 @@ VariadicReflection{ReflectionField:"variadic"}
448472
*main.StatUser
449473
*main.StatCompUser
450474
struct { UnnamedStructField string }
475+
TypeOfParent's own name: TypeOfParent
476+
TypeOfParent named: TypeOfNamedField NamedReflectionField
477+
TypeOfParent embedded: TypeOfEmbeddedField EmbeddedReflectionField

0 commit comments

Comments
 (0)