Skip to content

Commit 1fd0739

Browse files
n
Change-Id: I78354489ef5e7e470919b11c8315a7ed912bd301
1 parent 38450aa commit 1fd0739

File tree

3 files changed

+38
-49
lines changed

3 files changed

+38
-49
lines changed

src/cmd/compile/internal/test/inl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func TestIntendedInlining(t *testing.T) {
231231
"(*Pointer[go.shape.int]).Swap",
232232
},
233233
"internal/reflectlite": {
234-
"Flag.MustBe",
234+
"MustBe",
235235
"Flag.MustBeAssignable",
236236
"Flag.MustBeExported",
237237
"Flag.Kind",

src/internal/reflectlite/value.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -447,23 +447,12 @@ func (f Flag) Ro() Flag {
447447
return 0
448448
}
449449

450-
// mustBe panics if f's kind is not expected.
451-
// Making this a method on flag instead of on [reflect.Value]
452-
// (and embedding flag in Value) means that we can write
453-
// the very clear v.mustBe(Bool) and have it compile into
454-
// v.flag.mustBe(Bool), which will only bother to copy the
455-
// single important word for the receiver.
456-
func (f Flag) MustBe(expected Kind) {
450+
func MustBe(f Flag, expected Kind) {
457451
if f.Kind() != expected {
458-
mustBePanic(f)
452+
panic(NewValueError(valueMethodName(), f.Kind()))
459453
}
460454
}
461455

462-
//go:noinline
463-
func mustBePanic(k Flag) {
464-
panic(NewValueError(valueMethodName(), Kind(k.Kind())))
465-
}
466-
467456
// mustBeExported panics if f records that the value was obtained using
468457
// an unexported field.
469458
func (f Flag) MustBeExported() {
@@ -509,7 +498,7 @@ func (f Flag) MustBeAssignableSlow() {
509498
//
510499
//go:noinline
511500
func (f Flag) PanicNotMap() {
512-
f.MustBe(abi.Map)
501+
MustBe(f, abi.Map)
513502
}
514503

515504
// A ValueError occurs when a Value method is invoked on
@@ -551,6 +540,6 @@ func valueMethodName() string {
551540

552541
// NewValueError default return [ValueError].
553542
// When reflect is imported, return [reflect.ValueError].
554-
var NewValueError func(Method string, Kind abi.Kind) interface{} = func(Method string, Kind abi.Kind) interface{} {
543+
var NewValueError func(Method string, Kind abi.Kind) any = func(Method string, Kind abi.Kind) any {
555544
return &ValueError{Method: Method, Kind: Kind}
556545
}

src/reflect/value.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (v Value) Bool() bool {
194194
}
195195

196196
func (v Value) panicNotBool() {
197-
v.flag.MustBe(abi.Bool)
197+
reflectlite.MustBe(v.flag, abi.Bool)
198198
}
199199

200200
var bytesType = rtypeOf(([]byte)(nil))
@@ -235,7 +235,7 @@ func (v Value) bytesSlow() []byte {
235235
// runes returns v's underlying value.
236236
// It panics if v's underlying value is not a slice of runes (int32s).
237237
func (v Value) runes() []rune {
238-
v.flag.MustBe(abi.Slice)
238+
reflectlite.MustBe(v.flag, abi.Slice)
239239
if v.typ().Elem().Kind() != abi.Int32 {
240240
panic("reflect.Value.Bytes of non-rune slice")
241241
}
@@ -270,7 +270,7 @@ func (v Value) CanSet() bool {
270270
// If v is a variadic function, Call creates the variadic slice parameter
271271
// itself, copying in the corresponding values.
272272
func (v Value) Call(in []Value) []Value {
273-
v.flag.MustBe(abi.Func)
273+
reflectlite.MustBe(v.flag, abi.Func)
274274
v.flag.MustBeExported()
275275
return v.call("Call", in)
276276
}
@@ -283,7 +283,7 @@ func (v Value) Call(in []Value) []Value {
283283
// As in Go, each input argument must be assignable to the
284284
// type of the function's corresponding input parameter.
285285
func (v Value) CallSlice(in []Value) []Value {
286-
v.flag.MustBe(abi.Func)
286+
reflectlite.MustBe(v.flag, abi.Func)
287287
v.flag.MustBeExported()
288288
return v.call("CallSlice", in)
289289
}
@@ -1085,7 +1085,7 @@ func (v Value) capNonSlice() int {
10851085
// It panics if v's Kind is not [Chan] or
10861086
// v is a receive-only channel.
10871087
func (v Value) Close() {
1088-
v.flag.MustBe(abi.Chan)
1088+
reflectlite.MustBe(v.flag, abi.Chan)
10891089
v.flag.MustBeExported()
10901090
tt := (*chanType)(unsafe.Pointer(v.typ()))
10911091
if ChanDir(tt.Dir)&SendDir == 0 {
@@ -1211,7 +1211,7 @@ func (v Value) FieldByIndex(index []int) Value {
12111211
if len(index) == 1 {
12121212
return v.Field(index[0])
12131213
}
1214-
v.flag.MustBe(abi.Struct)
1214+
reflectlite.MustBe(v.flag, abi.Struct)
12151215
for i, x := range index {
12161216
if i > 0 {
12171217
if v.Kind() == Pointer && v.typ().Elem().Kind() == abi.Struct {
@@ -1234,7 +1234,7 @@ func (v Value) FieldByIndexErr(index []int) (Value, error) {
12341234
if len(index) == 1 {
12351235
return v.Field(index[0]), nil
12361236
}
1237-
v.flag.MustBe(abi.Struct)
1237+
reflectlite.MustBe(v.flag, abi.Struct)
12381238
for i, x := range index {
12391239
if i > 0 {
12401240
if v.Kind() == Ptr && v.typ().Elem().Kind() == abi.Struct {
@@ -1253,7 +1253,7 @@ func (v Value) FieldByIndexErr(index []int) (Value, error) {
12531253
// It returns the zero Value if no field was found.
12541254
// It panics if v's Kind is not [Struct].
12551255
func (v Value) FieldByName(name string) Value {
1256-
v.flag.MustBe(abi.Struct)
1256+
reflectlite.MustBe(v.flag, abi.Struct)
12571257
if f, ok := toRType(v.typ()).FieldByName(name); ok {
12581258
return v.FieldByIndex(f.Index)
12591259
}
@@ -1430,7 +1430,7 @@ func valueInterface(v Value, safe bool) any {
14301430
// Deprecated: The memory representation of interface values is not
14311431
// compatible with InterfaceData.
14321432
func (v Value) InterfaceData() [2]uintptr {
1433-
v.flag.MustBe(abi.Interface)
1433+
reflectlite.MustBe(v.flag, abi.Interface)
14341434
// The compiler loses track as it converts to uintptr. Force escape.
14351435
escapes(v.ptr)
14361436
// We treat this as a read operation, so we allow
@@ -1696,7 +1696,7 @@ var stringType = rtypeOf("")
16961696
// It returns the zero Value if key is not found in the map or if v represents a nil map.
16971697
// As in Go, the key's value must be assignable to the map's key type.
16981698
func (v Value) MapIndex(key Value) Value {
1699-
v.flag.MustBe(abi.Map)
1699+
reflectlite.MustBe(v.flag, abi.Map)
17001700
tt := (*mapType)(unsafe.Pointer(v.typ()))
17011701

17021702
// Do not require key to be exported, so that DeepEqual
@@ -1735,7 +1735,7 @@ func (v Value) MapIndex(key Value) Value {
17351735
// It panics if v's Kind is not [Map].
17361736
// It returns an empty slice if v represents a nil map.
17371737
func (v Value) MapKeys() []Value {
1738-
v.flag.MustBe(abi.Map)
1738+
reflectlite.MustBe(v.flag, abi.Map)
17391739
tt := (*mapType)(unsafe.Pointer(v.typ()))
17401740
keyType := tt.Key
17411741

@@ -1907,7 +1907,7 @@ func (iter *MapIter) Next() bool {
19071907
// which may allow the previously iterated-over map to be garbage collected.
19081908
func (iter *MapIter) Reset(v Value) {
19091909
if v.IsValid() {
1910-
v.flag.MustBe(abi.Map)
1910+
reflectlite.MustBe(v.flag, abi.Map)
19111911
}
19121912
iter.m = v
19131913
iter.hiter = hiter{}
@@ -2009,7 +2009,7 @@ func (v Value) MethodByName(name string) Value {
20092009
// NumField returns the number of fields in the struct v.
20102010
// It panics if v's Kind is not [Struct].
20112011
func (v Value) NumField() int {
2012-
v.flag.MustBe(abi.Struct)
2012+
reflectlite.MustBe(v.flag, abi.Struct)
20132013
tt := (*structType)(unsafe.Pointer(v.typ()))
20142014
return len(tt.Fields)
20152015
}
@@ -2140,7 +2140,7 @@ func (v Value) Pointer() uintptr {
21402140
// The boolean value ok is true if the value x corresponds to a send
21412141
// on the channel, false if it is a zero value received because the channel is closed.
21422142
func (v Value) Recv() (x Value, ok bool) {
2143-
v.flag.MustBe(abi.Chan)
2143+
reflectlite.MustBe(v.flag, abi.Chan)
21442144
v.flag.MustBeExported()
21452145
return v.recv(false)
21462146
}
@@ -2173,7 +2173,7 @@ func (v Value) recv(nb bool) (val Value, ok bool) {
21732173
// It panics if v's kind is not [Chan] or if x's type is not the same type as v's element type.
21742174
// As in Go, x's value must be assignable to the channel's element type.
21752175
func (v Value) Send(x Value) {
2176-
v.flag.MustBe(abi.Chan)
2176+
reflectlite.MustBe(v.flag, abi.Chan)
21772177
v.flag.MustBeExported()
21782178
v.send(x, false)
21792179
}
@@ -2223,15 +2223,15 @@ func (v Value) Set(x Value) {
22232223
// It panics if v's Kind is not [Bool] or if [Value.CanSet] returns false.
22242224
func (v Value) SetBool(x bool) {
22252225
v.flag.MustBeAssignable()
2226-
v.flag.MustBe(abi.Bool)
2226+
reflectlite.MustBe(v.flag, abi.Bool)
22272227
*(*bool)(v.ptr) = x
22282228
}
22292229

22302230
// SetBytes sets v's underlying value.
22312231
// It panics if v's underlying value is not a slice of bytes.
22322232
func (v Value) SetBytes(x []byte) {
22332233
v.flag.MustBeAssignable()
2234-
v.flag.MustBe(abi.Slice)
2234+
reflectlite.MustBe(v.flag, abi.Slice)
22352235
if toRType(v.typ()).Elem().Kind() != Uint8 { // TODO add Elem method, fix mustBe(Slice) to return slice.
22362236
panic("reflect.Value.SetBytes of non-byte slice")
22372237
}
@@ -2242,7 +2242,7 @@ func (v Value) SetBytes(x []byte) {
22422242
// It panics if v's underlying value is not a slice of runes (int32s).
22432243
func (v Value) setRunes(x []rune) {
22442244
v.flag.MustBeAssignable()
2245-
v.flag.MustBe(abi.Slice)
2245+
reflectlite.MustBe(v.flag, abi.Slice)
22462246
if v.typ().Elem().Kind() != abi.Int32 {
22472247
panic("reflect.Value.setRunes of non-rune slice")
22482248
}
@@ -2302,7 +2302,7 @@ func (v Value) SetInt(x int64) {
23022302
// greater than the capacity of the slice.
23032303
func (v Value) SetLen(n int) {
23042304
v.flag.MustBeAssignable()
2305-
v.flag.MustBe(abi.Slice)
2305+
reflectlite.MustBe(v.flag, abi.Slice)
23062306
s := (*unsafeheader.Slice)(v.ptr)
23072307
if uint(n) > uint(s.Cap) {
23082308
panic("reflect: slice length out of range in SetLen")
@@ -2315,7 +2315,7 @@ func (v Value) SetLen(n int) {
23152315
// greater than the capacity of the slice.
23162316
func (v Value) SetCap(n int) {
23172317
v.flag.MustBeAssignable()
2318-
v.flag.MustBe(abi.Slice)
2318+
reflectlite.MustBe(v.flag, abi.Slice)
23192319
s := (*unsafeheader.Slice)(v.ptr)
23202320
if n < s.Len || n > s.Cap {
23212321
panic("reflect: slice capacity out of range in SetCap")
@@ -2330,7 +2330,7 @@ func (v Value) SetCap(n int) {
23302330
// As in Go, key's elem must be assignable to the map's key type,
23312331
// and elem's value must be assignable to the map's elem type.
23322332
func (v Value) SetMapIndex(key, elem Value) {
2333-
v.flag.MustBe(abi.Map)
2333+
reflectlite.MustBe(v.flag, abi.Map)
23342334
v.flag.MustBeExported()
23352335
key.flag.MustBeExported()
23362336
tt := (*mapType)(unsafe.Pointer(v.typ()))
@@ -2401,15 +2401,15 @@ func (v Value) SetUint(x uint64) {
24012401
// It panics if v's Kind is not UnsafePointer.
24022402
func (v Value) SetPointer(x unsafe.Pointer) {
24032403
v.flag.MustBeAssignable()
2404-
v.flag.MustBe(abi.UnsafePointer)
2404+
reflectlite.MustBe(v.flag, abi.UnsafePointer)
24052405
*(*unsafe.Pointer)(v.ptr) = x
24062406
}
24072407

24082408
// SetString sets v's underlying value to x.
24092409
// It panics if v's Kind is not [String] or if [Value.CanSet] returns false.
24102410
func (v Value) SetString(x string) {
24112411
v.flag.MustBeAssignable()
2412-
v.flag.MustBe(abi.String)
2412+
reflectlite.MustBe(v.flag, abi.String)
24132413
*(*string)(v.ptr) = x
24142414
}
24152415

@@ -2556,7 +2556,7 @@ func (v Value) stringNonString() string {
25562556
// If the receive cannot finish without blocking, x is the zero Value and ok is false.
25572557
// If the channel is closed, x is the zero value for the channel's element type and ok is false.
25582558
func (v Value) TryRecv() (x Value, ok bool) {
2559-
v.flag.MustBe(abi.Chan)
2559+
reflectlite.MustBe(v.flag, abi.Chan)
25602560
v.flag.MustBeExported()
25612561
return v.recv(true)
25622562
}
@@ -2566,7 +2566,7 @@ func (v Value) TryRecv() (x Value, ok bool) {
25662566
// It reports whether the value was sent.
25672567
// As in Go, x's value must be assignable to the channel's element type.
25682568
func (v Value) TrySend(x Value) bool {
2569-
v.flag.MustBe(abi.Chan)
2569+
reflectlite.MustBe(v.flag, abi.Chan)
25702570
v.flag.MustBeExported()
25712571
return v.send(x, true)
25722572
}
@@ -2766,7 +2766,7 @@ func arrayAt(p unsafe.Pointer, i int, eltSize uintptr, whySafe string) unsafe.Po
27662766
// allocate the memory.
27672767
func (v Value) Grow(n int) {
27682768
v.flag.MustBeAssignable()
2769-
v.flag.MustBe(abi.Slice)
2769+
reflectlite.MustBe(v.flag, abi.Slice)
27702770
v.grow(n)
27712771
}
27722772

@@ -2792,7 +2792,7 @@ func (v Value) grow(n int) {
27922792
// incremented by the number of specified elements.
27932793
func (v Value) extendSlice(n int) Value {
27942794
v.flag.MustBeExported()
2795-
v.flag.MustBe(abi.Slice)
2795+
reflectlite.MustBe(v.flag, abi.Slice)
27962796

27972797
// Shallow copy the slice header to avoid mutating the source slice.
27982798
sh := *(*unsafeheader.Slice)(v.ptr)
@@ -2824,7 +2824,7 @@ func (v Value) Clear() {
28242824
// Append appends the values x to a slice s and returns the resulting slice.
28252825
// As in Go, each x's value must be assignable to the slice's element type.
28262826
func Append(s Value, x ...Value) Value {
2827-
s.flag.MustBe(abi.Slice)
2827+
reflectlite.MustBe(s.flag, abi.Slice)
28282828
n := s.Len()
28292829
s = s.extendSlice(len(x))
28302830
for i, v := range x {
@@ -2836,8 +2836,8 @@ func Append(s Value, x ...Value) Value {
28362836
// AppendSlice appends a slice t to a slice s and returns the resulting slice.
28372837
// The slices s and t must have the same element type.
28382838
func AppendSlice(s, t Value) Value {
2839-
s.flag.MustBe(abi.Slice)
2840-
t.flag.MustBe(abi.Slice)
2839+
reflectlite.MustBe(s.flag, abi.Slice)
2840+
reflectlite.MustBe(s.flag, abi.Slice)
28412841
typesMustMatch("reflect.AppendSlice", s.Type().Elem(), t.Type().Elem())
28422842
ns := s.Len()
28432843
nt := t.Len()
@@ -3008,7 +3008,7 @@ func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool) {
30083008
if !ch.IsValid() {
30093009
break
30103010
}
3011-
ch.flag.MustBe(abi.Chan)
3011+
reflectlite.MustBe(ch.flag, abi.Chan)
30123012
ch.flag.MustBeExported()
30133013
tt := (*chanType)(unsafe.Pointer(ch.typ()))
30143014
if ChanDir(tt.Dir)&SendDir == 0 {
@@ -3039,7 +3039,7 @@ func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool) {
30393039
if !ch.IsValid() {
30403040
break
30413041
}
3042-
ch.flag.MustBe(abi.Chan)
3042+
reflectlite.MustBe(ch.flag, abi.Chan)
30433043
ch.flag.MustBeExported()
30443044
tt := (*chanType)(unsafe.Pointer(ch.typ()))
30453045
if ChanDir(tt.Dir)&RecvDir == 0 {
@@ -3898,7 +3898,7 @@ func noescape(p unsafe.Pointer) unsafe.Pointer {
38983898
func kindAsFlag[T abi.Kind | Kind | int](k T) reflectlite.Flag { return reflectlite.Flag(k) }
38993899

39003900
func init() {
3901-
reflectlite.NewValueError = func(Method string, kind abi.Kind) interface{} {
3901+
reflectlite.NewValueError = func(Method string, kind abi.Kind) any {
39023902
return &ValueError{Method: Method, Kind: Kind(kind)}
39033903
}
39043904
}

0 commit comments

Comments
 (0)