Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go_indexer): mark variadics in all contexts #5845

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 20 additions & 13 deletions kythe/go/indexer/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ func (pi *PackageInfo) Emit(ctx context.Context, sink Sink, opts *EmitOptions) e
opts = &EmitOptions{}
}
e := &emitter{
ctx: ctx,
pi: pi,
sink: sink,
opts: opts,
impl: make(map[impl]struct{}),
anchored: make(map[ast.Node]struct{}),
fmeta: make(map[*ast.File]bool),
ctx: ctx,
pi: pi,
sink: sink,
opts: opts,
impl: make(map[impl]struct{}),
anchored: make(map[ast.Node]struct{}),
fmeta: make(map[*ast.File]bool),
variadics: make(map[*types.Slice]bool),
}

// Emit a node to represent the package as a whole.
Expand Down Expand Up @@ -239,6 +240,8 @@ type emitter struct {
anchored map[ast.Node]struct{} // see writeAnchor
firstErr error
cmap ast.CommentMap // current file's CommentMap

variadics map[*types.Slice]bool
}

type refKind int
Expand Down Expand Up @@ -474,7 +477,11 @@ func (e *emitter) emitType(typ types.Type) *spb.VName {
case *types.Array:
v = e.emitTApp(arrayTAppMS(typ.Len()), nodes.TBuiltin, govname.ArrayConstructorType(typ.Len()), e.emitType(typ.Elem()))
case *types.Slice:
v = e.emitTApp(sliceTAppMS, nodes.TBuiltin, govname.SliceConstructorType(), e.emitType(typ.Elem()))
if e.variadics[typ] {
v = e.emitTApp(variadicTAppMS, nodes.TBuiltin, govname.VariadicConstructorType(), e.emitType(typ.Elem()))
} else {
v = e.emitTApp(sliceTAppMS, nodes.TBuiltin, govname.SliceConstructorType(), e.emitType(typ.Elem()))
}
case *types.Pointer:
v = e.emitTApp(pointerTAppMS, nodes.TBuiltin, govname.PointerConstructorType(), e.emitType(typ.Elem()))
case *types.Chan:
Expand All @@ -495,14 +502,14 @@ func (e *emitter) emitType(typ types.Type) *spb.VName {
}},
}

params := e.visitTuple(typ.Params())
if typ.Variadic() && len(params) > 0 {
// Convert last parameter type from slice type to variadic type.
last := len(params) - 1
if typ.Variadic() {
// Mark last parameter type as variadic.
last := typ.Params().Len() - 1
if slice, ok := typ.Params().At(last).Type().(*types.Slice); ok {
params[last] = e.emitTApp(variadicTAppMS, nodes.TBuiltin, govname.VariadicConstructorType(), e.emitType(slice.Elem()))
e.variadics[slice] = true
}
}
params := e.visitTuple(typ.Params())

var ret *spb.VName
if typ.Results().Len() == 1 {
Expand Down
5 changes: 5 additions & 0 deletions kythe/go/indexer/testdata/code/rendered.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ func G[T comparable](t T) *Set[T] { return nil }
// - StrF.code/rendered/callsite_signature "Str[T](t)"
// - StrF.code/rendered/signature "func Str[T Stringer](t T) string"
func Str[T fmt.Stringer](t T) string { return t.String() }

// - @VA defines/binding VA
// - VA.code/rendered/callsite_signature "VA(args)"
// - VA.code/rendered/signature "func VA(args ...any)"
func VA(args ...any) {}