public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [FYI 0/3] fix a few slice-related bugs in Rust support
@ 2017-10-02 20:10 Tom Tromey
  2017-10-02 20:11 ` [FYI 2/3] Fix ptype of Rust slices Tom Tromey
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tom Tromey @ 2017-10-02 20:10 UTC (permalink / raw)
  To: gdb-patches

This series fixes a few slice-related bugs in the Rust support.  Two
were missing features, and one was a crasher.

Built and regression tested locally against the stable Rust compiler,
on x86-64 Fedora 25.

I'm checking this in.

Tom

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

* [FYI 2/3] Fix ptype of Rust slices
  2017-10-02 20:10 [FYI 0/3] fix a few slice-related bugs in Rust support Tom Tromey
@ 2017-10-02 20:11 ` Tom Tromey
  2017-10-02 20:11 ` [FYI 3/3] Fix &str printing in Rust Tom Tromey
  2017-10-02 20:11 ` [FYI 1/3] Allow indexing of &str " Tom Tromey
  2 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2017-10-02 20:11 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Something like "ptype &x[..]" (where "x" was a slice) would crash gdb.
rust_subscript wasn't handling slicing in the EVAL_AVOID_SIDE_EFFECTS
case.

2017-10-02  Tom Tromey  <tom@tromey.com>

	* rust-lang.c (rust_subscript): Handle slices in
	EVAL_AVOID_SIDE_EFFECTS case.

2017-10-02  Tom Tromey  <tom@tromey.com>

	* gdb.rust/simple.exp: Test ptype of a slice.
---
 gdb/ChangeLog                     |  5 +++++
 gdb/rust-lang.c                   | 42 ++++++++++++++++++++++++++++++++++++---
 gdb/testsuite/ChangeLog           |  4 ++++
 gdb/testsuite/gdb.rust/simple.exp | 19 ++++++++++++++++++
 4 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a8820c8..11c1ca3 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@
 2017-10-02  Tom Tromey  <tom@tromey.com>
 
+	* rust-lang.c (rust_subscript): Handle slices in
+	EVAL_AVOID_SIDE_EFFECTS case.
+
+2017-10-02  Tom Tromey  <tom@tromey.com>
+
 	* rust-lang.c (rust_slice_type_p): Recognize &str as a slice type.
 
 2017-10-02  Tom Tromey  <tom@tromey.com>
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index a9895fe..9b64efa 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -1456,17 +1456,53 @@ rust_subscript (struct expression *exp, int *pos, enum noside noside,
   else
     low = value_as_long (rhs);
 
+  struct type *type = check_typedef (value_type (lhs));
   if (noside == EVAL_AVOID_SIDE_EFFECTS)
     {
-      struct type *type = check_typedef (value_type (lhs));
+      struct type *base_type = nullptr;
+      if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
+	base_type = TYPE_TARGET_TYPE (type);
+      else if (rust_slice_type_p (type))
+	{
+	  for (int i = 0; i < TYPE_NFIELDS (type); ++i)
+	    {
+	      if (strcmp (TYPE_FIELD_NAME (type, i), "data_ptr") == 0)
+		{
+		  base_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, i));
+		  break;
+		}
+	    }
+	  if (base_type == nullptr)
+	    error (_("Could not find 'data_ptr' in slice type"));
+	}
+      else if (TYPE_CODE (type) == TYPE_CODE_PTR)
+	base_type = TYPE_TARGET_TYPE (type);
+      else
+	error (_("Cannot subscript non-array type"));
+
+      struct type *new_type;
+      if (want_slice)
+	{
+	  if (rust_slice_type_p (type))
+	    new_type = type;
+	  else
+	    {
+	      struct type *usize
+		= language_lookup_primitive_type (exp->language_defn,
+						  exp->gdbarch,
+						  "usize");
+	      new_type = rust_slice_type ("&[*gdb*]", base_type, usize);
+	    }
+	}
+      else
+	new_type = base_type;
 
