From 9423d50d53f132d7d00f5126144736bfe65627b6 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 19 Jan 2021 22:57:45 +0700 Subject: [PATCH] [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 Run-TryBot: Cuong Manh Le TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/ir/fmt.go | 14 +++++++------- test/fixedbugs/issue43762.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 test/fixedbugs/issue43762.go diff --git a/src/cmd/compile/internal/ir/fmt.go b/src/cmd/compile/internal/ir/fmt.go index ee6a62625a..0ebfb84286 100644 --- a/src/cmd/compile/internal/ir/fmt.go +++ b/src/cmd/compile/internal/ir/fmt.go @@ -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('#'))) diff --git a/test/fixedbugs/issue43762.go b/test/fixedbugs/issue43762.go new file mode 100644 index 0000000000..4544b6e496 --- /dev/null +++ b/test/fixedbugs/issue43762.go @@ -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'" -- 2.48.1