Skip to content

Commit

Permalink
fix(go_indexer): mark variadics in all contexts (#5845)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed Sep 15, 2023
1 parent 26c7439 commit b662dce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
33 changes: 20 additions & 13 deletions kythe/go/indexer/emit.go
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
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) {}

0 comments on commit b662dce

Please sign in to comment.