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, "", t0),
|
||||||
types.NewVar(token.NoPos, nil, "", t1))
|
types.NewVar(token.NoPos, nil, "", t1))
|
||||||
|
|
||||||
case "print", "println": // print{,ln}(any, ...interface{})
|
case "print", "println": // print{,ln}(any, ...)
|
||||||
isVariadic = true
|
// Note, args may have any type, not necessarily tEface.
|
||||||
// Note, arg0 may have any type, not necessarily tEface.
|
var params []*types.Var
|
||||||
params = append(params,
|
for _, arg := range e.Args {
|
||||||
types.NewVar(token.NoPos, nil, "", info.TypeOf(e.Args[0])),
|
params = append(params, types.NewVar(token.NoPos, nil, "", info.TypeOf(arg)))
|
||||||
types.NewVar(token.NoPos, nil, "", types.NewSlice(tEface)))
|
}
|
||||||
|
|
||||||
case "close":
|
case "close":
|
||||||
params = append(params, types.NewVar(token.NoPos, nil, "", info.TypeOf(e.Args[0])))
|
params = append(params, types.NewVar(token.NoPos, nil, "", info.TypeOf(e.Args[0])))
|
||||||
|
|
|
@ -972,18 +972,15 @@ func callBuiltin(caller *frame, callpos token.Pos, fn *ssa.Builtin, args []value
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case "print", "println": // print(anytype, ...interface{})
|
case "print", "println": // print(any, ...)
|
||||||
ln := fn.Name() == "println"
|
ln := fn.Name() == "println"
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
buf.WriteString(toString(args[0]))
|
for i, arg := range args {
|
||||||
if len(args) == 2 {
|
if i > 0 && ln {
|
||||||
for _, arg := range args[1].([]value) {
|
|
||||||
if ln {
|
|
||||||
buf.WriteRune(' ')
|
buf.WriteRune(' ')
|
||||||
}
|
}
|
||||||
buf.WriteString(toString(arg))
|
buf.WriteString(toString(arg))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if ln {
|
if ln {
|
||||||
buf.WriteRune('\n')
|
buf.WriteRune('\n')
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,8 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
print() // legal
|
||||||
|
|
||||||
if counter != 2*3*5 {
|
if counter != 2*3*5 {
|
||||||
panic(counter)
|
panic(counter)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue