Skip to content

spec: shift operations are underspecified or inconsistent #77117

Closed

Description

@griesemer

Per the spec, the following rule holds for shifts:

The right operand in a shift expression must have integer type [Go 1.13] or be an untyped constant representable by a value of type uint.

The following shift expression is permitted and doesn't follow this rule:

var s int = 100
var x = 1 << (1 << s)

The RHS of the (outer) shift expression is (1 << s). It does not have integer type because the type of 1 depends on context and the RHS of a shift doesn't imply a type (only that its value be representable as a uint). The RHS is also not constant because s is not constant. And finally, 1 << s for s == 100 does not fit into a uint value. There is no run-time panic in this case: 1 << s silently overflows to 0 and then the value of x is 1 (playground).

A non-constant RHS of a shift expression may itself be untyped as is the case here. In short, untyped values (not just constants) of integer kind may appear "transiently" when evaluating shifts at runtime. (The only other non-constant untyped values that are possible are boolean results of comparisons).

The prose in the spec needs to be adjusted to accommodate for these cases.

Activity

  1. self-assigned this
    on Jan 8, 2026
  2. griesemer commented on Jan 8, 2026

    @griesemer
    ContributorAuthor
  3. changed the title [-]spec: shift operation are underspecified[/-] [+]spec: shift operations are underspecified[/+] on Jan 8, 2026
  4. added this to the Go1.27 milestone on Jan 8, 2026
  5. added
    LanguageProposalIssues describing a requested change to the Go language specification.
    on Jan 8, 2026
  6. robpike commented on Jan 8, 2026

    @robpike
    Contributor

    Not this again. :)

  7. modified the milestones: Go1.27, Go1.28 on May 27, 2026
  8. gopherbot commented on Aug 13, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/814580 mentions this issue: go/ssa: allow transient untyped integer values

  9. griesemer commented on Aug 14, 2026

    @griesemer
    ContributorAuthor

    Here's a more systematic analysis of the example. To start with, shifts are governed by the following primary rules per the spec:

    A) The right operand in a shift expression must have integer type [Go 1.13] or be an untyped constant representable by a value of type uint.

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

    C) If the left operand of a constant shift expression is an untyped constant, the result is an integer constant; otherwise it is a constant of the same type as the left operand, which must be of integer type.

    D) The shift operators shift the left operand by the shift count specified by the right operand, which must be non-negative.

    E) If the shift count is negative at run time, a run-time panic occurs.

    F) The shift operators implement arithmetic shifts if the left operand is a signed integer and logical shifts if it is an unsigned integer.

    (Since we don't have any constant shifts, we won't need rule C here.)

    We can split the initialization expression in var x = 1 << (1 << s) into its subexpressions. We call the left and right operands L and R and number them by level (L0 = left operand at level 0, etc.):

    Level 0 (top level):
    L0: 1
    R0: 1 << s

    Level 1 (one level down):
    L1: 1
    R1: s

    L0 and L1 are untyped constants. R0 is a non-constant shift, and R1 is an int. Analyzing top-down we get:

    1. L0 is an untyped constant, the shift is non-constant, so per B) L0 is converted to the type if would assume if it were alone (as in var x = 1). Thus, L0 assumes the type int (default type for 1).

    2. Per A), R0 must have integer type (it is not an untyped constant). Per E) is must not be negative at run-time.

    Going one level down:

    1. L1 is an untyped constant, the shift is non-constant, so per B) L1 is converted to the type if would assume if it were alone (in 1 << s which is R0). We concluded that R0 must have integer type (see 2), so L1 must have integer type.

    2. Per A), R1 must have integer type, which is true (R1 is of type int).

    For the compiler to be able to generate code for the Level 1 shift, we must know a bit more about L1 than it "must have integer type". Specifically, we must have a concrete integer type that is permissible on the LHS of a non-constant shift (for Level 1), which then produces a result of the same type which is permissible on the RHS of a non-constant shift (for Level 0).

    There are many integer types to choose from but we probably want a type that provides enough range while not making things too complex. Since we need an integer type because of context, we cannot just go with L1's default type because L1 might be a valid floating-point number: 1 << (1.0 << s) is permitted, and the default type of 1.0 is float64 which wouldn't work with <<. But we can select the default type for integers, int. More precisely, we can probably say that L1 must be representable as a value of type int. If L1 is too large, we can get a compile-time error as desired.

    Alternatives:

    Why not (representable as a) uint? The shift count (1 << s) must not be negative per E), and a negative L1 will almost always lead to a negative result in (L1 << s) (or (L1 >> s), unless it's << and s large enough to produce a 0 result). If we require L1 to fit into a uint, we'd get a compile-time error rather than run-time panic if L1 is negative in this case (because then R0 will be negative), which may be desirable. But we want the rule for L1 apply generally. Consider R1 = (-1 << s + c) for some suitable value c and then the uint requirement is too restrictive.

    Why not int64? That would be a possibility but given that shift counts are small integers, a 64bit shift count doesn't make a lot of sense. Also, it would be different from other similar rules in the spec (e.g., for indices, which must fit into a value of type int, and not int64).

    For the same reasons, none of the other integer type choices make a lot of sense here.

  10. griesemer commented on Aug 19, 2026

    @griesemer
    ContributorAuthor

    Follow-up: there's an alternative interpretation which assumes that shifts are in fact fully specified.

    Per rule B) (see comment), in 1 << (1 << s)) the left operand of the non-constant shift (1 << s) is converted into the type it would assume if there was no shift. That is, the inner 1 would assume the type it had in 1 << (1) (inner shift removed). Per rule A), the right operand in a shift, if an untyped constant, must be representable as a uint. From this would follow that the inner 1 must be representable as a uint.

    By this reasoning, we can conclude that the inner 1 must be representable as a uint. But the current implementation disagrees:
    1 << (-1 << s) leads to a run-time panic (playground), not a compiler error that -1 doesn't fit into a uint.

  11. changed the title [-]spec: shift operations are underspecified[/-] [+]spec: shift operations are underspecified or inconsistent[/+] on Aug 19, 2026
  12. griesemer commented on Oct 9, 2026

    @griesemer
    ContributorAuthor

    CL 847765 opts for the latter interpretation: in 1 << (2 << s)), the 2 must be representable as a uint.

  13. gopherbot commented on Oct 9, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/847765 mentions this issue: go/types, types2: report error for oversized non-constant shift counts

  14. griesemer commented on Oct 9, 2026

    @griesemer
    ContributorAuthor

    Leaving open for clarification in the spec (additional examples).

  15. gopherbot commented on Nov 11, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/862481 mentions this issue: spec: clarify shifts whose right operand is itself a non-constant shift

  16. added a commit that references this issue on Nov 11, 2026
    6b08245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

DocumentationIssues describing a change to documentation.LanguageProposalIssues describing a requested change to the Go language specification.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions