Skip to content

cmd/compile: better error message for wrong-cased field name in composite literal #22794

@odeke-em

Description

@odeke-em

I was just working on some program in a code base that uses the suffix Id for a field,
but assigning from an struct from another codebase that uses ID instead.
For example given https://play.golang.org/p/9FAMV7mLZE

package main

type it struct {
	ID string
}

func main() {
	i1 := &it{"Foo"}
	if i1.Id != "" {
	}
	i2 := &it{Id: "Bar"}
}

on trying to compile it I get

tmp/sandbox912846952/main.go:9:7: i1.Id undefined (type *it has no field or method Id, but does have ID)
tmp/sandbox912846952/main.go:11:14: unknown field 'Id' in struct literal of type it

Notice the nice hint on line 9 i1.Id undefined (type *it has no field or method Id, but does have ID)
it would be nice(though lengthy) to give a hint on line 14 such as

tmp/sandbox000177384/main.go:11:14: unknown field 'Id' in struct literal of type it (but does have ID)

/cc compiler squad @mdempsky @griesemer @randall77 @rsc.

This is a super low priority issue that I can work on when free(or anyone else is welcome too), ideas and vetoes welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeSoonThis needs action soon. (recent regressions, service outages, unusual time-sensitive situations)help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions