Skip to content
New issue

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

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

Already on GitHub? # to your account

Unexpected execution flow when overshadowing error-type with *Error (both nil) #26000

Closed
kristoiv opened this issue Jun 21, 2018 · 1 comment
Closed

Comments

@kristoiv
Copy link

kristoiv commented Jun 21, 2018

What version of Go are you using (go version)?

go version go1.10.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kristoffer/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kristoffer/Go/"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dd/n8x7kkd92wg3ksv044l6wjh40000gn/T/go-build284967747=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"os"
)

type CustomError struct {
	Err string
}

func (ce CustomError) Error() string {
	return ce.Err
}

func SomeFunc() (string, *CustomError) {
	return "hello", nil
}

func main() {
	// Do something that creates a variable err of type error
	_, err := os.Open("/")
	if err != nil {
		panic(err)
	}

	// Then replace the err type with *CustomError
	val, err := SomeFunc()
	if err != nil {
		panic(err)
	}

	fmt.Println("No problem", val)
}

What did you expect to see?

No problem hello

What did you see instead?

fatal error: panic while printing panic value

goroutine 1 [running]:
runtime.throw(0x10c2e93, 0x20)
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/panic.go:616 +0x81 fp=0xc42005bbb0 sp=0xc42005bb90 pc=0x1026051
runtime.preprintpanics.func1()
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/panic.go:395 +0x58 fp=0xc42005bbd8 sp=0xc42005bbb0 pc=0x104a318
runtime.call32(0x0, 0x10c59b8, 0x0, 0x0)
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/asm_amd64.s:573 +0x3b fp=0xc42005bc08 sp=0xc42005bbd8 pc=0x104b8bb
panic(0x10a58c0, 0xc42008c1c0)
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/panic.go:502 +0x229 fp=0xc42005bca8 sp=0xc42005bc08 pc=0x1025a19
runtime.panicwrap()
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/error.go:154 +0x2aa fp=0xc42005bdf0 sp=0xc42005bca8 pc=0x10059aa
main.(*CustomError).Error(0x0, 0x10a4420, 0x0)
:1 +0x47 fp=0xc42005be00 sp=0xc42005bdf0 pc=0x108da47
runtime.preprintpanics(0xc42005bee0)
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/panic.go:401 +0xb1 fp=0xc42005be78 sp=0xc42005be00 pc=0x10255b1
panic(0x10a4420, 0x0)
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/panic.go:543 +0x38d fp=0xc42005bf18 sp=0xc42005be78 pc=0x1025b7d
main.main()
/Users/kristoffer/Go/src/git.noisolation.com/ev2/bluetoothtest/go/cmd/test4/main.go:30 +0x116 fp=0xc42005bf88 sp=0xc42005bf18 pc=0x108d976

@kristoiv
Copy link
Author

The error type can contain a value which is nil, without being nil itself. Overshadowing doesn't work in the local scope so it was instead assigned.

Closing the issue.

Reference:
https://golang.org/doc/faq#nil_error

@golang golang locked and limited conversation to collaborators Jun 21, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

2 participants