Conditionally drop the discriminant field in quirk_rust_enum
authorTom Tromey <tom@tromey.com>
Thu, 12 Apr 2018 14:05:16 +0000 (08:05 -0600)
committerTom Tromey <tom@tromey.com>
Tue, 17 Apr 2018 19:37:44 +0000 (13:37 -0600)
While debugging the crash that Jan reported, I noticed that in some
situations we could end up with a situation where one branch of a Rust
enum type ended up with a field count of -1.

The fix is simple: only conditionally drop the discriminant field when
rewriting the enum variants.

I couldn't find a way to test this; I only noticed it while debugging
the DWARF reader.

2018-04-17  Tom Tromey  <tom@tromey.com>

* dwarf2read.c (quirk_rust_enum): Conditionally drop the
discriminant field.

gdb/ChangeLog
gdb/dwarf2read.c

index 35072e85db02817dcd58117ae9fba28daae043b4..55bdd14895175ed5910047195847f92272401747 100644 (file)
@@ -1,3 +1,8 @@
+2018-04-17  Tom Tromey  <tom@tromey.com>
+
+       * dwarf2read.c (quirk_rust_enum): Conditionally drop the
+       discriminant field.
+
 2018-04-17  Tom Tromey  <tom@tromey.com>
 
        * dwarf2read.c (quirk_rust_enum): Handle unions correctly.
index 0d3af00c463d752608bb9e8fb809a9312de3b6d4..4207e4c53172261cdfe92fde311c8f9a6c90687e 100644 (file)
@@ -10079,10 +10079,13 @@ quirk_rust_enum (struct type *type, struct objfile *objfile)
          if (iter != discriminant_map.end ())
            disc->discriminants[i] = iter->second;
 
-         /* Remove the discriminant field.  */
+         /* Remove the discriminant field, if it exists.  */
          struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
-         --TYPE_NFIELDS (sub_type);
-         ++TYPE_FIELDS (sub_type);
+         if (TYPE_NFIELDS (sub_type) > 0)
+           {
+             --TYPE_NFIELDS (sub_type);
+             ++TYPE_FIELDS (sub_type);
+           }
          TYPE_FIELD_NAME (union_type, i) = variant_name;
          TYPE_NAME (sub_type)
            = rust_fully_qualify (&objfile->objfile_obstack,