Skip to content

Commit e0783a8

Browse files
findleyrgopherbot
authored andcommitted
internal/gcimporter: remove bug report on objectpath failure
As reported in golang/go#61674, there are certain cases involving instantiated fields that simply can't be encoded using objectpaths. Therefore, the workaround is fundamentally insufficient, and should probably be removed or made irrelevant (golang/go#61674). In the meantime, update commentary and remove the bug report. Update the gopls regression test for this behavior to exercise the objectpath failure. While at it, ensure that the marker regtests panic on bugs, like all other regtests. This ran into an existing imprecise bug report, which is amended. Updates golang/go#61674 Change-Id: I0eaea00d46cec88ac4c20cebdde805a7db3a33ad Reviewed-on: https://go-review.googlesource.com/c/tools/+/514575 gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Robert Findley <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 75f6f9c commit e0783a8

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

gopls/internal/lsp/cache/check.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1457,8 +1457,10 @@ func typeCheckImpl(ctx context.Context, b *typeCheckBatch, inputs typeCheckInput
14571457
diags, err := typeErrorDiagnostics(inputs.moduleMode, inputs.linkTarget, pkg, e)
14581458
if err != nil {
14591459
// If we fail here and there are no parse errors, it means we are hiding
1460-
// a valid type-checking error from the user. This must be a bug.
1461-
if len(pkg.parseErrors) == 0 {
1460+
// a valid type-checking error from the user. This must be a bug, with
1461+
// one exception: relocated primary errors may fail processing, because
1462+
// they reference locations outside of the package.
1463+
if len(pkg.parseErrors) == 0 && !e.relocated {
14621464
bug.Reportf("failed to compute position for type error %v: %v", e, err)
14631465
}
14641466
continue
@@ -1788,6 +1790,7 @@ func missingPkgError(from PackageID, pkgPath string, moduleMode bool) error {
17881790
}
17891791

17901792
type extendedError struct {
1793+
relocated bool // if set, this is a relocation of a primary error to a secondary location
17911794
primary types.Error
17921795
secondaries []types.Error
17931796
}
@@ -1840,7 +1843,7 @@ func expandErrors(errs []types.Error, supportsRelatedInformation bool) []extende
18401843

18411844
// Copy over the secondary errors, noting the location of the
18421845
// current error we're cloning.
1843-
clonedError := extendedError{primary: relocatedSecondary, secondaries: []types.Error{original.primary}}
1846+
clonedError := extendedError{relocated: true, primary: relocatedSecondary, secondaries: []types.Error{original.primary}}
18441847
for j, secondary := range original.secondaries {
18451848
if i == j {
18461849
secondary.Msg += " (this error)"
@@ -1849,7 +1852,6 @@ func expandErrors(errs []types.Error, supportsRelatedInformation bool) []extende
18491852
}
18501853
result = append(result, clonedError)
18511854
}
1852-
18531855
}
18541856
return result
18551857
}

gopls/internal/regtest/marker/marker_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"os"
99
"testing"
1010

11+
"golang.org/x/tools/gopls/internal/bug"
1112
. "golang.org/x/tools/gopls/internal/lsp/regtest"
1213
"golang.org/x/tools/internal/testenv"
1314
)
1415

1516
func TestMain(m *testing.M) {
17+
bug.PanicOnBugs = true
1618
testenv.ExitIfSmallMachine()
1719
os.Exit(m.Run())
1820
}

gopls/internal/regtest/marker/testdata/references/issue60676.txt

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ shared by types from multiple packages. See golang/go#60676.
55
Note that the marker test runner awaits the initial workspace load, so export
66
data should be populated at the time references are requested.
77

8+
-- flags --
9+
-min_go=go1.18
10+
811
-- go.mod --
912
module mod.test
1013

@@ -32,8 +35,11 @@ type EI interface {
3235
N() //@loc(NDef, "N")
3336
}
3437

38+
type T[P any] struct{ f P }
39+
3540
type Error error
3641

42+
3743
-- b/b.go --
3844
package b
3945

@@ -43,6 +49,8 @@ type B a.A
4349

4450
type BI a.AI
4551

52+
type T a.T[int] // must not panic
53+
4654
-- c/c.go --
4755
package c
4856

internal/gcimporter/iexport.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func IExportShallow(fset *token.FileSet, pkg *types.Package, reportf ReportFunc)
4646
// TODO(adonovan): use byte slices throughout, avoiding copying.
4747
const bundle, shallow = false, true
4848
var out bytes.Buffer
49-
err := iexportCommon(&out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg}, reportf)
49+
err := iexportCommon(&out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg})
5050
return out.Bytes(), err
5151
}
5252

