Skip to content

go/types: confusing behaviour of Selection.Indirect() for Kind()==MethodVal #8353

Open

Description

@adonovan
In go/types/selection.go, the intended significance of the Indirect flag (for MethodVal)
is hard to tell from the example.  I was assuming that is means whether there are any
pointer indirections between the type of the receiver and the declared type of the
method, but that doesn't seem to explain it:

I instrumented recordSelection:

% cat sel.go 

package main

type T struct {}

func (T)f() {}
func (*T)g() {}

func main() {
        var x T
        x.f()
        x.g()

        var p *T
        p.f()
        p.g()
}

% ./ssadump sel.go
sel.go:11:2: SEL method (main.T) f() indirect=false
sel.go:12:2: SEL method (main.T) g() indirect=false
sel.go:15:2: SEL method (*main.T) f() indirect=true
sel.go:16:2: SEL method (*main.T) g() indirect=true

In the last selection, there is no actual indirection between the receiver type *T and
the method type *T, yet the indirect flag is reported as true.  (The indirect flag seems
to record only the pointerness of the receiver, which is redundant information.)

I think, by analogy with field selections, the indirect bit should be set iff there was
an implicit pointer indirection between the actual receiver type and the formal receiver
type, e.g a (T) method was called with an expression of type (*T), or in this example:

   type A struct {}
   func (*A) f() {}
   type B struct {*A}

   ... B{}.f()   // indirect, since method (*A).f 

An (A) method is called with an expression of type B.

