Skip to content

Commit 97c51a2

Browse files
committed
go/analysis/passes/shift: support constants in float syntax
Adds support for integer constants expressed in float syntax '2.0'. Also fixes the same issue in gopls/internal/golang/hover.go. Fixes golang/go#65939 Change-Id: Ic42b4cc7c6aedf7f115e3195044f06001cb6fb12 Reviewed-on: https://go-review.googlesource.com/c/tools/+/567115 TryBot-Result: Gopher Robot <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Run-TryBot: Tim King <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 509ff8b commit 97c51a2

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

go/analysis/passes/shift/shift.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ func checkLongShift(pass *analysis.Pass, node ast.Node, x, y ast.Expr) {
8989
if v == nil {
9090
return
9191
}
92-
amt, ok := constant.Int64Val(v)
92+
u := constant.ToInt(v) // either an Int or Unknown
93+
amt, ok := constant.Int64Val(u)
9394
if !ok {
9495
return
9596
}

go/analysis/passes/shift/testdata/src/a/a.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,8 @@ func ShiftDeadCode() {
153153
_ = i << 64 // want "too small for shift"
154154
}
155155
}
156+
157+
func issue65939() {
158+
a := 1
159+
println(a << 2.0)
160+
}

gopls/internal/golang/hover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ func objectString(obj types.Object, qf types.Qualifier, declPos token.Pos, file
873873
case *types.Named:
874874
// Try to add a formatted duration as an inline comment.
875875
pkg := typ.Obj().Pkg()
876-
if pkg.Path() == "time" && typ.Obj().Name() == "Duration" {
876+
if pkg.Path() == "time" && typ.Obj().Name() == "Duration" && obj.Val().Kind() == constant.Int {
877877
if d, ok := constant.Int64Val(obj.Val()); ok {
878878
comment = time.Duration(d).String()
879879
}

0 commit comments

Comments
 (0)