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
This commit is contained in:
parent
9cce4759bb
commit
e1e9089196
|
@ -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])))
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -151,6 +151,8 @@ func init() {
|
|||
}
|
||||
|
||||
func main() {
|
||||
print() // legal
|
||||
|
||||
if counter != 2*3*5 {
|
||||
panic(counter)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue