[PATCH] Improve code coverage of Rust testing

Tom Tromey tom@tromey.com
Wed Apr 21 21:47:41 GMT 2021


I enabled code coverage and ran the gdb test suite, and noticed that
the new Rust parser was missing testing on a few lines that were easy
to cover.  This patch mostly adds tests for certain syntax errors; but
this process also uncovered a couple of real bugs: I must have
cut-and-pasted the 'sizeof' parsing code from some other code, because
it is checking for KW_MUT (the old bison parser did not do this), and
the array length check is actually impossible because a negative
number like '-1' is parsed as two tokens.

gdb/ChangeLog
2021-04-21  Tom Tromey  <tom@tromey.com>

	* rust-parse.c (rust_parser::parse_sizeof): Remove KW_MUT code.
	(struct typed_val_int) <val>: Now ULONGEST.
	(rust_parser::parse_array_type): Remove negative check.
	(rust_lex_int_test): Change 'value' to ULONGEST.

gdb/testsuite/ChangeLog
2021-04-21  Tom Tromey  <tom@tromey.com>

	* gdb.rust/modules.exp: Add checks for syntax errors.
	* gdb.rust/expr.exp: Add checks for syntax errors.
	* gdb.rust/simple.exp: Add checks for syntax errors.
---
 gdb/ChangeLog                      |  7 +++++++
 gdb/rust-parse.c                   | 11 +++--------
 gdb/testsuite/ChangeLog            |  6 ++++++
 gdb/testsuite/gdb.rust/expr.exp    |  2 ++
 gdb/testsuite/gdb.rust/modules.exp |  4 ++++
 gdb/testsuite/gdb.rust/simple.exp  | 19 +++++++++++++++++++
 6 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index bb31782811c..2f2afcf7e9f 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -126,7 +126,7 @@ enum token_type : int
 
 struct typed_val_int
 {
-  LONGEST val;
+  ULONGEST val;
   struct type *type;
 };
 
