&& TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type, 0)));
}
+/* Return true if TYPE, which must be an enum type, has no
+ variants. */
+
+static bool
+rust_empty_enum_p (const struct type *type)
+{
+ gdb_assert (rust_enum_p (type));
+ /* In Rust the enum always fills the containing structure. */
+ gdb_assert (TYPE_FIELD_BITPOS (type, 0) == 0);
+
+ return TYPE_NFIELDS (TYPE_FIELD_TYPE (type, 0)) == 0;
+}
+
/* Given an enum type and contents, find which variant is active. */
-struct field *
+static struct field *
rust_enum_variant (struct type *type, const gdb_byte *contents)
{
/* In Rust the enum always fills the containing structure. */
opts.deref_ref = 0;
+ if (rust_empty_enum_p (type))
+ {
+ /* Print the enum type name here to be more clear. */
+ fprintf_filtered (stream, _("%s {<No data fields>}"), TYPE_NAME (type));
+ return;
+ }
+
const gdb_byte *valaddr = value_contents_for_printing (val);
struct field *variant_field = rust_enum_variant (type, valaddr);
embedded_offset += FIELD_BITPOS (*variant_field) / 8;
if (is_enum)
{
fputs_filtered ("enum ", stream);
+
+ if (rust_empty_enum_p (type))
+ {
+ if (tagname != NULL)
+ {
+ fputs_filtered (tagname, stream);
+ fputs_filtered (" ", stream);
+ }
+ fputs_filtered ("{}", stream);
+ return;
+ }
+
type = TYPE_FIELD_TYPE (type, 0);
struct dynamic_prop *discriminant_prop
if (rust_enum_p (type))
{
+ if (rust_empty_enum_p (type))
+ error (_("Cannot access field %d of empty enum %s"),
+ field_number, TYPE_NAME (type));
+
const gdb_byte *valaddr = value_contents (lhs);
struct field *variant_field = rust_enum_variant (type, valaddr);
type = value_type (lhs);
if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type))
{
+ if (rust_empty_enum_p (type))
+ error (_("Cannot access field %s of empty enum %s"),
+ field_name, TYPE_NAME (type));
+
const gdb_byte *valaddr = value_contents (lhs);
struct field *variant_field = rust_enum_variant (type, valaddr);
" }"
}
+# PR rust/23626 - this used to crash. Note that the results are
+# fairly lax because most existing versions of Rust (those before the
+# DW_TAG_variant patches) do not emit what gdb wants here; and there
+# was little point fixing gdb to cope with these cases as the fixed
+# compilers will be available soon
+gdb_test "print empty_enum_value" \
+ " = simple::EmptyEnum.*"
+gdb_test "ptype empty_enum_value" "simple::EmptyEnum.*"
+# Just make sure these don't crash, for the same reason.
+gdb_test "print empty_enum_value.0" ""
+gdb_test "print empty_enum_value.something" ""
+
load_lib gdb-python.exp
if {[skip_python_tests]} {
continue