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

spec: possibly inaccurate description of untyped case expressions #26313

Closed
marat-rkh opened this issue Jul 10, 2018 · 4 comments
Closed

spec: possibly inaccurate description of untyped case expressions #26313

marat-rkh opened this issue Jul 10, 2018 · 4 comments

Comments

@marat-rkh
Copy link

marat-rkh commented Jul 10, 2018

"Expression switches" section of Switch statements has the following note about untyped case expressions:

If a case expression is untyped, it is first converted to the type of the switch expression.

I have the following code that is rejected by the compiler:

package main

func main() {
	var s string = "123"
	switch s {
	case 123: // error: invalid case 123 in switch on s (mismatched types int and string)
	}
}

123 is untyped constant and it is possible to convert it to string type. This possibility is mentioned as a special case in Conversions:

As a special case, an integer constant x can be converted to a string type using the same rule as for non-constant x.

So, this makes me wonder if spec is inaccurate here and convertibility requirements for untyped case expressions should not include special cases? Sorry if I am missing something obvious.

@ianlancetaylor ianlancetaylor added Documentation NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jul 10, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jul 10, 2018
@ianlancetaylor
Copy link
Contributor

Nice example.

@rsc
Copy link
Contributor

rsc commented Sep 26, 2018

Needs fix but probably in the spec, not in the compilers. We don't want this conversion to happen automatically.

@griesemer griesemer removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Sep 27, 2018
@griesemer
Copy link
Contributor

This should definitively be fixed in the spec; ideally in the Conversions section. There are other places in the spec where it talks about (implicit) conversions, e.g., in assignments, and we don't want an implicit string conversion in those cases either. Will prepare a CL.

@gopherbot
Copy link

Change https://golang.org/cl/139099 mentions this issue: spec: distinguish between explicit and implicit conversions (clarification)

@golang golang locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants