diff --git a/_cmptest/reflect_indirect/test.go b/_cmptest/reflect_indirect/test.go new file mode 100644 index 00000000..29fcf750 --- /dev/null +++ b/_cmptest/reflect_indirect/test.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "reflect" +) + +func main() { + x := 42 + p := &x + + // Test 1: Non-pointer value - should return same value + v1 := reflect.Indirect(reflect.ValueOf(x)) + fmt.Printf("Non-pointer: %v\n", v1.Interface()) + + // Test 2: Pointer - should dereference + v2 := reflect.Indirect(reflect.ValueOf(p)) + fmt.Printf("Pointer: %v\n", v2.Interface()) + + // Test 3: Nil pointer + var nilPtr *int + v3 := reflect.Indirect(reflect.ValueOf(nilPtr)) + fmt.Printf("Nil pointer valid: %v\n", v3.IsValid()) +} diff --git a/runtime/internal/lib/reflect/value.go b/runtime/internal/lib/reflect/value.go index 4cec494d..f1657ab5 100644 --- a/runtime/internal/lib/reflect/value.go +++ b/runtime/internal/lib/reflect/value.go @@ -1759,6 +1759,16 @@ func ValueOf(i any) Value { return unpackEface(i) } +// Indirect returns the value that v points to. +// If v is a nil pointer, Indirect returns a zero Value. +// If v is not a pointer, Indirect returns v. +func Indirect(v Value) Value { + if v.Kind() != Pointer { + return v + } + return v.Elem() +} + // arrayAt returns the i-th element of p, // an array whose elements are eltSize bytes wide. // The array pointed at by p must have at least i+1 elements: