diff --git a/upb/decode.c b/upb/decode.c index dae9b3d1aa..f1be7502c3 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -492,19 +492,7 @@ static bool upb_decode_mapfield(upb_decstate *d, upb_decframe *frame, const upb_msglayout_field *field, int len) { upb_map *map = *(upb_map**)&frame->msg[field->offset]; const upb_msglayout *entry = frame->layout->submsgs[field->submsg_index]; - - /* The compiler ensures that all map entry messages have this layout. */ - struct map_entry { - upb_msg_internal internal; - union { - upb_strview str; /* For str/bytes. */ - upb_value val; /* For all other types. */ - } k; - union { - upb_strview str; /* For str/bytes. */ - upb_value val; /* For all other types. */ - } v; - } ent; + upb_map_entry ent; if (!map) { /* Lazily create map. */ diff --git a/upb/def.c b/upb/def.c index 12bb104bed..1a83687788 100644 --- a/upb/def.c +++ b/upb/def.c @@ -869,10 +869,9 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) { static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) { if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) { - /* Map entries aren't actually stored, they are only used during parsing. - * For parsing, it helps a lot if all map entry messages have the same - * layout. */ - return sizeof(upb_strview); + upb_map_entry ent; + UPB_ASSERT(sizeof(ent.k) == sizeof(ent.v)); + return sizeof(ent.k); } else if (upb_fielddef_isseq(f)) { return sizeof(void*); } else { diff --git a/upb/msg.h b/upb/msg.h index b66730f11e..2b3274b6da 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -200,6 +200,21 @@ typedef struct { upb_strtable table; } upb_map; +/* Map entries aren't actually stored, they are only used during parsing. For + * parsing, it helps a lot if all map entry messages have the same layout. + * The compiler and def.c must ensure that all map entries have this layout. */ +typedef struct { + upb_msg_internal internal; + union { + upb_strview str; /* For str/bytes. */ + upb_value val; /* For all other types. */ + } k; + union { + upb_strview str; /* For str/bytes. */ + upb_value val; /* For all other types. */ + } v; +} upb_map_entry; + /* Creates a new map on the given arena with this key/value type. */ upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size);