Skip to content

spec: clarify type elision rules for composite literals #17954

Closed
@griesemer

Description

@griesemer

Per the spec ( https://golang.org/ref/spec#Composite_literals ):

"Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T."

Given this paragraph, it's not obviously clear if the following code should be permitted:

type T *struct {
  s string
}

var _ = []T{
  { "foo" },
}

Here, the element type is a named type T that happens to be a *S. From the phrasing in the spec, it's not obvious whether the rule also applies in this case, or not (it doesn't explicitly say whether the underlying type is meant or not).

The gc and gccgo compilers accept the code above, go/types does not.

In contrast, the code below is also accepted by go/types (and clearly satisfies the spec rule).

var _ = []*struct {
  s string
}{
  { "foo" },
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions