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

runtime: assignment to nil map panics with string, not Error type #14965

Closed
alexhornbake opened this issue Mar 25, 2016 · 8 comments
Closed

runtime: assignment to nil map panics with string, not Error type #14965

alexhornbake opened this issue Mar 25, 2016 · 8 comments
Milestone

Comments

@alexhornbake
Copy link

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    $ go version
    go version go1.6 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    $ go env
    GOARCH="amd64"
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GO15VENDOREXPERIMENT="1"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
    CXX="g++"
    CGO_ENABLED="1"
  3. What did you do?
    Assignment to a nil map.
    http://play.golang.org/p/ES8UfghkFv
  4. What did you expect to see?
    assignment to nil map should panic with type Error as specified here:
    https://golang.org/ref/spec#Run_time_panics
  5. What did you see instead?
    assignment to nil map panics with type String
@ianlancetaylor ianlancetaylor changed the title Assignment to nil map panics with string, not Error type runtime: assignment to nil map panics with string, not Error type Mar 25, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Mar 25, 2016
@ianlancetaylor
Copy link
Contributor

There are cases like this in chan.go and select.go also, and perhaps error.go as well. In general any panic("string") in non-test code in the runtime is suspect, except for cases like panic("not reached").

@odeke-em
Copy link
Member

The only tricky thing though is that since importing errors or even fmt would result in circular imports and fail, using runtime's errorString is the way to go. However errorString's Error() method prefixes runtime error: before every message.
This will mean that the panics that will now be fixed e.g originally "assignment to entry in nil map" will now be "runtime error: assignment to entry in nil map". If this is deemed as not a compatibility problem, since this code has been in the stdlib for a while since July 2014
screen shot 2016-03-27 at 2 45 17 pm

I can spin up a CL to fix this.

@minux
Copy link
Member

minux commented Mar 27, 2016 via email

@davecheney
Copy link
Contributor

Should some of these be throw cases, not panic cases? I think most of these
are not recoverable.

On Mon, 28 Mar 2016, 10:01 Minux Ma, notifications@github.com wrote:

we can define a new string type that implements
error in runtime for those cases.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#14965 (comment)

@odeke-em
Copy link
Member

@minux we already have errorString
screen shot 2016-03-27 at 5 16 41 pm
If we defined a new string type for just these cases, it might look out of place and moreover there are already usages of errorString in runtime so that might look confusing when the only difference between the two different error strings' Error() results is the prefix "runtime: ".

@davecheney IMO I think we could do this in a separate issue as that changes the behavior as opposed to first fixing this issue and conforming to the docs that say panics from the runtime should be with Error's as defined here https://golang.org/ref/spec#Run_time_panics.

@minux
Copy link
Member

minux commented Mar 28, 2016 via email

@odeke-em
Copy link
Member

@minux thanks. In this case, we have no other option then but to define a new string type.
I just wanted to get clarification that we were on the same page.

@gopherbot
Copy link

CL https://golang.org/cl/21214 mentions this issue.

@golang golang locked and limited conversation to collaborators Apr 10, 2017
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

6 participants