@@ -86,16 +86,16 @@ const bundleVersion = 0
8686
// so that calls to IImportData can override with a provided package path.
8787
func IExportData(out io.Writer, fset *token.FileSet, pkg *types.Package) error {
8888
const bundle, shallow = false, false
89-
return iexportCommon(out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg}, nil)
89+
return iexportCommon(out, fset, bundle, shallow, iexportVersion, []*types.Package{pkg})
9090
}
9191

9292
// IExportBundle writes an indexed export bundle for pkgs to out.
9393
func IExportBundle(out io.Writer, fset *token.FileSet, pkgs []*types.Package) error {
9494
const bundle, shallow = true, false
95-
return iexportCommon(out, fset, bundle, shallow, iexportVersion, pkgs, nil)
95+
return iexportCommon(out, fset, bundle, shallow, iexportVersion, pkgs)
9696
}
9797

98-
func iexportCommon(out io.Writer, fset *token.FileSet, bundle, shallow bool, version int, pkgs []*types.Package, reportf ReportFunc) (err error) {
98+
func iexportCommon(out io.Writer, fset *token.FileSet, bundle, shallow bool, version int, pkgs []*types.Package) (err error) {
9999
if !debug {
100100
defer func() {
101101
if e := recover(); e != nil {
@@ -113,7 +113,6 @@ func iexportCommon(out io.Writer, fset *token.FileSet, bundle, shallow bool, ver
113113
fset: fset,
114114
version: version,
115115
shallow: shallow,
116-
reportf: reportf,
117116
allPkgs: map[*types.Package]bool{},
118117
stringIndex: map[string]uint64{},
119118
declIndex: map[types.Object]uint64{},
@@ -330,7 +329,6 @@ type iexporter struct {
330329

331330
shallow bool // don't put types from other packages in the index
332331
objEncoder *objectpath.Encoder // encodes objects from other packages in shallow mode; lazily allocated
333-
reportf ReportFunc // if non-nil, used to report bugs
334332
localpkg *types.Package // (nil in bundle mode)
335333

336334
// allPkgs tracks all packages that have been referenced by
@@ -917,22 +915,25 @@ func (w *exportWriter) objectPath(obj types.Object) {
917915
objectPath, err := w.p.objectpathEncoder().For(obj)
918916
if err != nil {
919917
// Fall back to the empty string, which will cause the importer to create a
920-
// new object.
918+
// new object, which matches earlier behavior. Creating a new object is
919+
// sufficient for many purposes (such as type checking), but causes certain
920+
// references algorithms to fail (golang/go#60819). However, we didn't
921+
// notice this problem during months of [email protected] testing.
921922
//
922-
// This is incorrect in shallow mode (golang/go#60819), but matches
923-
// the previous behavior. This code is defensive, as it is hard to
924-
// prove that the objectpath algorithm will succeed in all cases, and
925-
// creating a new object sort of works.
926-
// (we didn't notice the bug during months of [email protected] testing)
923+
// TODO(golang/go#61674): this workaround is insufficient, as in the case
924+
// where the field forwarded from an instantiated type that may not appear
925+
// in the export data of the original package:
927926
//
928-
// However, report a bug so that we can eventually have confidence
929-
// that export/import is producing a correct package.
927+
// // package a
928+
// type A[P any] struct{ F P }
930929
//
931-
// TODO: remove reportf once we have such confidence.
930+
// // package b
931+
// type B a.A[int]
932+
//
933+
// We need to update references algorithms not to depend on this
934+
// de-duplication, at which point we may want to simply remove the
935+
// workaround here.
932936
w.string("")
933-
if w.p.reportf != nil {
934-
w.p.reportf("unable to encode object %q in package %q: %v", obj.Name(), obj.Pkg().Path(), err)
935-
}
936937
return
937938
}
938939
w.string(string(objectPath))

internal/gcimporter/iexport_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func readExportFile(filename string) ([]byte, error) {
6161
func iexport(fset *token.FileSet, version int, pkg *types.Package) ([]byte, error) {
6262
var buf bytes.Buffer
6363
const bundle, shallow = false, false
64-
if err := gcimporter.IExportCommon(&buf, fset, bundle, shallow, version, []*types.Package{pkg}, nil); err != nil {
64+
if err := gcimporter.IExportCommon(&buf, fset, bundle, shallow, version, []*types.Package{pkg}); err != nil {
6565
return nil, err
6666
}
6767
return buf.Bytes(), nil

0 commit comments

Comments
 (0)