@@ -1447,9 +1447,6 @@ rust_parser::parse_sizeof ()
 {
   assume (KW_SIZEOF);
 
-  if (current_token == KW_MUT)
-    lex ();
-
   require ('(');
   operation_up result = make_operation<unop_sizeof_operation> (parse_expr ());
   require (')');
@@ -1600,9 +1597,7 @@ rust_parser::parse_array_type ()
 
   if (current_token != INTEGER && current_token != DECIMAL_INTEGER)
     error (_("integer expected"));
-  LONGEST val = current_int_val.val;
-  if (val < 0)
-    error (_("Negative array length"));
+  ULONGEST val = current_int_val.val;
   lex ();
   require (']');
 
@@ -2117,7 +2112,7 @@ rust_lex_test_one (rust_parser *parser, const char *input, int expected)
 
 static void
 rust_lex_int_test (rust_parser *parser, const char *input,
-		   LONGEST value, int kind)
+		   ULONGEST value, int kind)
 {
   rust_lex_test_one (parser, input, kind);
   SELF_CHECK (parser->current_int_val.val == value);
diff --git a/gdb/testsuite/gdb.rust/expr.exp b/gdb/testsuite/gdb.rust/expr.exp
index d81b6fcbf57..603e5388c3f 100644
--- a/gdb/testsuite/gdb.rust/expr.exp
+++ b/gdb/testsuite/gdb.rust/expr.exp
@@ -111,6 +111,8 @@ gdb_test "print ()" " = \\(\\)"
 gdb_test "print \[1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]"
 gdb_test "ptype \[1,2,3,4\]" "type = \\\[i32; 4\\\]"
 gdb_test "print \[mut 1,2,3,4\]" " = \\\[1, 2, 3, 4\\\]"
+gdb_test "print \[1,2 3" "',' or ']' expected"
+gdb_test "print \[1 2" "',', ';', or ']' expected"
 
 gdb_test "print b\"hi rust\"" " = b\"hi rust\""
 # This isn't rusty syntax yet, but that's another bug -- this is just
diff --git a/gdb/testsuite/gdb.rust/modules.exp b/gdb/testsuite/gdb.rust/modules.exp
index 6659f426a3c..4c9f4549cb7 100644
--- a/gdb/testsuite/gdb.rust/modules.exp
+++ b/gdb/testsuite/gdb.rust/modules.exp
@@ -91,3 +91,7 @@ foreach mod {mod1::inner::innest mod1::inner mod1 {}} {
 }
 
 gdb_test "print ::TWENTY_THREE" " = 23"
+
+gdb_test "print super TWENTY_THREE" "'::' expected"
+gdb_test "print super::23" "identifier expected"
+gdb_test "ptype ::Generic::<::Generic<self::Type" "'>' expected"
diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp
index 0be0e94ac4c..1588e155165 100644
--- a/gdb/testsuite/gdb.rust/simple.exp
+++ b/gdb/testsuite/gdb.rust/simple.exp
@@ -64,6 +64,7 @@ gdb_test "print j2" " = simple::Unit"
 gdb_test "ptype j2" " = struct simple::Unit"
 gdb_test "print simple::Unit" " = simple::Unit"
 gdb_test "print simple::Unit{}" " = simple::Unit"
+gdb_test "print simple::Unit{23}" "'}', '\.\.', or identifier expected"
 
 gdb_test "print f" " = \"hi bob\""
 gdb_test "print fslice" " = \"bob\""
@@ -90,6 +91,8 @@ gdb_test "print fromslice" " = 3"
 gdb_test "print slice\[0\]" " = 3"
 gdb_test "print (slice as &\[i32\])\[0\]" " = 3"
 
+gdb_test "print slice as \[i32; 73.9\]" "integer expected"
+
 gdb_test_sequence "ptype slice" "" {
     " = struct &\\\[i32\\\] \\{"
     "  data_ptr: \\*mut i32,"
@@ -131,6 +134,11 @@ gdb_test_sequence "ptype z" "" {
 }
 gdb_test "print z.1" " = 8"
 
+# Some error checks.
+gdb_test "print z.1_0" \
+    "'_' not allowed in integers in anonymous field references"
+gdb_test "print z.mut" "field name expected"
+
 gdb_test "print univariant" " = simple::Univariant::Foo{a: 1}"
 gdb_test "print univariant.a" " = 1"
 gdb_test "print univariant_anon" " = simple::UnivariantAnon::Foo\\(1\\)"
@@ -188,6 +196,9 @@ gdb_test_sequence "ptype e" "" {
     "\\}"
 }
 
+# Test a parser error.
+gdb_test "print sizeof e" "'\\(' expected"
+
 gdb_test "print e.0" " = 73"
 gdb_test "print e.1" \
     "Cannot access field 1 of variant simple::MoreComplicated::Two, there are only 1 fields"
@@ -222,6 +233,10 @@ gdb_test "ptype empty" "fn \\(\\)"
 
 gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21"
 
+gdb_test "print diff2(73, 74 75" "',' or '\\\)' expected"
+gdb_test "print (diff2 as fn i32, i32) -> i32)(19, -2)" "'\\\(' expected"
+gdb_test "print (diff2 as fn (i32, i32) i32)(19, -2)" "'->' expected"
+
 gdb_test "print \"hello rust\"" " = \"hello rust.*\""
 gdb_test "print \"hello" "Unexpected EOF in string"
 gdb_test "print r##\"hello \" rust\"##" " = \"hello \\\\\" rust.*\""
@@ -335,6 +350,10 @@ gdb_test "print (1,2,3)" "Tuple expressions not supported yet"
 gdb_test "print (1,)" "Tuple expressions not supported yet"
 gdb_test "print (1)" " = 1"
 
+# Test a syntax error in tuple expressions.
+gdb_test "print (1,2,," "unexpected token"
+gdb_test "print (1,2 8" "',' or '\\\)' expected"
+
 gdb_test "print 23..97.0" "Range expression with different types"
 
 gdb_test "print (*parametrized.next.val)" \
-- 
2.26.2



More information about the Gdb-patches mailing list