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

cmd/compile: compiler panics on key-element slice literal with float index #16439

Closed
ALTree opened this issue Jul 20, 2016 · 9 comments
Closed
Milestone

Comments

@ALTree
Copy link
Member

ALTree commented Jul 20, 2016

go version devel +883e128 Wed Jul 20 05:08:37 2016 +0000 linux/amd64

The compiler knows that you must use an integer constant as an index for a key-element literal, and the error message is fine when a negative index is used:

package main

func main() {
    _ = []int{-1: 3}
}
./prova.go:4: index must be non-negative integer constant

but the compiler panics when a float is used:

package main

func main() {
    _ = []int{-1.0: 3}
}
# command-line-arguments
panic: interface conversion: interface {} is *gc.Mpflt, not *gc.Mpint

goroutine 1 [running]:
panic(0x8b5cc0, 0xc4204109c0)
    /home/alberto/go/src/runtime/panic.go:500 +0x1a1
cmd/compile/internal/gc.nonnegconst(0xc42041e510, 0x2)
    /home/alberto/go/src/cmd/compile/internal/gc/const.go:1494 +0x17a
cmd/compile/internal/gc.typecheckcomplit(0xc42041e3f0, 0x0)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:2906 +0x34f
cmd/compile/internal/gc.typecheck1(0xc42041e3f0, 0x2, 0xc42001d3b0)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:823 +0x57e8
cmd/compile/internal/gc.typecheck(0xc42041e3f0, 0x2, 0xc42001d3b0)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:188 +0x654
cmd/compile/internal/gc.typecheckas(0xc42041e750)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:3210 +0xa9
cmd/compile/internal/gc.typecheck1(0xc42041e750, 0x1, 0xb07570)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:1952 +0x3bee
cmd/compile/internal/gc.typecheck(0xc42041e750, 0x1, 0x0)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:188 +0x654
cmd/compile/internal/gc.typecheckslice(0xc42002e758, 0x1, 0x1, 0x1)
    /home/alberto/go/src/cmd/compile/internal/gc/typecheck.go:49 +0x57
cmd/compile/internal/gc.Main()
    /home/alberto/go/src/cmd/compile/internal/gc/main.go:389 +0x178d
cmd/compile/internal/amd64.Main()
    /home/alberto/go/src/cmd/compile/internal/amd64/galign.go:93 +0x2fa
main.main()
    /home/alberto/go/src/cmd/compile/main.go:33 +0x2a3
@ALTree ALTree changed the title cmd/compile: compiler panic on key-element slice literal with float index cmd/compile: compiler panics on key-element slice literal with float index Jul 20, 2016
@odeke-em
Copy link
Member

I've sent a CL https://go-review.googlesource.com/#/c/25081.

@gopherbot
Copy link

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

@ALTree
Copy link
Member Author

ALTree commented Jul 20, 2016

That was fast; I opened the issue to provide a reference to a patch I wanted to mail (it's CL 25090, gobot didn't reference it here since I closed it with R=go1.8).

Well, we can now choose between two different fixes.

@ianlancetaylor ianlancetaylor added this to the Go1.8 milestone Jul 20, 2016
@josharian
Copy link
Contributor

FWIW, I prefer CL 25090.

nonnegconst could use a bit of cleanup, but that's unrelated...and that's true for much of the compiler.

@odeke-em
Copy link
Member

Thanks @ALTree. @josharian aye aye, yap I now see that too, CL 25090 is smaller, less invasive and will prevent other crashes that slip through in that check. I considered one like it but thought I could somewhat improve the message since 'non-negative integer' seems like a catch all statement IMO but it might just be sufficient.

@josharian
Copy link
Contributor

@odeke-em yep, I think it's sufficient. I'm thrilled to see more folks working on the compiler, though. Please keep sending CLs!

@odeke-em
Copy link
Member

Cool, I'll close my CL in favor of 25090 and we can move work to it. Awesome, I look forward to doing work on the compiler.

@griesemer
Copy link
Contributor

For the record, a positive floating-point constant representable as an integer (e.g., 2.0) is permissible. Same for complex numbers representable as an integer (e.g., 2.0 + 0i). See my comments on https://go-review.googlesource.com/#/c/25090 .

@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Sep 26, 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