New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: spurious "cannot use _ as value" error #15481

Open
adonovan opened this Issue Apr 28, 2016 · 10 comments

Comments

Projects
None yet
9 participants
@adonovan

adonovan commented Apr 28, 2016

go version devel +af125a5 Thu Apr 28 17:56:28 2016 +0000 linux/amd64

https://play.golang.org/p/O_WijGjKJ7

@ianlancetaylor ianlancetaylor changed the title from gc: spurious "cannot use _ as value" error to cmd/compile: spurious "cannot use _ as value" error Apr 28, 2016

@ianlancetaylor ianlancetaylor added this to the Go1.7Maybe milestone Apr 28, 2016

@rsc

This comment has been minimized.

Contributor

rsc commented May 17, 2016

It has been this way since as far back as Go 1.2. Doesn't matter for Go 1.7, and arguably low enough priority not for Go 1.8 either.

@rsc rsc modified the milestones: Unplanned, Go1.7Maybe May 17, 2016

@griesemer griesemer self-assigned this Nov 17, 2016

@griesemer griesemer modified the milestones: Go1.9, Unplanned Nov 17, 2016

@odeke-em

This comment has been minimized.

Member

odeke-em commented Nov 23, 2016

Interestingly a permutation of the code in the bug report
https://play.golang.org/p/hz5ePuCDlF

package main

type m map[string]string

var _ = map[string]string{}       // ok
var _ = map[string]string{"": ""} // ok

func main() {
	var _ = map[string]string{}           // ok
	var _ = map[string]bool{}             // ok
	var _ = map[string]string{"a": "A"}   // error: cannot use _ as value
	var _ = map[string]bool{"true": true} // error: cannot use _ as value
}

might allude to where the bug is:

  • only within a function/closure
  • objects of the same type whose left hand hand side identifier is _.

Sorry for the noise, was just marking bugs I wanna take a stab at for Go1.9.

@dmitshur

This comment has been minimized.

Member

dmitshur commented May 24, 2017

I ran into this as well in #20484.

This also affects struct literals:

package main

var _ = int(5)           // ok
var _ = struct{ int }{}  // ok
var _ = struct{ int }{5} // ok

func main() {
	_ = int(5)               // ok
	_ = struct{ int }{}      // ok
	_ = struct{ int }{5}     // ok
	var _ = int(5)           // ok
	var _ = struct{ int }{}  // ok
	var _ = struct{ int }{5} // cannot use _ as value
}

https://play.golang.org/p/xpsxF1WC8z

The common theme seems to be that it cannot be in package scope, it has to be a var _ = rather than _ =, and the composite literal must be not empty.

@griesemer

This comment has been minimized.

Contributor

griesemer commented Jun 8, 2017

Moving to 1.10. There are work-arounds if this is important (perhaps in tests) and it has been like this for a long time. Should be fixed a part of the front-end rewrite.

@ainar-g

This comment has been minimized.

Contributor

ainar-g commented Oct 2, 2017

Another interesting example I've found:

var a = map[float64]int{math.NaN(): 42} // Works.
var _ = map[float64]int{42: 42}         // Works.
var _ = map[float64]int{math.NaN(): 42} // Error.
@smasher164

This comment has been minimized.

Contributor

smasher164 commented Dec 31, 2017

Is the following behavior correct in the context of a type?

package main

type A interface {
    fn(_) // cannot use _ as value
}

func fn(v _) {} // cannot use _ as value

func main() {
    var a _ // cannot use _ as value
}

I would expect an error along the lines of "cannot use _ as type"

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Jan 2, 2018

@smasher164 Yes, the error message is fairly bad. It would be nice to get a better error message here. I'm not sure it's quite the same as the other cases in this issue, but it's similar.

@griesemer

This comment has been minimized.

Contributor

griesemer commented Jun 4, 2018

Too late for 1.11.

@griesemer griesemer modified the milestones: Go1.11, Go1.12 Jun 4, 2018

@griesemer

This comment has been minimized.

Contributor

griesemer commented Sep 5, 2018

Simpler reproducer:

package p

type E struct {
	f func()
}

var _ = E{func() {}}
@go101

This comment has been minimized.

go101 commented Oct 28, 2018

This is okay.

var _ = map[bool]int{"aaa" != "": 1, false: 0}

This is not.

var _ = map[bool]int{"aaa"[:] != "": 1, false: 0}

@griesemer griesemer modified the milestones: Go1.12, Go1.13 Dec 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment