From 921d8f549f9e35d3f83c7b1a381146a7dc1246f4 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 27 Jun 2016 21:16:59 +0530 Subject: [PATCH] Print void types correctly in Rust Rust prefers to not specify the return type of a function when it is unit (`()`). The type is also referred to as "void" in debuginfo but not in actual usage, so we should never be printing "void" when the language is Rust. 2016-06-27 Manish Goregaokar gdb/ChangeLog: * rust-lang.c (rust_print_type): Print unit types as "()" * rust-lang.c (rust_print_type): Omit return type for functions returning unit gdb/testsuite/ChangeLog: * gdb.rust/simple.rs: Add test for returning unit in a function * gdb.rust/simple.exp: Add expectation for functions returning unit --- gdb/ChangeLog | 6 ++++++ gdb/rust-lang.c | 22 ++++++++++++++++++---- gdb/testsuite/ChangeLog | 5 +++++ gdb/testsuite/gdb.rust/simple.exp | 1 + gdb/testsuite/gdb.rust/simple.rs | 7 +++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index ad6f5d920ed..40adb80f917 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2016-06-27 Manish Goregaokar + + * rust-lang.c (rust_print_type): Print unit types as "()" + * rust-lang.c (rust_print_type): Omit return type for functions + returning unit + 2016-06-25 Pierre-Marie de Rodat * python/py-breakpoint.c (bppy_init): Clear bppy_pending_object diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index 0c56a0f6fde..23ddd5aecda 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -446,7 +446,7 @@ static const struct generic_val_print_decorations rust_decorations = " * I", "true", "false", - "void", + "()", "[", "]" }; @@ -729,13 +729,22 @@ rust_print_type (struct type *type, const char *varstring, if (show <= 0 && TYPE_NAME (type) != NULL) { - fputs_filtered (TYPE_NAME (type), stream); + /* Rust calls the unit type "void" in its debuginfo, + but we don't want to print it as that. */ + if (TYPE_CODE (type) == TYPE_CODE_VOID) + fputs_filtered ("()", stream); + else + fputs_filtered (TYPE_NAME (type), stream); return; } type = check_typedef (type); switch (TYPE_CODE (type)) { + case TYPE_CODE_VOID: + fputs_filtered ("()", stream); + break; + case TYPE_CODE_FUNC: /* Delegate varargs to the C printer. */ if (TYPE_VARARGS (type)) @@ -753,8 +762,13 @@ rust_print_type (struct type *type, const char *varstring, rust_print_type (TYPE_FIELD_TYPE (type, i), "", stream, -1, 0, flags); } - fputs_filtered (") -> ", stream); - rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags); + fputs_filtered (")", stream); + /* If it returns unit, we can omit the return type. */ + if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID) + { + fputs_filtered (" -> ", stream); + rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags); + } break; case TYPE_CODE_ARRAY: diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index f5bdb4036e2..b7e30f53365 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-06-27 Manish Goregaokar + + * gdb.rust/simple.rs: Add test for returning unit in a function + * gdb.rust/simple.exp: Add expectation for functions returning unit + 2016-06-27 Pierre-Marie de Rodat * gdb.python/py-breakpoint-create-fail.c, diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index 88f1c89a748..4622f75b1d3 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -149,6 +149,7 @@ gdb_test "print self::diff2(8, 9)" " = -1" gdb_test "print ::diff2(23, -23)" " = 46" gdb_test "ptype diff2" "fn \\(i32, i32\\) -> i32" +gdb_test "ptype empty" "fn \\(\\)" gdb_test "print (diff2 as fn(i32, i32) -> i32)(19, -2)" " = 21" diff --git a/gdb/testsuite/gdb.rust/simple.rs b/gdb/testsuite/gdb.rust/simple.rs index 32da5807b7f..3d28e272982 100644 --- a/gdb/testsuite/gdb.rust/simple.rs +++ b/gdb/testsuite/gdb.rust/simple.rs @@ -48,6 +48,12 @@ fn diff2(x: i32, y: i32) -> i32 { x - y } +// Empty function, should not have "void" +// or "()" in its return type +fn empty() { + +} + pub struct Unit; // This triggers the non-zero optimization that yields a different @@ -111,4 +117,5 @@ fn main () { println!("{}, {}", x.0, x.1); // set breakpoint here println!("{}", diff2(92, 45)); + empty(); } -- 2.30.2