Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Manish Goregaokar <manish@mozilla.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] Fix handling of discriminantless univariant enums in Rust
Date: Sun, 30 Oct 2016 03:40:00 -0000	[thread overview]
Message-ID: <CAFOnWk=cUZfUzn9a4qp_nw8aFxkGWx3TSOGapH7HmOfWAeoSRg@mail.gmail.com> (raw)
In-Reply-To: <CAFOnWkk0up4VFozB3wCLYPsjkHVzg8BjYuM4fGp9GT2eGXe37w@mail.gmail.com>

Rolled up patch:

From 789f95700e43b29d0c374985f8504eb414c85eec Mon Sep 17 00:00:00 2001
From: Manish Goregaokar <manish@mozilla.com>
Date: Thu, 27 Oct 2016 16:46:34 -0700
Subject: [PATCH] Fix handling of discriminantless univariant enums in Rust;
 fix bug with encoded enums

2016-10-27  Manish Goregaokar  <manish@mozilla.com>

gdb/ChangeLog:
    * rust-lang.c (rust_get_disr_info): Treat univariant enums
    without discriminants as encoded enums with a real field
    * rust-lang.c (rust_evaluate_subexp): Handle field access
    on encoded struct-like enums

gdb/testsuite/ChangeLog:
    * simple.rs: Add test for univariant enums without discriminants
    and for encoded struct-like enums
    * simple.exp: Add test expectations
---
 gdb/rust-lang.c                   | 17 +++++++++++++++--
 gdb/testsuite/gdb.rust/simple.exp | 11 +++++++++++
 gdb/testsuite/gdb.rust/simple.rs  | 30 ++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 82cd3f9..63c7992 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -194,7 +194,18 @@ rust_get_disr_info (struct type *type, const
gdb_byte *valaddr,
      has changed its debuginfo format.  */
       error (_("Could not find enum discriminant field"));
     }
-
+  else if (TYPE_NFIELDS (type) == 1) {
+    /* Sometimes univariant enums are encoded without a
+    discriminant. In that case, treating it as an encoded enum
+    with the first field being the actual type works.  */
+    const char* field_name = TYPE_NAME (TYPE_FIELD_TYPE (type, 0));
+    ret.name = concat (TYPE_NAME (type), "::",
+                       rust_last_path_segment (field_name),
+                       (char *) NULL);
+    ret.field_no = RUST_ENCODED_ENUM_REAL;
+    ret.is_encoded = 1;
+    return ret;
+  }
   if (strcmp (TYPE_FIELD_NAME (disr_type, 0), "RUST$ENUM$DISR") != 0)
     error (_("Rust debug format has changed"));

@@ -1725,7 +1736,9 @@ tuple structs, and tuple-like enum variants"));
         variant_type = TYPE_FIELD_TYPE (type, disr.field_no);

         if (variant_type == NULL
-        || rust_tuple_variant_type_p (variant_type))
+            || (disr.is_encoded
+                ? rust_tuple_struct_type_p (variant_type)
+                : rust_tuple_variant_type_p (variant_type)))
           error(_("Attempting to access named field %s of tuple variant %s, \
 which has only anonymous fields"),
             field_name, disr.name);
diff --git a/gdb/testsuite/gdb.rust/simple.exp
b/gdb/testsuite/gdb.rust/simple.exp
index 5e00b03..14eae2e 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -103,6 +103,11 @@ gdb_test_sequence "ptype z" "" {
 }
 gdb_test "print z.1" " = 8"

+gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
+gdb_test "print univariant.a" " = 1"
+gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
+gdb_test "print univariant_anon.0" " = 1"
+
 gdb_test_sequence "ptype simple::ByeBob" "" {
     " = struct simple::ByeBob \\("
     "  i32,"
@@ -220,3 +225,9 @@ gdb_test "print (1,)" "Tuple expressions not supported yet"
 gdb_test "print (1)" " = 1"

 gdb_test "print 23..97.0" "Range expression with different types"
+
+gdb_test "print (*parametrized.next.val)" \
+    " = simple::ParametrizedStruct<i32> {next:
simple::ParametrizedEnum<Box<simple::ParametrizedStruct<i32>>>::Empty,
value: 1}"
+gdb_test "print parametrized.next.val" " =
\\(simple::ParametrizedStruct<i32> \\*\\) $hex"
+gdb_test "print parametrized" \
+    " = simple::ParametrizedStruct<i32> \\{next:
simple::ParametrizedEnum<Box<simple::ParametrizedStruct<i32>>>::Val\\{val:
$hex\\}, value: 0\\}"
\ No newline at end of file
diff --git a/gdb/testsuite/gdb.rust/simple.rs b/gdb/testsuite/gdb.rust/simple.rs
index eeff3d7..670f54e 100644
--- a/gdb/testsuite/gdb.rust/simple.rs
+++ b/gdb/testsuite/gdb.rust/simple.rs
@@ -63,6 +63,23 @@ enum SpaceSaver {
     Nothing,
 }

+enum Univariant {
+    Foo {a: u8}
+}
+enum UnivariantAnon {
+    Foo(u8)
+}
+
+enum ParametrizedEnum<T> {
+    Val { val: T },
+    Empty,
+}
+
+struct ParametrizedStruct<T> {
+    next: ParametrizedEnum<Box<ParametrizedStruct<T>>>,
+    value: T
+}
+
 fn main () {
     let a = ();
     let b : [i32; 0] = [];
@@ -93,6 +110,9 @@ fn main () {
     let y = HiBob {field1: 7, field2: 8};
     let z = ByeBob(7, 8);

+    let univariant = Univariant::Foo {a : 1};
+    let univariant_anon = UnivariantAnon::Foo(1);
+
     let slice = &w[2..3];
     let fromslice = slice[0];
     let slice2 = &slice[0..1];
@@ -117,6 +137,16 @@ fn main () {
     let custom_some = NonZeroOptimized::Value("hi".into());
     let custom_none = NonZeroOptimized::Empty;

+    let parametrized = ParametrizedStruct {
+        next: ParametrizedEnum::Val {
+            val: Box::new(ParametrizedStruct {
+                next: ParametrizedEnum::Empty,
+                value: 1,
+            })
+        },
+        value: 0,
+    };
+
     println!("{}, {}", x.0, x.1);        // set breakpoint here
     println!("{}", diff2(92, 45));
     empty();
-- 
2.10.1

-Manish


On Sat, Oct 29, 2016 at 8:35 PM, Manish Goregaokar <manish@mozilla.com> wrote:
> Yes, I'm trying it with the followup patch. I kept them separate since
> that's an independent bug that just affects univariant ones too. Will
> roll them together.
> -Manish
>
>
> On Sat, Oct 29, 2016 at 8:34 PM, Tom Tromey <tom@tromey.com> wrote:
>>>>>>> "Manish" == Manish Goregaokar <manish@mozilla.com> writes:
>>
>> Manish> `print univariant.a` works for me.
>>
>> I tried this patch and it failed the way I thought it would:
>>
>> print univariant.a
>> Attempting to access named field a of tuple variant simple::Univariant::Foo, which has only anonymous fields
>> (gdb) FAIL: gdb.rust/simple.exp: print univariant.a
>>
>>
>> Maybe you're trying it with the follow-up patch installed?
>> In that case I think the patches should probably be combined.
>>
>> Tom


  reply	other threads:[~2016-10-30  3:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-27 23:52 Manish Goregaokar
2016-10-28 19:57 ` Manish Goregaokar
2016-10-30  2:48 ` Tom Tromey
2016-10-30  2:53   ` Tom Tromey
2016-10-30  2:58     ` Manish Goregaokar
2016-10-30  3:06       ` Manish Goregaokar
2016-10-30  3:34       ` Tom Tromey
2016-10-30  3:36         ` Manish Goregaokar
2016-10-30  3:40           ` Manish Goregaokar [this message]
2016-10-31  3:03             ` Tom Tromey
2016-10-31  3:20               ` Manish Goregaokar
2016-10-31  3:29                 ` Tom Tromey
2016-10-31  3:47                   ` Manish Goregaokar
2016-10-31  6:17                     ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFOnWk=cUZfUzn9a4qp_nw8aFxkGWx3TSOGapH7HmOfWAeoSRg@mail.gmail.com' \
    --to=manish@mozilla.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox