Skip to content

Commit 32129bf

Browse files
committed
go/internal/gcimporter: adjust importer to match compiler importer
This is a port of CL 288632 to x/tools/go/internal/gcimporter. This logic was ostensibly unported to avoid breaking build compatibility with go1.12. Now that gopls no longer support 1.12, It is safe to make this change. Fixes golang/go#53803 Change-Id: Ic9b4d7a60511076a83d8fa72cf7c4a3bdcab3fce Reviewed-on: https://go-review.googlesource.com/c/tools/+/417580 Reviewed-by: Jamal Carvalho <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent 22d1494 commit 32129bf

File tree

1 file changed

+20
-40
lines changed

1 file changed

+20
-40
lines changed

go/internal/gcimporter/iimport.go

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go/token"
1818
"go/types"
1919
"io"
20+
"math/big"
2021
"sort"
2122
"strings"
2223

@@ -512,7 +513,9 @@ func (r *importReader) value() (typ types.Type, val constant.Value) {
512513
val = constant.MakeString(r.string())
513514

514515
case types.IsInteger:
515-
val = r.mpint(b)
516+
var x big.Int
517+
r.mpint(&x, b)
518+
val = constant.Make(&x)
516519

517520
case types.IsFloat:
518521
val = r.mpfloat(b)
@@ -561,8 +564,8 @@ func intSize(b *types.Basic) (signed bool, maxBytes uint) {
561564
return
562565
}
563566

564-
func (r *importReader) mpint(b *types.Basic) constant.Value {
565-
signed, maxBytes := intSize(b)
567+
func (r *importReader) mpint(x *big.Int, typ *types.Basic) {
568+
signed, maxBytes := intSize(typ)
566569

567570
maxSmall := 256 - maxBytes
568571
if signed {
@@ -581,7 +584,8 @@ func (r *importReader) mpint(b *types.Basic) constant.Value {
581584
v = ^v
582585
}
583586
}
584-
return constant.MakeInt64(v)
587+
x.SetInt64(v)
588+
return
585589
}
586590

587591
v := -n
@@ -591,47 +595,23 @@ func (r *importReader) mpint(b *types.Basic) constant.Value {
591595
if v < 1 || uint(v) > maxBytes {
592596
errorf("weird decoding: %v, %v => %v", n, signed, v)
593597
}
594-
595-
buf := make([]byte, v)
596-
io.ReadFull(&r.declReader, buf)
597-
598-
// convert to little endian
599-
// TODO(gri) go/constant should have a more direct conversion function
600-
// (e.g., once it supports a big.Float based implementation)
601-
for i, j := 0, len(buf)-1; i < j; i, j = i+1, j-1 {
602-
buf[i], buf[j] = buf[j], buf[i]
603-
}
604-
605-
x := constant.MakeFromBytes(buf)
598+
b := make([]byte, v)
599+
io.ReadFull(&r.declReader, b)
600+
x.SetBytes(b)
606601
if signed && n&1 != 0 {
607-
x = constant.UnaryOp(token.SUB, x, 0)
602+
x.Neg(x)
608603
}
609-
return x
610604
}
611605

612-
func (r *importReader) mpfloat(b *types.Basic) constant.Value {
613-
x := r.mpint(b)
614-
if constant.Sign(x) == 0 {
615-
return x
616-
}
617-
618-
exp := r.int64()
619-
switch {
620-
case exp > 0:
621-
x = constant.Shift(x, token.SHL, uint(exp))
622-
// Ensure that the imported Kind is Float, else this constant may run into
623-
// bitsize limits on overlarge integers. Eventually we can instead adopt
624-
// the approach of CL 288632, but that CL relies on go/constant APIs that
625-
// were introduced in go1.13.
626-
//
627-
// TODO(rFindley): sync the logic here with tip Go once we no longer
628-
// support go1.12.
629-
x = constant.ToFloat(x)
630-
case exp < 0:
631-
d := constant.Shift(constant.MakeInt64(1), token.SHL, uint(-exp))
632-
x = constant.BinaryOp(x, token.QUO, d)
606+
func (r *importReader) mpfloat(typ *types.Basic) constant.Value {
607+
var mant big.Int
608+
r.mpint(&mant, typ)
609+
var f big.Float
610+
f.SetInt(&mant)
611+
if f.Sign() != 0 {
612+
f.SetMantExp(&f, int(r.int64()))
633613
}
634-
return x
614+
return constant.Make(&f)
635615
}
636616

637617
func (r *importReader) ident() string {

0 commit comments

Comments
 (0)