Repository: kythe/kythe
Pull request #5812 (2023-08-25 17:15:09)

fix(go_indexer): fix builtin type references


Patch:
diff --git a/kythe/go/extractors/govname/govname.go b/kythe/go/extractors/govname/govname.go
index d2618957f..ce81d845b 100644
--- a/kythe/go/extractors/govname/govname.go
+++ b/kythe/go/extractors/govname/govname.go
@@ -185,16 +185,6 @@ func ForPackage(pkg *build.Package, opts *PackageVNameOptions) *spb.VName {
 	return v
 }
 
-// ForBuiltin returns a VName for a Go built-in with the given signature.
-func ForBuiltin(signature string) *spb.VName {
-	return &spb.VName{
-		Corpus:    GolangCorpus,
-		Language:  Language,
-		Root:      "ref/spec",
-		Signature: signature,
-	}
-}
-
 // ForStandardLibrary returns a VName for a standard library package with the
 // given import path.
 func ForStandardLibrary(importPath string) *spb.VName {
diff --git a/kythe/go/indexer/BUILD b/kythe/go/indexer/BUILD
index 5184589e5..7f6e388f0 100644
--- a/kythe/go/indexer/BUILD
+++ b/kythe/go/indexer/BUILD
@@ -211,6 +211,7 @@ go_indexer_test(
     has_marked_source = True,
     import_path = "rendered",
     resolve_code_facts = True,
+    deps = [":builtin_test"],
 )
 
 go_indexer_test(
diff --git a/kythe/go/indexer/cmd/go_indexer/go_indexer.go b/kythe/go/indexer/cmd/go_indexer/go_indexer.go
index e5c95297b..fa70c460a 100644
--- a/kythe/go/indexer/cmd/go_indexer/go_indexer.go
+++ b/kythe/go/indexer/cmd/go_indexer/go_indexer.go
@@ -167,6 +167,7 @@ func indexGo(ctx context.Context, unit *apb.CompilationUnit, f indexer.Fetcher)
 		UseFileAsTopLevelScope:         *useFileAsTopLevelScope,
 		OverrideStdlibCorpus:           *overrideStdlibCorpus,
 		EmitRefCallOverIdentifier:      *emitRefCallOverIdentifier,
+		Verbose:                        *verbose,
 	})
 }
 
diff --git a/kythe/go/indexer/emit.go b/kythe/go/indexer/emit.go
index f0cd831bb..05f1b216e 100644
--- a/kythe/go/indexer/emit.go
+++ b/kythe/go/indexer/emit.go
@@ -80,6 +80,9 @@ type EmitOptions struct {
 	// over function identifiers (or the legacy behavior of over the entire
 	// callsite).
 	EmitRefCallOverIdentifier bool
+
+	// Verbose determines whether verbose logging is enabled.
+	Verbose bool
 }
 
 func (e *EmitOptions) emitMarkedSource() bool {
@@ -132,6 +135,13 @@ func (e *EmitOptions) docURL(pi *PackageInfo) string {
 	return u.String()
 }
 
+func (e *EmitOptions) verbose() bool {
+	if e == nil {
+		return false
+	}
+	return e.Verbose
+}
+
 // An impl records that a type A implements an interface B.
 type impl struct{ A, B types.Object }
 
@@ -210,8 +220,10 @@ func (pi *PackageInfo) Emit(ctx context.Context, sink Sink, opts *EmitOptions) e
 	e.emitSatisfactions()
 
 	// TODO(fromberger): Add diagnostics for type-checker errors.
-	for _, err := range pi.Errors {
-		log.Warningf("Type resolution error: %v", err)
+	if opts.verbose() {
+		for _, err := range pi.Errors {
+			log.Warningf("Type resolution error: %v", err)
+		}
 	}
 	return e.firstErr
 }
diff --git a/kythe/go/indexer/indexer.go b/kythe/go/indexer/indexer.go
index 3129fec5f..4e227a01e 100644
--- a/kythe/go/indexer/indexer.go
+++ b/kythe/go/indexer/indexer.go
@@ -486,7 +486,7 @@ func (pi *PackageInfo) ObjectVName(obj types.Object) *spb.VName {
 	if base := pi.PackageVName[pkg]; base != nil {
 		vname = proto.Clone(base).(*spb.VName)
 	} else if pkg == nil {
-		return govname.ForBuiltin(sig)
+		return govname.Builtin(obj.Name())
 	} else {
 		// This is an indirect import, that is, a name imported but not
 		// mentioned explicitly by the package being indexed.
diff --git a/kythe/go/util/markedsource/resolve.go b/kythe/go/util/markedsource/resolve.go
index a364a5e03..ef8016dc7 100644
--- a/kythe/go/util/markedsource/resolve.go
+++ b/kythe/go/util/markedsource/resolve.go
@@ -145,13 +145,13 @@ func (r *Resolver) resolve(ticket string, ms *cpb.MarkedSource) *cpb.MarkedSourc
 	case cpb.MarkedSource_PARAMETER_LOOKUP_BY_PARAM_WITH_DEFAULTS, cpb.MarkedSource_PARAMETER_LOOKUP_BY_PARAM:
 		// TODO: handle param/default
 		params := r.params[ticket]
-		if int(ms.GetLookupIndex()) >= len(params) {
+		if int(ms.GetLookupIndex()) > len(params) {
 			return ensureKind(invalidLookupReplacement, cpb.MarkedSource_PARAMETER)
 		}
 		return r.resolveParameters(ms, params[ms.GetLookupIndex():])
 	case cpb.MarkedSource_PARAMETER_LOOKUP_BY_TPARAM:
 		tparams := r.tparams[ticket]
-		if int(ms.GetLookupIndex()) >= len(tparams) {
+		if int(ms.GetLookupIndex()) > len(tparams) {
 			return ensureKind(invalidLookupReplacement, cpb.MarkedSource_PARAMETER)
 		}
 		return r.resolveParameters(ms, tparams[ms.GetLookupIndex():])
diff --git a/kythe/go/util/tools/markedsource/markedsource.go b/kythe/go/util/tools/markedsource/markedsource.go
index 397408d72..de3167282 100644
--- a/kythe/go/util/tools/markedsource/markedsource.go
+++ b/kythe/go/util/tools/markedsource/markedsource.go
@@ -44,6 +44,7 @@ import (
 var (
 	rewrite = flag.Bool("rewrite", false, "Rewrite all code facts to be fully resolved")
 
+	renderSignature         = flag.Bool("render_signatures", false, "Whether to emit /kythe/code/rendered/signature facts (requires --rewrite)")
 	renderCallsiteSignature = flag.Bool("render_callsite_signatures", false, "Whether to emit /kythe/code/rendered/callsite_signature facts (requires --rewrite)")
 	renderQualifiedName     = flag.Bool("render_qualified_names", false, "Whether to emit /kythe/code/rendered/qualified_name facts (requires --rewrite)")
 )
@@ -96,6 +97,14 @@ func main() {
 						FactValue: []byte(val),
 					}))
 				}
+				if *renderSignature {
+					val := markedsource.RenderSignature(resolved, markedsource.PlaintextContent, nil)
+					exitOnErr(wr.PutProto(&spb.Entry{
+						Source:    e.Source,
+						FactName:  renderFactPrefix + "signature",
+						FactValue: []byte(val),
+					}))
+				}
 			}
 			exitOnErr(wr.PutProto(e))
 		}


Test patch:
diff --git a/kythe/go/extractors/govname/govname_test.go b/kythe/go/extractors/govname/govname_test.go
index bf64b6c1b..6c3653550 100644
--- a/kythe/go/extractors/govname/govname_test.go
+++ b/kythe/go/extractors/govname/govname_test.go
@@ -139,20 +139,6 @@ func TestNotStandardLib(t *testing.T) {
 	}
 }
 
