Skip to content
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

Closed
adonovan opened this issue Apr 28, 2016 · 11 comments
Closed

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

adonovan opened this issue Apr 28, 2016 · 11 comments
Labels
early-in-cycle A change that should be done early in the 3 month dev cycle. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@adonovan
Copy link
Member

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 gc: spurious "cannot use _ as value" error cmd/compile: spurious "cannot use _ as value" error Apr 28, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7Maybe milestone Apr 28, 2016
@rsc
Copy link
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
Copy link
Member

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
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.

@griesemer griesemer modified the milestones: Go1.10, Go1.11 Nov 29, 2017
@griesemer griesemer added early-in-cycle A change that should be done early in the 3 month dev cycle. NeedsFix The path to resolution is known, but the work has not been done. labels Nov 29, 2017
@smasher164
Copy link
Member

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
Copy link
Contributor

@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
Copy link
Contributor

Too late for 1.11.

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

Simpler reproducer:

package p

type E struct {
	f func()
}

var _ = E{func() {}}

@go101
Copy link

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
Copy link
Contributor

These errors are gone with Go 1.18. Closing.

@dmitshur dmitshur modified the milestones: Backlog, Go1.18 Mar 18, 2022
@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
early-in-cycle A change that should be done early in the 3 month dev cycle. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

10 participants