-      result = value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (lhs));
+      return value_zero (new_type, VALUE_LVAL (lhs));
     }
   else
     {
       LONGEST low_bound;
       struct value *base;
-      struct type *type = check_typedef (value_type (lhs));
 
       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
 	{
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index edc5079..296878c 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,9 @@
 2017-10-02  Tom Tromey  <tom@tromey.com>
 
+	* gdb.rust/simple.exp: Test ptype of a slice.
+
+2017-10-02  Tom Tromey  <tom@tromey.com>
+
 	* gdb.rust/simple.exp: Test index of slice.
 
 2017-09-27  Tom Tromey  <tom@tromey.com>
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
index 1a46317..b01841f 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -85,6 +85,25 @@ gdb_test "print fromslice" " = 3"
 gdb_test "print slice\[0\]" " = 3"
 gdb_test "print slice as &\[i32\]\[0\]" " = 3"
 
+gdb_test_sequence "ptype slice" "" {
+    " = struct &\\\[i32\\\] \\{"
+    "  data_ptr: i32 \\*,"
+    "  length: usize,"
+    "\\}"
+}
+gdb_test_sequence "ptype &slice\[..\]" "" {
+    " = struct &\\\[i32\\\] \\{"
+    "  data_ptr: i32 \\*,"
+    "  length: usize,"
+    "\\}"
+}
+gdb_test_sequence "ptype &b\[..\]" "" {
+    " = struct &\\\[\\*gdb\\*\\\] \\{"
+    "  data_ptr: i32 \\*,"
+    "  length: usize,"
+    "\\}"
+}
+
 gdb_test "print x" " = \\(23, 25\\.5\\)"
 gdb_test "ptype x" " = \\(i32, f64\\)"
 gdb_test "print x as (i32,f64)" " = \\(23, 25\\.5\\)"
-- 
2.9.5

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

* [FYI 3/3] Fix &str printing in Rust
  2017-10-02 20:10 [FYI 0/3] fix a few slice-related bugs in Rust support Tom Tromey
  2017-10-02 20:11 ` [FYI 2/3] Fix ptype of Rust slices Tom Tromey
@ 2017-10-02 20:11 ` Tom Tromey
  2017-10-02 20:11 ` [FYI 1/3] Allow indexing of &str " Tom Tromey
  2 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2017-10-02 20:11 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Printing a string slice ("&str") in Rust would print until the
terminating \0; but that is incorrect because a slice has a length.
This fixes &str printing, and arranges to preserve the type name when
slicing a slice, so that printing a slice of an "&str" works as well.

This is PR rust/22236.

2017-10-02  Tom Tromey  <tom@tromey.com>

	PR rust/22236:
	* rust-lang.c (rust_val_print_str): New function.
	(val_print_struct): Call it.
	(rust_subscript): Preserve name of slice type.

2017-10-02  Tom Tromey  <tom@tromey.com>

	PR rust/22236:
	* gdb.rust/simple.rs (main): New variable "fslice".
	* gdb.rust/simple.exp: Add slice tests.  Update string tests.
---
 gdb/ChangeLog                     |  7 +++++++
 gdb/rust-lang.c                   | 29 +++++++++++++++++++++++++++--
 gdb/testsuite/ChangeLog           |  6 ++++++
 gdb/testsuite/gdb.rust/simple.exp | 12 ++++++------
 gdb/testsuite/gdb.rust/simple.rs  |  2 ++
 5 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 11c1ca3..e0cf4c0 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,12 @@
 2017-10-02  Tom Tromey  <tom@tromey.com>
 
+	PR rust/22236:
+	* rust-lang.c (rust_val_print_str): New function.
+	(val_print_struct): Call it.
+	(rust_subscript): Preserve name of slice type.
+
+2017-10-02  Tom Tromey  <tom@tromey.com>
+
 	* rust-lang.c (rust_subscript): Handle slices in
 	EVAL_AVOID_SIDE_EFFECTS case.
 
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 9b64efa..261ddb1 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -467,6 +467,21 @@ rust_printstr (struct ui_file *stream, struct type *type,
 
 \f
 
+/* Helper function to print a string slice.  */
+
+static void
+rust_val_print_str (struct ui_file *stream, struct value *val,
+		    const struct value_print_options *options)
+{
+  struct value *base = value_struct_elt (&val, NULL, "data_ptr", NULL,
+					 "slice");
+  struct value *len = value_struct_elt (&val, NULL, "length", NULL, "slice");
+
+  val_print_string (TYPE_TARGET_TYPE (value_type (base)), "UTF-8",
+		    value_as_address (base), value_as_long (len), stream,
+		    options);
+}
+
 /* rust_print_type branch for structs and untagged unions.  */
 
 static void
@@ -477,6 +492,13 @@ val_print_struct (struct type *type, int embedded_offset,
 {
   int i;
   int first_field;
+
+  if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
+    {
+      rust_val_print_str (stream, val, options);
+      return;
+    }
+
   bool is_tuple = rust_tuple_type_p (type);
   bool is_tuple_struct = !is_tuple && rust_tuple_struct_type_p (type);
   struct value_print_options opts;
@@ -1562,8 +1584,11 @@ rust_subscript (struct expression *exp, int *pos, enum noside noside,
 	  usize = language_lookup_primitive_type (exp->language_defn,
 						  exp->gdbarch,
 						  "usize");
-	  slice = rust_slice_type ("&[*gdb*]", value_type (result),
-				   usize);
+	  const char *new_name = ((type != nullptr
+				   && rust_slice_type_p (type))
+				  ? TYPE_NAME (type) : "&[*gdb*]");
+
+	  slice = rust_slice_type (new_name, value_type (result), usize);
 
 	  addrval = value_allocate_space_in_inferior (TYPE_LENGTH (slice));
 	  addr = value_as_long (addrval);
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 296878c..4de56f7 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,11 @@
 2017-10-02  Tom Tromey  <tom@tromey.com>
 
+	PR rust/22236:
+	* gdb.rust/simple.rs (main): New variable "fslice".
+	* gdb.rust/simple.exp: Add slice tests.  Update string tests.
+
+2017-10-02  Tom Tromey  <tom@tromey.com>
+
 	* gdb.rust/simple.exp: Test ptype of a slice.
 
 2017-10-02  Tom Tromey  <tom@tromey.com>
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
index b01841f..90516b9 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -64,6 +64,10 @@ gdb_test "ptype j2" " = struct simple::Unit"
 gdb_test "print simple::Unit" " = simple::Unit"
 gdb_test "print simple::Unit{}" " = simple::Unit"
 
+gdb_test "print f" " = \"hi bob\""
+gdb_test "print fslice" " = \"bob\""
+gdb_test "print &f\[3..\]" " = \"bob\""
+
 gdb_test "print g" " = \\(u8 \\(\\*\\)\\\[6\\\]\\) $hex b\"hi bob\""
 gdb_test "ptype g" " = u8 \\(\\*\\)\\\[6\\\]"
 
@@ -200,13 +204,9 @@ gdb_test "ptype empty" "fn \\(\\)"
 
 gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
 
-# We need the ".*" because currently we don't extract the length and
-# use it to intelligently print the string data.
-gdb_test "print \"hello rust\"" \
-    " = &str \\{data_ptr: $hex \"hello rust.*\", length: 10\\}"
+gdb_test "print \"hello rust\"" " = \"hello rust.*\""
 gdb_test "print \"hello" "Unexpected EOF in string"
-gdb_test "print r##\"hello \" rust\"##" \
-    " = &str \\{data_ptr: $hex \"hello \\\\\" rust.*\", length: 12\\}"
+gdb_test "print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\""
 gdb_test "print r\"hello" "Unexpected EOF in string"
 gdb_test "print r###\"###hello\"" "Unexpected EOF in string"
 gdb_test "print r###\"###hello\"##" "Unexpected EOF in string"
diff --git a/gdb/testsuite/gdb.rust/simple.rs b/gdb/testsuite/gdb.rust/simple.rs
index 9c154e7..d6d1755 100644
--- a/gdb/testsuite/gdb.rust/simple.rs
+++ b/gdb/testsuite/gdb.rust/simple.rs
@@ -95,6 +95,8 @@ fn main () {
     let g = b"hi bob";
     let h = b'9';
 
+    let fslice = &f[3..];
+
     let i = ["whatever"; 8];
 
     let j = Unit;
-- 
2.9.5

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

* [FYI 1/3] Allow indexing of &str in Rust
  2017-10-02 20:10 [FYI 0/3] fix a few slice-related bugs in Rust support Tom Tromey
  2017-10-02 20:11 ` [FYI 2/3] Fix ptype of Rust slices Tom Tromey
  2017-10-02 20:11 ` [FYI 3/3] Fix &str printing in Rust Tom Tromey
@ 2017-10-02 20:11 ` Tom Tromey
  2 siblings, 0 replies; 4+ messages in thread
From: Tom Tromey @ 2017-10-02 20:11 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

rust_slice_type_p was not recognizing &str as a slice type, so indexing
into (or making a slice of) a slice was not working.

2017-10-02  Tom Tromey  <tom@tromey.com>

	* rust-lang.c (rust_slice_type_p): Recognize &str as a slice type.

2017-10-02  Tom Tromey  <tom@tromey.com>

	* gdb.rust/simple.exp: Test index of slice.
---
 gdb/ChangeLog                     | 4 ++++
 gdb/rust-lang.c                   | 3 ++-
 gdb/testsuite/ChangeLog           | 4 ++++
 gdb/testsuite/gdb.rust/simple.exp | 2 ++
 4 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f5b265b..a8820c8 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@
 2017-10-02  Tom Tromey  <tom@tromey.com>
 
+	* rust-lang.c (rust_slice_type_p): Recognize &str as a slice type.
+
+2017-10-02  Tom Tromey  <tom@tromey.com>
+
 	* rust-lang.h (rust_slice_type): Add "extern".
 
 2017-10-02  Tom Tromey  <tom@tromey.com>
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index c5764bf..a9895fe 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -340,7 +340,8 @@ rust_slice_type_p (struct type *type)
 {
   return (TYPE_CODE (type) == TYPE_CODE_STRUCT
 	  && TYPE_TAG_NAME (type) != NULL
-	  && strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0);
+	  && (strncmp (TYPE_TAG_NAME (type), "&[", 2) == 0
+	      || strcmp (TYPE_TAG_NAME (type), "&str") == 0));
 }
 
 /* Return true if TYPE is a range type, otherwise false.  */
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 4348e76..edc5079 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2017-10-02  Tom Tromey  <tom@tromey.com>
+
+	* gdb.rust/simple.exp: Test index of slice.
+
 2017-09-27  Tom Tromey  <tom@tromey.com>
 
 	* gdb.base/macscp.exp: Add __VA_OPT__ tests.
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
index 403a11b..1a46317 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -55,6 +55,8 @@ gdb_test "print *(&c as &i32)" " = 0"
 gdb_test "print *(&c as *const i32)" " = 0"
 gdb_test "print *(&c as *mut i32)" " = 0"
 
+gdb_test "print/c f\[0\]" " = 104 'h'"
+
 gdb_test "print j" " = simple::Unit"
 gdb_test "ptype j" " = struct simple::Unit"
 gdb_test "print j2" " = simple::Unit"
-- 
2.9.5

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

end of thread, other threads:[~2017-10-02 20:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-02 20:10 [FYI 0/3] fix a few slice-related bugs in Rust support Tom Tromey
2017-10-02 20:11 ` [FYI 2/3] Fix ptype of Rust slices Tom Tromey
2017-10-02 20:11 ` [FYI 3/3] Fix &str printing in Rust Tom Tromey
2017-10-02 20:11 ` [FYI 1/3] Allow indexing of &str " Tom Tromey

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).