From e1e9089196427c2b665aced3632e0a26b97cb16d Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 10 Oct 2013 13:33:29 -0400 Subject: [PATCH] go.tools/importer: change type of print{,ln} built-ins. Before: func(any, ...interface{}). After: func(any, ...any) They are no longer variadic, so you can't write print(x, y...). (Recall that print(1) and print(interface{}(1)) behave differently and that this is useful.) Fixes bug 6560 R=gri CC=golang-dev https://golang.org/cl/14455054 --- importer/pkginfo.go | 12 ++++++------ ssa/interp/ops.go | 13 +++++-------- ssa/interp/testdata/coverage.go | 2 ++ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/importer/pkginfo.go b/importer/pkginfo.go index 646bbd2b..0b098785 100644 --- a/importer/pkginfo.go +++ b/importer/pkginfo.go @@ -135,12 +135,12 @@ func (info *PackageInfo) BuiltinCallSignature(e *ast.CallExpr) *types.Signature types.NewVar(token.NoPos, nil, "", t0), types.NewVar(token.NoPos, nil, "", t1)) - case "print", "println": // print{,ln}(any, ...interface{}) - isVariadic = true - // Note, arg0 may have any type, not necessarily tEface. - params = append(params, - types.NewVar(token.NoPos, nil, "", info.TypeOf(e.Args[0])), - types.NewVar(token.NoPos, nil, "", types.NewSlice(tEface))) + case "print", "println": // print{,ln}(any, ...) + // Note, args may have any type, not necessarily tEface. + var params []*types.Var + for _, arg := range e.Args { + params = append(params, types.NewVar(token.NoPos, nil, "", info.TypeOf(arg))) + } case "close": params = append(params, types.NewVar(token.NoPos, nil, "", info.TypeOf(e.Args[0]))) diff --git a/ssa/interp/ops.go b/ssa/interp/ops.go index 611990a3..37acd6bf 100644 --- a/ssa/interp/ops.go +++ b/ssa/interp/ops.go @@ -972,17 +972,14 @@ func callBuiltin(caller *frame, callpos token.Pos, fn *ssa.Builtin, args []value } return nil - case "print", "println": // print(anytype, ...interface{}) + case "print", "println": // print(any, ...) ln := fn.Name() == "println" var buf bytes.Buffer - buf.WriteString(toString(args[0])) - if len(args) == 2 { - for _, arg := range args[1].([]value) { - if ln { - buf.WriteRune(' ') - } - buf.WriteString(toString(arg)) + for i, arg := range args { + if i > 0 && ln { + buf.WriteRune(' ') } + buf.WriteString(toString(arg)) } if ln { buf.WriteRune('\n') diff --git a/ssa/interp/testdata/coverage.go b/ssa/interp/testdata/coverage.go index 00d8b4d3..56c30315 100644 --- a/ssa/interp/testdata/coverage.go +++ b/ssa/interp/testdata/coverage.go @@ -151,6 +151,8 @@ func init() { } func main() { + print() // legal + if counter != 2*3*5 { panic(counter) }