[dev.regabi] cmd/compile: use '%q' for printing rune values less than 128

Fixes #43762

Change-Id: I51734c9b4ee2366a5dae53b2d27b363f4d5fe6c1
Reviewed-on: https://go-review.googlesource.com/c/go/+/284592
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2021-01-19 22:57:45 +07:00
parent a2f825c542
commit 9423d50d53
2 changed files with 18 additions and 7 deletions

View File

@ -589,20 +589,20 @@ func exprFmt(n Node, s fmt.State, prec int) {
}
if n.Type() == types.UntypedRune {
switch x, ok := constant.Int64Val(n.Val()); {
switch x, ok := constant.Uint64Val(n.Val()); {
case !ok:
fallthrough
default:
fmt.Fprintf(s, "('\\x00' + %v)", n.Val())
case ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'':
fmt.Fprintf(s, "'%c'", int(x))
case x < utf8.RuneSelf:
fmt.Fprintf(s, "%q", x)
case 0 <= x && x < 1<<16:
fmt.Fprintf(s, "'\\u%04x'", uint(int(x)))
case x < 1<<16:
fmt.Fprintf(s, "'\\u%04x'", x)
case 0 <= x && x <= utf8.MaxRune:
fmt.Fprintf(s, "'\\U%08x'", uint64(x))
case x <= utf8.MaxRune:
fmt.Fprintf(s, "'\\U%08x'", x)
}
} else {
fmt.Fprint(s, types.FmtConst(n.Val(), s.Flag('#')))

View File

@ -0,0 +1,11 @@
// errorcheck
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
var _ = true == '\\' // ERROR "invalid operation: true == '\\\\'"
var _ = true == '\'' // ERROR "invalid operation: true == '\\''"
var _ = true == '\n' // ERROR "invalid operation: true == '\\n'"