-func TestForBuiltin(t *testing.T) {
-	const signature = "blah"
-	want := &spb.VName{
-		Corpus:    GolangCorpus,
-		Language:  Language,
-		Root:      "ref/spec",
-		Signature: signature,
-	}
-	got := ForBuiltin("blah")
-	if !proto.Equal(got, want) {
-		t.Errorf("ForBuiltin(%q): got %+v, want %+v", signature, got, want)
-	}
-}
-
 func TestForStandardLibrary(t *testing.T) {
 	tests := []struct {
 		input string
diff --git a/kythe/go/indexer/testdata/builtin.go b/kythe/go/indexer/testdata/builtin.go
index 55164a57b..3fa1be703 100644
--- a/kythe/go/indexer/testdata/builtin.go
+++ b/kythe/go/indexer/testdata/builtin.go
@@ -1,9 +1,9 @@
 package builtin
 
 //- BoolBuiltin=vname("bool#builtin", "golang.org", "", "", "go").node/kind tbuiltin
-//- BoolBuiltin.code/rendered/callsite_signature "bool"
+//- BoolBuiltin.code/rendered/signature "bool"
 //- IntBuiltin=vname("int#builtin", "golang.org", "", "", "go").node/kind tbuiltin
-//- IntBuiltin.code/rendered/callsite_signature "int"
+//- IntBuiltin.code/rendered/signature "int"
 
 //- ErrorBuiltin=vname("error#builtin", "golang.org", "", "", "go").node/kind tbuiltin
-//- ErrorBuiltin.code/rendered/callsite_signature "error"
+//- ErrorBuiltin.code/rendered/signature "error"
diff --git a/kythe/go/indexer/testdata/code/rendered.go b/kythe/go/indexer/testdata/code/rendered.go
index e995eaaff..3fb66c493 100644
--- a/kythe/go/indexer/testdata/code/rendered.go
+++ b/kythe/go/indexer/testdata/code/rendered.go
@@ -20,3 +20,13 @@ func (s *S) M() {}
 // - @arg defines/binding Arg
 // - Arg.code/rendered/qualified_name "rendered.S.MArg.arg"
 func (s *S) MArg(arg int) {}
+
+//- StringBuiltin=vname("string#builtin", "golang.org", "", "", "go").node/kind tbuiltin
+//- StringBuiltin.code/rendered/callsite_signature "string"
+//- ErrorBuiltin=vname("error#builtin", "golang.org", "", "", "go").node/kind tbuiltin
+//- ErrorBuiltin.code/rendered/callsite_signature "error"
+
+// - @H defines/binding H?
+// - H.code/rendered/callsite_signature "H(param)"
+// - H.code/rendered/signature "H(param func() (string, error)) error"
+func H(param func() (string, error)) error { return nil }
diff --git a/tools/build_rules/verifier_test/verifier_test.sh.in b/tools/build_rules/verifier_test/verifier_test.sh.in
index ed19db93f..9f6b77fe7 100644
--- a/tools/build_rules/verifier_test/verifier_test.sh.in
+++ b/tools/build_rules/verifier_test/verifier_test.sh.in
@@ -27,7 +27,7 @@ ENTRIES_GZ=(@ENTRIES_GZ@)
   if (( ${#ENTRIES_GZ[@]} )); then gunzip -c "${ENTRIES_GZ[@]}"; fi
 ) | (
   if [[ -z "@REWRITE@" ]]; then
-    @MARKEDSOURCE@ --rewrite --render_callsite_signatures --render_qualified_names
+    @MARKEDSOURCE@ --rewrite --render_callsite_signatures --render_qualified_names --render_signatures
   else
     cat
   fi