Skip to content

reflect: field lookup ignores methods that cancel fields (ambiguity is ignored) #5706

Description

@griesemer
1) $ cat x.go
package main

import "reflect"

type S1 struct {
    m int
}

type S2 struct{}

func (S2) m() int { return 0 }

type S struct {
    S1
    S2
}

func main() {
    var s S
    t := reflect.TypeOf(s)
    if f, ok := t.FieldByName("m"); ok {
        println("field found:", f.Name)
    } else {
        println("field not found")
    }
}


2) $ go run x.go
field found: m


But in truth, the field m and the method m are at the same level and thus "cancel
each other out". In the same program, accessing s.m leads to a compile-time error (
ambiguous selector s.m ).

The bug is in http://golang.org/src/pkg/reflect/type.go?#L856
(structType.FieldByNameFunc) which ignores methods.

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