summaryrefslogtreecommitdiffhomepage
path: root/pkg
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2021-01-06 02:23:38 +0000
committergVisor bot <gvisor-bot@google.com>2021-01-06 02:23:38 +0000
commitc4a1f8adb649cba3f31d4f286656defdf6ea57ee (patch)
tree9b454ca9e15c8ae13945dbb14265e12ee5aa1575 /pkg
parentbc54e6da95eac5df5977d6aa8eccb1ccc21440a4 (diff)
parentab32fa2481d84e3f390f2cd81e25c537756b7aa1 (diff)
Merge release-20201208.0-112-gab32fa248 (automated)
Diffstat (limited to 'pkg')
-rw-r--r--pkg/state/types.go71
1 files changed, 36 insertions, 35 deletions
diff --git a/pkg/state/types.go b/pkg/state/types.go
index 84aed8732..420675880 100644
--- a/pkg/state/types.go
+++ b/pkg/state/types.go
@@ -329,47 +329,48 @@ var reverseTypeDatabase = map[reflect.Type]string{}
// This must be called on init and only done once.
func Register(t Type) {
name := t.StateTypeName()
- fields := t.StateFields()
- assertValidType(name, fields)
- // Register must always be called on pointers.
typ := reflect.TypeOf(t)
- if typ.Kind() != reflect.Ptr {
- Failf("Register must be called on pointers")
+ if raceEnabled {
+ assertValidType(name, t.StateFields())
+ // Register must always be called on pointers.
+ if typ.Kind() != reflect.Ptr {
+ Failf("Register must be called on pointers")
+ }
}
typ = typ.Elem()
- if typ.Kind() == reflect.Struct {
- // All registered structs must implement SaverLoader. We allow
- // the registration is non-struct types with just the Type
- // interface, but we need to call StateSave/StateLoad methods
- // on aggregate types.
- if _, ok := t.(SaverLoader); !ok {
- Failf("struct %T does not implement SaverLoader", t)
+ if raceEnabled {
+ if typ.Kind() == reflect.Struct {
+ // All registered structs must implement SaverLoader. We allow
+ // the registration is non-struct types with just the Type
+ // interface, but we need to call StateSave/StateLoad methods
+ // on aggregate types.
+ if _, ok := t.(SaverLoader); !ok {
+ Failf("struct %T does not implement SaverLoader", t)
+ }
+ } else {
+ // Non-structs must not have any fields. We don't support
+ // calling StateSave/StateLoad methods on any non-struct types.
+ // If custom behavior is required, these types should be
+ // wrapped in a structure of some kind.
+ if fields := t.StateFields(); len(fields) != 0 {
+ Failf("non-struct %T has non-zero fields %v", t, fields)
+ }
+ // We don't allow non-structs to implement StateSave/StateLoad
+ // methods, because they won't be called and it's confusing.
+ if _, ok := t.(SaverLoader); ok {
+ Failf("non-struct %T implements SaverLoader", t)
+ }
}
- } else {
- // Non-structs must not have any fields. We don't support
- // calling StateSave/StateLoad methods on any non-struct types.
- // If custom behavior is required, these types should be
- // wrapped in a structure of some kind.
- if len(fields) != 0 {
- Failf("non-struct %T has non-zero fields %v", t, fields)
+ if _, ok := primitiveTypeDatabase[name]; ok {
+ Failf("conflicting primitiveTypeDatabase entry for %T: used by primitive", t)
}
- // We don't allow non-structs to implement StateSave/StateLoad
- // methods, because they won't be called and it's confusing.
- if _, ok := t.(SaverLoader); ok {
- Failf("non-struct %T implements SaverLoader", t)
+ if _, ok := globalTypeDatabase[name]; ok {
+ Failf("conflicting globalTypeDatabase entries for %T: name conflict", t)
+ }
+ if name == interfaceType {
+ Failf("conflicting name for %T: matches interfaceType", t)
}
- }
- if _, ok := primitiveTypeDatabase[name]; ok {
- Failf("conflicting primitiveTypeDatabase entry for %T: used by primitive", t)
- }
- if _, ok := globalTypeDatabase[name]; ok {
- Failf("conflicting globalTypeDatabase entries for %T: name conflict", t)
- }
- if name == interfaceType {
- Failf("conflicting name for %T: matches interfaceType", t)
- }
- globalTypeDatabase[name] = typ
- if raceEnabled {
reverseTypeDatabase[typ] = name
}
+ globalTypeDatabase[name] = typ
}