Activity

  1. ianlancetaylor commented on Jul 10, 2014

    @ianlancetaylor
    Contributor

    Comment 1:

    Labels changed: added repo-tools, release-go1.4.

  2. griesemer commented on Jul 10, 2014

    @griesemer
    Contributor

    Comment 2:

    Status changed to Accepted.

  3. rsc commented on Sep 30, 2014

    @rsc
    Contributor

    Comment 3:

    go/types is not a released package. Unless this is affecting a released binary (namely
    godoc), this is not for any release.

    Labels changed: added release-none, removed release-go1.4.

  4. griesemer commented on Sep 30, 2014

    @griesemer
    Contributor

    Comment 4:

    It does not affect godoc. It's fine to delay this.
  5. added this to the Unplanned milestone on Apr 10, 2015
  6. changed the title [-]go/types: confusing behaviour of Selection.Indirect() for Kind()==MethodVal[/-] [+]x/tools/go/types: confusing behaviour of Selection.Indirect() for Kind()==MethodVal[/+] on Apr 14, 2015
  7. modified the milestones: Unreleased, Unplanned on Apr 14, 2015
  8. 2 remaining items

  9. griesemer commented on Dec 20, 2017

    @griesemer
    Contributor

    I'm not sure we can change this at this point. @adonovan please comment on this issue, or close otherwise. Thanks.

  10. adonovan commented on Oct 9, 2023

    @adonovan
    MemberAuthor

    I just stumbled into this again:

    type T struct{}
    
    func (*T) g(x any) any { return x }
    
    // x.g  Selection: MethodVal recv=*T obj=func (*T).g(x any) any type=func(x any) any indirect=true index=[0]
    func f(x *T, y any) any { return x.g(y) } 
    
    

    I think this is a definite bug but I agree it may be too late to change things without introducing much more subtle bugs. I'll make a doc change to Selection.

  11. gopherbot commented on Oct 9, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/533935 mentions this issue: go/types: document unfixable bug at Selection.Indirect

  12. mvdan commented on Oct 9, 2023

    @mvdan
    Member

    Is it not possible to swap the behavior depending on the language version, kinda like we're doing with range variable scopes? It might not be worth the hassle, but presumably this can't be riskier than changing the semantics of range loops.

  13. adonovan commented on Oct 9, 2023

    @adonovan
    MemberAuthor

    I would rather just add a new method and deprecate the old one. The correct behavior can be computed from the Selection alone using logic like this (from https://go.dev/cl/533156):

    // indirectSelection is like seln.Indirect() without bug #8353.
    func indirectSelection(seln *types.Selection) bool {
    	// Work around bug #8353 in Selection.Indirect when Kind=MethodVal.
    	if seln.Kind() == types.MethodVal {
    		tArg := effectiveDirectReceiver(seln)
    		if tArg == nil {
    			return true // indirect
    		}
    
    		// Does receiver arg/param pointerness match?
    		tParam := seln.Obj().Type().(*types.Signature).Recv().Type()
    		return isPointer(tArg) && !isPointer(tParam) // implicit *
    	}
    
    	return seln.Indirect()
    }
    
    // effectiveDirectReceiver returns the effective type of the method
    // receiver after all implicit field selections (but not implicit * or
    // & operations) have been applied.
    //
    // It returns nil if any implicit field selection was indirect.
    func effectiveDirectReceiver(seln *types.Selection) types.Type {
    	assert(seln.Kind() == types.MethodVal, "not MethodVal")
    	tArg := seln.Recv()
    	indices := seln.Index()
    	for _, index := range indices[:len(indices)-1] {
    		tstruct, ok := typeparams.CoreType(tArg).(*types.Struct)
    		if !ok {
    			// Not a struct: must be a pointer.
    			return nil
    		}
    		tArg = tstruct.Field(index).Type()
    	}
    	return tArg
    }
    
  14. findleyr commented on Oct 11, 2023

    @findleyr
    Contributor

    Agree that we should create a new method and deprecate the old one.

    Since we keep stumbling into this, can we elevate this issue to a proposal and get it done? I suggest IsIndirect as the new method name, by comparison with IsInterface, IsComparable, IsInteger, etc. In fact, perhaps IsIndirect is a more consistent name for this predicate anyway.

  15. adonovan commented on Oct 11, 2023

    @adonovan
    MemberAuthor

    I think that makes sense if we can get the type checker to compute the correct value of IsIndirect; I'd rather not just bolt my relatively expensive workaround onto the existing implementation. So that means it's mostly a type checker implementation task.

  16. findleyr commented on Oct 11, 2023

    @findleyr
    Contributor

    I'm sure the type checker can compute the correct value and store it in types.Selection (modulo other bugs like #51592). I can implement.

    Is there any objection to making this a proposal, so that we can finally close this bug?

  17. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Jul 13, 2024
  18. mateusz834 commented on Apr 30, 2026

    @mateusz834
    Member

    Just want to point out that we have GODEBUGs now and could be leveraged here not to require new API (obviously such GODEBUG would require a proposal).

  19. mateusz834 commented on Apr 30, 2026

    @mateusz834
    Member

    In the last selection, there is no actual indirection between the receiver type *T and
    the method type *T, yet the indirect flag is reported as true. (The indirect flag seems
    to record only the pointerness of the receiver, which is redundant information.)

    Not only that but this is also wrong: sel.go:12:2: SEL method (main.T) g() indirect=false from #8353 (comment) So i think that the godoc mention of this bug is still wrong.

    // Beware: Indirect spuriously returns true (Go issue #8353) for a
    // MethodVal selection in which the receiver argument and parameter
    // both have type *T so there is no indirection.
    // Unfortunately, a fix is too risky.

    CC @adonovan

  20. adonovan commented on May 1, 2026

    @adonovan
    MemberAuthor

    we have GODEBUGs now

    This is too obscure to be worth a GODEBUG. We should just add a new method and deprecate the old one.

    Not only that but this is also wrong: sel.go:12:2: SEL method (main.T) g() indirect=false

    I think it's correct: no loads through pointers were involved. There was an implicit insertion of & though.

    Perhaps it would be more useful if the new method returned one of three values, something like +1 for *x, 0 for x, and -1 for &x. We (@mateusz834 perhaps?) should sketch this out in typesinternal before we propose new API.

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

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions