Skip to content

cmd/compile: unnecessary bounds check when indexing slice from range #71439

Closed as not planned
@dsnet

Description

@dsnet

Go version

go1.23

Output of go env in your module/workspace:

GOARCH=amd64
GOOS=linux

What did you do?

Compile the following:

package main

import "archive/tar"

var src []tar.Header
var dst *tar.Header

func main() {
	for i := range src {
		dst = &src[i] // line 10
	}
}

What did you see happen?

I see this compiled out:

...
0x0069 00105 (main.go:10)	PCDATA	$1, $0
0x0069 00105 (main.go:10)	CALL	runtime.panicIndex(SB)
0x006e 00110 (main.go:10)	XCHGL	AX, AX
...

What did you expect to see?

No such call to runtime.panicIndex. The slice is indexed from an iteration integer that is provably bounded by the length of src itself. The only possible way a panic occurs is if src is mutated during the iteration (or asynchronously in another goroutine, in which case there must be synchronization primitives).

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions