Skip to content

cmd/compile: reflect.Value.MethodByName used as a method expression value is not seen as a reflection lookup; linker prunes the method #82047

Closed

Description

@dims

Go version

go version go1.27.1 darwin/arm64

Also go1.26.8 and tip go1.28-devel_606427cb8c (2026-10-06). Same on linux/amd64.

Output of go env in your module/workspace

GOOS=darwin
GOARCH=arm64
CGO_ENABLED=1
GOFLAGS=
GOEXPERIMENT=

What did you do?

package main

import "reflect"

type T struct{}

func (T) M() { println("M called") }

func main() {
	f := reflect.Value.MethodByName // method expression as a value
	f(reflect.ValueOf(T{}), "M").Call(nil)
}

go run .

What did you see happen?

fatal error: unreachable method called. linker bug?

go tool nm has no main.T.M; -ldflags=-dumpdep shows no <ReflectMethod> edge. Same with the method expression in a package variable, a composite literal, or a static table, and with reflect.Value.Method.

What did you expect to see?

M called. usemethod in cmd/compile/internal/walk/expr.go marks callers of MethodByName and records constant names, but only for calls. A method expression used as a value lowers to a plain function reference, so the linker never learns about the lookup. Wrapping the call in a func literal makes it work. Bound method values and reflect.Type.MethodByName are fine; the compiler emits wrappers with real calls for those.

Found while working on #72895. Fix with cmd/link dead-code fixtures ready; CL to follow.

Activity

  1. added 2 commits that reference this issue on Oct 7, 2026
    ba85320
    48de1c2
  2. thepudds commented on Oct 7, 2026

    @thepudds
    Member

    Hi @dims, sounds like you are well on your way to a fix, but in case useful, a bisect seemed to land on 6b52bfa:

      first BAD:   6b52bfa393 | 2023-09-01 | cmd/link: remove special cases for Method() and MethodByName().
      GOOD parent: e802f211b0 | 2023-09-01 | cmd/compile: special-case MethodByName(string literal) to keep the DCE enabled.
    

    CC @dominiquelefevre @cherrymui

  3. added a commit that references this issue on Oct 7, 2026
    8e65403
  4. gopherbot commented on Oct 7, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/846145 mentions this issue: cmd/compile: mark function values of reflect.Value.Method and MethodByName

  5. added a commit that references this issue on Oct 7, 2026
    3b573c4
  6. dims commented on Oct 7, 2026

    @dims
    ContributorAuthor

    @thepudds thanks for the bisect! I think i have a tiny fix, let's see if that's good enough. please do peek when you get a chance.

  7. added a commit that references this issue on Oct 8, 2026
    c574346
  8. added this to the Go1.28 milestone on Oct 9, 2026
  9. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Oct 9, 2026
  10. added a commit that references this issue on Oct 9, 2026
    d8fec91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions