Skip to content

Commit fc8b4ca

Browse files
committed
go/internal/gcimporter: stub support importing/exporting constant kind
Add placeholder support for importing/exporting constant kind in the Go 1.18 export data format. Also eliminate the redundant iimport.iexportVersion field. For golang/go#45837 Change-Id: I94dbcadde0fae55788ce1a139a2760276f644630 Reviewed-on: https://go-review.googlesource.com/c/tools/+/358035 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 18096c5 commit fc8b4ca

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

go/internal/gcimporter/iexport.go

+3
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ func (w *exportWriter) param(obj types.Object) {
700700

701701
func (w *exportWriter) value(typ types.Type, v constant.Value) {
702702
w.typ(typ, nil)
703+
if w.p.version >= iexportVersionGo1_18 {
704+
w.int64(int64(v.Kind()))
705+
}
703706

704707
switch b := typ.Underlying().(*types.Basic); b.Info() & types.IsConstType {
705708
case types.IsBoolean:

go/internal/gcimporter/iexport_go118_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ type G[C comparable] int
4040
func ImplicitFunc[T ~int]() {}
4141
4242
type ImplicitType[T ~int] int
43+
44+
// Exercise constant import/export
45+
const C1 = 42
46+
const C2 int = 42
47+
const C3 float64 = 42
4348
`
4449
testExportSrc(t, []byte(src))
4550
}

go/internal/gcimporter/iimport.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ func iimportCommon(fset *token.FileSet, imports map[string]*types.Package, data
149149
r.Seek(sLen+dLen, io.SeekCurrent)
150150

151151
p := iimporter{
152-
exportVersion: version,
153-
ipath: path,
154-
version: int(version),
152+
version: int(version),
153+
ipath: path,
155154

156155
stringData: stringData,
157156
stringCache: make(map[uint64]string),
@@ -255,9 +254,8 @@ func iimportCommon(fset *token.FileSet, imports map[string]*types.Package, data
255254
}
256255

257256
type iimporter struct {
258-
exportVersion int64
259-
ipath string
260-
version int
257+
version int
258+
ipath string
261259

262260
stringData []byte
263261
stringCache map[uint64]string
@@ -428,7 +426,7 @@ func (r *importReader) obj(name string) {
428426
// We need to "declare" a typeparam in order to have a name that
429427
// can be referenced recursively (if needed) in the type param's
430428
// bound.
431-
if r.p.exportVersion < iexportVersionGenerics {
429+
if r.p.version < iexportVersionGenerics {
432430
errorf("unexpected type param type")
433431
}
434432
// Remove the "path" from the type param name that makes it unique
@@ -445,7 +443,7 @@ func (r *importReader) obj(name string) {
445443
id := ident{r.currPkg.Name(), name}
446444
r.p.tparamIndex[id] = t
447445
var implicit bool
448-
if r.p.exportVersion >= iexportVersionGo1_18 {
446+
if r.p.version >= iexportVersionGo1_18 {
449447
implicit = r.bool()
450448
}
451449
constraint := r.typ()
@@ -474,6 +472,10 @@ func (r *importReader) declare(obj types.Object) {
474472

475473
func (r *importReader) value() (typ types.Type, val constant.Value) {
476474
typ = r.typ()
475+
if r.p.version >= iexportVersionGo1_18 {
476+
// TODO: add support for using the kind.
477+
_ = constant.Kind(r.int64())
478+
}
477479

478480
switch b := typ.Underlying().(*types.Basic); b.Info() & types.IsConstType {
479481
case types.IsBoolean:
@@ -616,7 +618,7 @@ func (r *importReader) qualifiedIdent() (*types.Package, string) {
616618
}
617619

618620
func (r *importReader) pos() token.Pos {
619-
if r.p.exportVersion >= iexportVersionPosCol {
621+
if r.p.version >= iexportVersionPosCol {
620622
r.posv1()
621623
} else {
622624
r.posv0()
@@ -746,7 +748,7 @@ func (r *importReader) doType(base *types.Named) (res types.Type) {
746748
return typ
747749

748750
case typeParamType:
749-
if r.p.exportVersion < iexportVersionGenerics {
751+
if r.p.version < iexportVersionGenerics {
750752
errorf("unexpected type param type")
751753
}
752754
pkg, name := r.qualifiedIdent()
@@ -760,7 +762,7 @@ func (r *importReader) doType(base *types.Named) (res types.Type) {
760762
return r.p.tparamIndex[id]
761763

762764
case instanceType:
763-
if r.p.exportVersion < iexportVersionGenerics {
765+
if r.p.version < iexportVersionGenerics {
764766
errorf("unexpected instantiation type")
765767
}
766768
// pos does not matter for instances: they are positioned on the original
@@ -779,7 +781,7 @@ func (r *importReader) doType(base *types.Named) (res types.Type) {
779781
return t
780782

781783
case unionType:
782-
if r.p.exportVersion < iexportVersionGenerics {
784+
if r.p.version < iexportVersionGenerics {
783785
errorf("unexpected instantiation type")
784786
}
785787
terms := make([]*typeparams.Term, r.uint64())

0 commit comments

Comments
 (0)