public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Special case "&str" in Rust parser
@ 2023-05-17 15:10 Tom Tromey
  2023-05-17 15:59 ` Andrew Burgess
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2023-05-17 15:10 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

"&str" is an important type in Rust -- it's the type of string
literals.  However, the compiler puts it in the DWARF in a funny way.
The slice itself is present and named "&str".  However, the Rust
parser doesn't look for types with names like this, but instead tries
to construct them from components.  In this case it tries to make a
pointer-to-"str" -- but "str" isn't always available, and in any case
that wouldn't yield the best result.

This patch adds a special case for &str.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22251
---
 gdb/rust-parse.c                  | 10 ++++++++++
 gdb/testsuite/gdb.rust/simple.exp |  3 +++
 2 files changed, 13 insertions(+)

diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 648e48dda40..427169611d3 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -1682,6 +1682,16 @@ rust_parser::parse_slice_type ()
 {
   assume ('&');
 
+  /* Handle &str specially.  This is an important type in Rust.  While
+     the compiler does emit the "&str" type in the DWARF, just "str"
+     itself isn't always available -- but it's handy if this works
+     seamlessly.  */
+  if (current_token == IDENT && get_string () == "str")
+    {
+      lex ();
+      return rust_slice_type ("&str", get_type ("u8"), get_type ("usize"));
+    }
+
   bool is_slice = current_token == '[';
   if (is_slice)
     lex ();
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
index 08ebed3f103..a615b92ec47 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -68,6 +68,9 @@ gdb_test "print simple::Unit{23}" "'}', '\.\.', or identifier expected"
 gdb_test "print f" " = \"hi bob\""
 gdb_test "print fslice" " = \"bob\""
 gdb_test "print &f\[3..\]" " = \"bob\""
+gdb_test "whatis f" "type = &str"
+gdb_test "print *(&f as *mut &str)" " = \"hi bob\"" \
+    "print via cast to &str"
 
 gdb_test "print g" " = \\(\\*mut \\\[u8; 6\\\]\\) $hex b\"hi bob\""
 gdb_test "ptype g" " = \\*mut \\\[u8; 6\\\]"
-- 
2.39.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Special case "&str" in Rust parser
  2023-05-17 15:10 [PATCH] Special case "&str" in Rust parser Tom Tromey
@ 2023-05-17 15:59 ` Andrew Burgess
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Burgess @ 2023-05-17 15:59 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches; +Cc: Tom Tromey

Tom Tromey <tom@tromey.com> writes:

> "&str" is an important type in Rust -- it's the type of string
> literals.  However, the compiler puts it in the DWARF in a funny way.
> The slice itself is present and named "&str".  However, the Rust
> parser doesn't look for types with names like this, but instead tries
> to construct them from components.  In this case it tries to make a
> pointer-to-"str" -- but "str" isn't always available, and in any case
> that wouldn't yield the best result.
>
> This patch adds a special case for &str.
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22251

LGTM.

Reviewed-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew


> ---
>  gdb/rust-parse.c                  | 10 ++++++++++
>  gdb/testsuite/gdb.rust/simple.exp |  3 +++
>  2 files changed, 13 insertions(+)
>
> diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
> index 648e48dda40..427169611d3 100644
> --- a/gdb/rust-parse.c
> +++ b/gdb/rust-parse.c
> @@ -1682,6 +1682,16 @@ rust_parser::parse_slice_type ()
>  {
>    assume ('&');
>  
> +  /* Handle &str specially.  This is an important type in Rust.  While
> +     the compiler does emit the "&str" type in the DWARF, just "str"
> +     itself isn't always available -- but it's handy if this works
> +     seamlessly.  */
> +  if (current_token == IDENT && get_string () == "str")
> +    {
> +      lex ();
> +      return rust_slice_type ("&str", get_type ("u8"), get_type ("usize"));
> +    }
> +
>    bool is_slice = current_token == '[';
>    if (is_slice)
>      lex ();
> diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
> index 08ebed3f103..a615b92ec47 100644
> --- a/gdb/testsuite/gdb.rust/simple.exp
> +++ b/gdb/testsuite/gdb.rust/simple.exp
> @@ -68,6 +68,9 @@ gdb_test "print simple::Unit{23}" "'}', '\.\.', or identifier expected"
>  gdb_test "print f" " = \"hi bob\""
>  gdb_test "print fslice" " = \"bob\""
>  gdb_test "print &f\[3..\]" " = \"bob\""
> +gdb_test "whatis f" "type = &str"
> +gdb_test "print *(&f as *mut &str)" " = \"hi bob\"" \
> +    "print via cast to &str"
>  
>  gdb_test "print g" " = \\(\\*mut \\\[u8; 6\\\]\\) $hex b\"hi bob\""
>  gdb_test "ptype g" " = \\*mut \\\[u8; 6\\\]"
> -- 
> 2.39.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-05-17 15:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-17 15:10 [PATCH] Special case "&str" in Rust parser Tom Tromey
2023-05-17 15:59 ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).