Skip to content

go/types: no way to construct the signature of append(s, "string"...) via the API #55030

@findleyr

Description

@findleyr

Discovered in a discussion with @timothy-king about #54946: when type checking the following package, we record the type of append as func([]byte, string...) []byte:

package p

func _() {
  var d []byte
  var s string
  _ = append(d, s...)
}

However, there is no way to construct func([]byte, string...) []byte via go/types APIs. NewSignatureType panics with variadic parameter must be of unnamed slice type. This has never mattered before because this type cannot appear in export data, but it matters for x/tools/go/ssa, which must do substitution inside function bodies.

What should we do about this?

  1. add a new API to go/types?
  2. remove this panic from go/types?
  3. work around this limitation in x/tools/go/ssa, for example by inserting a synthetic conversion?
  4. some other hack, for example the following rather ugly hack (which ignores error handling, etc)?
expr, _ := parser.ParseExpr("append([]byte(nil), \"\"...)")
info := Info{Types: make(map[ast.Expr]TypeAndValue)}
CheckExpr(fset, pkg, appendPos, expr, &info) // fset, pkg, appendPos from an existing package
k := expr.(*ast.CallExpr).Fun.(*ast.Ident)
T := info.Types[k].Type

Let's discuss. I would lean toward (2) or (4). This doesn't seem worth a new API (though the limitation should perhaps be more clearly documented).

CC @griesemer @adonovan @timothy-king

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions