Skip to content

cmd/compile: "cannot use _ as value" errors initializing blank fields in struct literal #38905

Closed

Description

@mdempsky

Following up on #38690, this code is valid and accepted by go/types, but cmd/compile fails to accept it:

package p

type t struct{ _ u }
type u [10]int

func f(x int) t   { return t{u{1 / x, 1 % x}} }
func g(p *int) t  { return t{u{*p}} }
func h(s []int) t { return t{u{s[0]}} }

These are expressions where candiscard(value) returns false.

Note that only one error message is printed, but that's because we hit a Fatalf after the error (use go tool compile -d panic to see it). You can also comment out some of the functions or shuffle their order to see that any of them alone triggers the failure.

Fails to compile in Go 1.14 too, so not a regression.

/cc @cuonglm

Activity

  1. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on May 6, 2020
  2. added this to the Go1.16 milestone on May 6, 2020
  3. mdempsky commented on May 6, 2020

    @mdempsky
    ContributorAuthor

    It looks like one possible fix is to have splitnode return nblank whenever var_ == nblank.

  4. mdempsky commented on May 6, 2020

    @mdempsky
    ContributorAuthor

    Slightly better test case that provides better coverage of splitnode:

    package p
    
    type t struct{ _ u }
    type u [10]struct{ f int }
    
    func f(x int) t   { return t{u{{1 / x}, {1 % x}}} }
    func g(p *int) t  { return t{u{{*p}}} }
    func h(s []int) t { return t{u{{s[0]}}} }
    
  5. gopherbot commented on May 6, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/232617 mentions this issue: cmd/compile: another fix initializing blank fields in struct literal

  6. locked and limited conversation to collaborators on Aug 15, 2021
  7. added a commit that references this issue on Feb 17, 2026
    0031fa8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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