Skip to content

spec: gotype accepts a[1.0<<s] though the compilers do not - which is correct? #14844

@momchil-velikov

Description

@momchil-velikov

The spec says (https://golang.org/ref/spec#Operators):

If the left operand of a non-constant shift expression is an untyped constant, it is first converted
to the type it would assume if the shift expression were replaced by its left operand alone.

Consider the following program (http://play.golang.org/p/LsZYy_l_hD):

package main
import "fmt"
func main() {
    s := uint(0)
    a := []int{1, 2}
    // A
    x := a[1.0]
    // B
    x = a[1.0<<s] // error
    fmt.Println(x, s)
}

According to the above part of the spec, it would seem that 1.0 in statement B should be converted to int, as it was converted in statement A, since A is what B looks like after, quote, "shift expression were replaced by its left operand alone."

However:

  • go version devel +f2772a4 Tue Mar 15 19:57:40 2016 +0000 linux/amd64 reports an error:

./shift-5.go:11: invalid operation: 1 << s (shift of type float64)

  • corresponding gotype produces no output
  • gccgo (GCC) 6.0.0 20160314 (experimental) [master revision 96e5a1e:5a53517:1bd2aefdf63c38093e950d365c5b4fa9ed4a5197]

shift-5.go:11:7: error: index must be integer
x = a[1.0<<s] // error
^
shift-5.go:11:11: error: shift of non-integer operand
x = a[1.0<<s] // error

Indeed, the spec does not require the index in non-map index-expressions to be of or convertable to int. Nonetheless, one would expect either both or none of A and B to fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions