You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
spec: shift operations are underspecified or inconsistent #77117
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:
varsint=100varx=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.
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:
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).
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:
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.
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.
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.
Activity
griesemer commentedon Jan 8, 2026
cc: @randall77 @ianlancetaylor @adonovan
[-]spec: shift operation are underspecified[/-][+]spec: shift operations are underspecified[/+]gabyhelp commentedon Jan 8, 2026
Related Issues
Related Code Changes
Related Discussions
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
robpike commentedon Jan 8, 2026
Not this again. :)
gopherbot commentedon Aug 13, 2026
Change https://go.dev/cl/814580 mentions this issue:
go/ssa: allow transient untyped integer valuesgriesemer commentedon Aug 14, 2026
Here's a more systematic analysis of the example. To start with, shifts are governed by the following primary rules per the spec:
(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:
1R0:
1 << sLevel 1 (one level down):
L1:
1R1:
sL0 and L1 are untyped constants. R0 is a non-constant shift, and R1 is an
int. Analyzing top-down we get: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 typeint(default type for1).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:
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 << swhich is R0). We concluded that R0 must have integer type (see 2), so L1 must have integer type.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 of1.0is 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 typeint. 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<<andslarge enough to produce a 0 result). If we require L1 to fit into auint, 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 valuecand then theuintrequirement 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 typeint, and notint64).For the same reasons, none of the other integer type choices make a lot of sense here.
griesemer commentedon Aug 19, 2026
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 inner1would assume the type it had in1 << (1)(inner shift removed). Per rule A), the right operand in a shift, if an untyped constant, must be representable as auint. From this would follow that the inner1must be representable as auint.By this reasoning, we can conclude that the inner
1must be representable as auint. But the current implementation disagrees:1 << (-1 << s)leads to a run-time panic (playground), not a compiler error that-1doesn't fit into auint.[-]spec: shift operations are underspecified[/-][+]spec: shift operations are underspecified or inconsistent[/+]griesemer commentedon Oct 9, 2026
CL 847765 opts for the latter interpretation: in
1 << (2 << s)), the2must be representable as auint.gopherbot commentedon Oct 9, 2026
Change https://go.dev/cl/847765 mentions this issue:
go/types, types2: report error for oversized non-constant shift countsgriesemer commentedon Oct 9, 2026
Leaving open for clarification in the spec (additional examples).
go/types, types2: report error for oversized non-constant shift counts
gopherbot commentedon Nov 11, 2026
Change https://go.dev/cl/862481 mentions this issue:
spec: clarify shifts whose right operand is itself a non-constant shiftspec: clarify shifts whose right operand is itself a non-constant shift