Special case "&str" in Rust parser

"&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
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
This commit is contained in:
Tom Tromey 2023-05-17 09:07:50 -06:00
parent 80d4e113d7
commit cd61a1453e
2 changed files with 13 additions and 0 deletions

View File

@ -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 ();

View File

@ -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\\\]"