Skip to content

reflect: method lookup using anonymous field succeeds, but Call crashes #451

@gopherbot

Description

@gopherbot

by Ostsol:

What steps will reproduce the problem?

package main

import (
    "fmt";
    "reflect"
)

type A struct { 
    num int; 
    str string 

} 

type B struct { 
    A 
} 

func (self *A) Set (num int, str string) { 
    self.num = num; 
    self.str = str 
}

func CallSet (val interface{}, num int, str string) { 
    vptr := reflect.NewValue (val).(*reflect.PtrValue); 
    mthd := vptr.Method (0); 

    params := []reflect.Value{reflect.NewValue (num), reflect.NewValue 
(str)}; 
    mthd.Call (params) 
}

func main () {
    b := new (B);
    CallSet (b, 4, "hi");
    fmt.Printf ("%d %s\n", b.num, b.str)
}

What is the expected output? What do you see instead?

This program should output the same as if one called 'b.Set (4, "hi")':

4 "hi"

Instead it produces a runtime panic, declaring:

type mismatch: *main.A != *main.B

What is your $GOOS?  $GOARCH?

linux AMD64

Which revision are you using?  (hg identify)

84c2d8accf42+ tip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions