Skip to content

Commit a9ee026

Browse files
fmt: display contained values when formatting atomic booleans and integers
* treat `atomic.{Int32,Int64,Uint32,Uint64}` values as integers when formatting, using the underlying integer value (by calling Load). This has the effect of, for example, formatting an atomic integer type containing 42 as "42", not "{{}, 42}"). * treat `atomic.Bool` values as booleans when formatting, using the contained boolean value (by calling Load). This has the effect of, for example, formatting an true atomic.Bool value containing as "true", not "{{}, 1}" or "{{} %!t(uint32=1)}" as before. Fixes #54731
1 parent 7393049 commit a9ee026

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/fmt/fmt_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"reflect"
1414
"runtime"
1515
"strings"
16+
"sync/atomic"
1617
"testing"
1718
"time"
1819
"unicode"
@@ -39,6 +40,36 @@ type (
3940
renamedComplex128 complex128
4041
)
4142

43+
func newAtomicInt32(val int32) *atomic.Int32 {
44+
var r atomic.Int32
45+
r.Store(val)
46+
return &r
47+
}
48+
49+
func newAtomicUint32(val uint32) *atomic.Uint32 {
50+
var r atomic.Uint32
51+
r.Store(val)
52+
return &r
53+
}
54+
55+
func newAtomicInt64(val int64) *atomic.Int64 {
56+
var r atomic.Int64
57+
r.Store(val)
58+
return &r
59+
}
60+
61+
func newAtomicUint64(val uint64) *atomic.Uint64 {
62+
var r atomic.Uint64
63+
r.Store(val)
64+
return &r
65+
}
66+
67+
func newAtomicBool(val bool) *atomic.Bool {
68+
var r atomic.Bool
69+
r.Store(val)
70+
return &r
71+
}
72+
4273
func TestFmtInterface(t *testing.T) {
4374
var i1 any
4475
i1 = "abc"
@@ -1081,6 +1112,32 @@ var fmtTests = []struct {
10811112
{"%☠", SI{&[]any{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"},
10821113
{"%☠", reflect.Value{}, "<invalid reflect.Value>"},
10831114
{"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(int=1)]"},
1115+
1116+
// atomics
1117+
{"%d", newAtomicInt32(25), "25"},
1118+
{"%d", newAtomicInt32(math.MaxInt32), "2147483647"},
1119+
{"%d", newAtomicInt32(-5), "-5"},
1120+
{"%d", newAtomicInt32(math.MinInt32), "-2147483648"},
1121+
1122+
{"%d", newAtomicUint32(19), "19"},
1123+
{"%d", newAtomicUint32(math.MaxUint32), "4294967295"},
1124+
1125+
{"%d", newAtomicInt64(99), "99"},
1126+
{"%d", newAtomicInt64(math.MaxInt64), "9223372036854775807"},
1127+
{"%d", newAtomicInt64(-7), "-7"},
1128+
{"%d", newAtomicInt64(math.MinInt64), "-9223372036854775808"},
1129+
1130+
{"%d", newAtomicUint64(12), "12"},
1131+
{"%d", newAtomicUint64(math.MaxUint64), "18446744073709551615"},
1132+
1133+
{"%t", newAtomicBool(true), "true"},
1134+
{"%t", newAtomicBool(false), "false"},
1135+
1136+
{"%v", newAtomicInt32(42), "42"},
1137+
{"%v", newAtomicUint32(4242), "4242"},
1138+
{"%v", newAtomicInt64(424242), "424242"},
1139+
{"%v", newAtomicUint64(42424242), "42424242"},
1140+
{"%v", newAtomicBool(true), "true"},
10841141
}
10851142

10861143
// zeroFill generates zero-filled strings of the specified width. The length

src/fmt/print.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"strconv"
1313
"sync"
14+
"sync/atomic"
1415
"unicode/utf8"
1516
)
1617

@@ -747,6 +748,26 @@ func (p *pp) printArg(arg any, verb rune) {
747748
p.fmtInteger(f, unsigned, verb)
748749
case uintptr:
749750
p.fmtInteger(uint64(f), unsigned, verb)
751+
case atomic.Int32:
752+
p.fmtInteger(uint64(f.Load()), signed, verb)
753+
case *atomic.Int32:
754+
p.fmtInteger(uint64(f.Load()), signed, verb)
755+
case atomic.Int64:
756+
p.fmtInteger(uint64(f.Load()), signed, verb)
757+
case *atomic.Int64:
758+
p.fmtInteger(uint64(f.Load()), signed, verb)
759+
case atomic.Uint32:
760+
p.fmtInteger(uint64(f.Load()), unsigned, verb)
761+
case *atomic.Uint32:
762+
p.fmtInteger(uint64(f.Load()), unsigned, verb)
763+
case atomic.Uint64:
764+
p.fmtInteger(uint64(f.Load()), unsigned, verb)
765+
case *atomic.Uint64:
766+
p.fmtInteger(uint64(f.Load()), unsigned, verb)
767+
case atomic.Bool:
768+
p.fmtBool(f.Load(), verb)
769+
case *atomic.Bool:
770+
p.fmtBool(f.Load(), verb)
750771
case string:
751772
p.fmtString(f, verb)
752773
case []byte:

0 commit comments

Comments
 (0)