Skip to content

reflect: StructOf allows fields with inconsistent export information #45277

Description

@dsnet

The StructField.PkgPath field should only be populated if the field is unexported. Currently in Go, this can only occur if the first character is an uppercase letter. Thus, an exported Name should not be allowed with PkgPath also being specified. However, this is currently permitted:

t := reflect.StructOf([]reflect.StructField{{
	Name:    "Field",                   // exported field
	PkgPath: "some.bogus/package/path", // should only be specified for unexported fields
	Type:    reflect.TypeOf(0),
}})                                         // expect this to panic, but it doesn't
f := t.Field(0)
fmt.Printf("%q.%s\n", f.PkgPath, f.Name)    // "some.bogus/package/path".Field

I expect the snippet above to panic.

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

    Status
    Triage Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions