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: strange panic message #12046

Closed
bronze1man opened this issue Aug 6, 2015 · 5 comments
Closed

runtime: strange panic message #12046

bronze1man opened this issue Aug 6, 2015 · 5 comments

Comments

@bronze1man
Copy link
Contributor

I have following program.

package main

type st string
func main() {
    s:=st("1")
    panic("a="+s)
}

http://play.golang.org/p/0DCktht8U2

I think it should not compile, but it compile and got the panic message:

panic: (main.st) (0x63660,0x1020a040)

goroutine 1 [running]:
main.main()
    /tmp/sandbox341206984/main.go:6 +0x100

goroutine 2 [runnable]:
runtime.forcegchelper()
    /usr/src/go/src/runtime/proc.go:90
runtime.goexit()
    /usr/src/go/src/runtime/asm_amd64p32.s:1086 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
    /usr/src/go/src/runtime/mgc0.go:82
runtime.goexit()
    /usr/src/go/src/runtime/asm_amd64p32.s:1086 +0x1
@cznic
Copy link
Contributor

cznic commented Aug 6, 2015

I think it should not compile...

Please discuss why it should not compile, the program seems legal to me.

... but it compile and got the panic message:

Somewhere in the runtime the stack trace printing routine attempts to output the value panic was called with. It handles predeclared types, like eg. string, and probably some others. The argument to panic is converted to an interface{}, I think the two numbers printed are the two items in the interface{} runtime struct {*typeInfo, *value}.

FTR: converting the argument to string makes the trace look more usual: http://play.golang.org/p/einqqW9zTp

@ianlancetaylor
Copy link
Member

This is working as expected. The panic messages is telling you the type followed by the value. panic handles the predeclared type "string" specially, and it handles types that implement the fmt.Stringer method specially.

@bronze1man
Copy link
Contributor Author

"why it should not compile?"
Type st should not add with string type. It looks like different types.

@ianlancetaylor
Copy link
Member

The literal "a=" is an untyped constant. This is not string + st, it's untyped + st.

http://golang.org/ref/spec#Constants

@bronze1man
Copy link
Contributor Author

Thanks. I just forgot that.

@golang golang locked and limited conversation to collaborators Aug 5, 2016
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

4 participants