Skip to content

reflect: cannot call *T methods on addressable Values of type T #24184

Description

@mdempsky

I would expect this program to work:

package main

import "reflect"

type T int
func (*T) M() {}

func main() {
        var t T
        v := reflect.ValueOf(&t).Elem()
        v.MethodByName("M").Call(nil)
}

Currently, it produces panic: reflect: call of reflect.Value.Call on zero Value panic, because M is not in the method set of T (only *T).

This seems overly strict to me. The Go spec allows calling t.M() where t is an addressable value of type T; it's just implicitly executed as (&t).M(). I would expect package reflect to handle this implicit dereference, but it does not.

For comparison, the spec also allows an implicit dereference to call value-receiver methods on pointer types, and package reflect does perform this implicit dereference.

/cc @ianlancetaylor @dsnet

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions