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

Why slice not painc #42069

Closed
georgehao opened this issue Oct 19, 2020 · 1 comment
Closed

Why slice not painc #42069

georgehao opened this issue Oct 19, 2020 · 1 comment

Comments

@georgehao
Copy link

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

$ go version
go version go1.15.2 darwin/amd64

Does this issue reproduce with the latest release?

it seem all the version the same result

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/usr/local/bin/"
GOCACHE="/Users/haohongfan/Library/Caches/go-build"
GOENV="/Users/haohongfan/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/haohongfan/go/pkg/mod"
GONOPROXY="git.zuoyebang.cc"
GONOSUMDB="git.zuoyebang.cc"
GOOS="darwin"
GOPATH="/Users/haohongfan/go"
GOPRIVATE="git.zuoyebang.cc"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/Users/haohongfan/sdk/go1.15.2"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/haohongfan/sdk/go1.15.2/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/haohongfan/goproject/test/go.mod"
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/9q/mcmf4t9x6qgf9bc4x83nvl5m0000gn/T/go-build978115702=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

func a() []int {
	a1 := []int{1}
	a2 := a1[1:]
	return a2
}

func main() {
	a()
}

I also get the asm

"".a STEXT size=87 args=0x18 locals=0x18
	0x0000 00000 (main.go:3)	TEXT	"".a(SB), ABIInternal, $24-24
	0x0000 00000 (main.go:3)	MOVQ	(TLS), CX
	0x0009 00009 (main.go:3)	CMPQ	SP, 16(CX)
	0x000d 00013 (main.go:3)	PCDATA	$0, $-2
	0x000d 00013 (main.go:3)	JLS	80
	0x000f 00015 (main.go:3)	PCDATA	$0, $-1
	0x000f 00015 (main.go:3)	SUBQ	$24, SP
	0x0013 00019 (main.go:3)	MOVQ	BP, 16(SP)
	0x0018 00024 (main.go:3)	LEAQ	16(SP), BP
	0x001d 00029 (main.go:3)	FUNCDATA	$0, gclocals·2a5305abe05176240e61b8620e19a815(SB)
	0x001d 00029 (main.go:3)	FUNCDATA	$1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x001d 00029 (main.go:4)	LEAQ	type.[1]int(SB), AX
	0x0024 00036 (main.go:4)	MOVQ	AX, (SP)
	0x0028 00040 (main.go:4)	PCDATA	$1, $0
	0x0028 00040 (main.go:4)	CALL	runtime.newobject(SB)
	0x002d 00045 (main.go:4)	MOVQ	8(SP), AX
	0x0032 00050 (main.go:4)	MOVQ	$1, (AX)
	0x0039 00057 (main.go:6)	MOVQ	AX, "".~r0+32(SP)
	0x003e 00062 (main.go:6)	XORPS	X0, X0
	0x0041 00065 (main.go:6)	MOVUPS	X0, "".~r0+40(SP)
	0x0046 00070 (main.go:6)	MOVQ	16(SP), BP
	0x004b 00075 (main.go:6)	ADDQ	$24, SP
	0x004f 00079 (main.go:6)	RET
	0x0050 00080 (main.go:6)	NOP
	0x0050 00080 (main.go:3)	PCDATA	$1, $-1
	0x0050 00080 (main.go:3)	PCDATA	$0, $-2
	0x0050 00080 (main.go:3)	CALL	runtime.morestack_noctxt(SB)
	0x0055 00085 (main.go:3)	PCDATA	$0, $-1
	0x0055 00085 (main.go:3)	JMP	0
	0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 76 41 48  eH..%....H;a.vAH
	0x0010 83 ec 18 48 89 6c 24 10 48 8d 6c 24 10 48 8d 05  ...H.l$.H.l$.H..
	0x0020 00 00 00 00 48 89 04 24 e8 00 00 00 00 48 8b 44  ....H..$.....H.D
	0x0030 24 08 48 c7 00 01 00 00 00 48 89 44 24 20 0f 57  $.H......H.D$ .W
	0x0040 c0 0f 11 44 24 28 48 8b 6c 24 10 48 83 c4 18 c3  ...D$(H.l$.H....
	0x0050 e8 00 00 00 00 eb a9                             .......
	rel 5+4 t=17 TLS+0
	rel 32+4 t=16 type.[1]int+0
	rel 41+4 t=8 runtime.newobject+0
	rel 81+4 t=8 runtime.morestack_noctxt+0

See here, i just not check the out of index

	0x002d 00045 (main.go:4)	MOVQ	8(SP), AX
	0x0032 00050 (main.go:4)	MOVQ	$1, (AX)
	0x0039 00057 (main.go:6)	MOVQ	AX, "".~r0+32(SP)

What did you expect to see?

panic

What did you see instead?

empty

@randall77
Copy link
Contributor

This is intentional. The upper bound for slicing (as opposed to indexing) includes equality. Please read https://golang.org/ref/spec#Slice_expressions

@golang golang locked and limited conversation to collaborators Oct 19, 2021
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

3 participants