From patchwork Mon Jun 27 16:25:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manish Goregaokar X-Patchwork-Id: 13407 Received: (qmail 106464 invoked by alias); 27 Jun 2016 16:25:52 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 106453 invoked by uid 89); 27 Jun 2016 16:25:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=rarely X-HELO: mail-wm0-f53.google.com Received: from mail-wm0-f53.google.com (HELO mail-wm0-f53.google.com) (74.125.82.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Mon, 27 Jun 2016 16:25:40 +0000 Received: by mail-wm0-f53.google.com with SMTP id r201so122884686wme.1 for ; Mon, 27 Jun 2016 09:25:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=SAaZjCmIZzrvYpWcAKxd+096XXpdoEn1uF+qbLFRqcc=; b=W4CpQZU+5WG6Nxdl+wXKrt9EsBOm6I6fFBtdWWDyD2Za51uDD5kjG9NV9qdocVrI8C osypv84jUwgoYOZe9zQYo8GX3fQtyPzHQSFRxtpPHtvCN1a4OKh5eI4tdqyv/62sBcQg ENg0hY8bLMGgUnMGpLEF/3CdRcokg/UiR36/EQdlPEZrEqtRztvbH5NkdxIInKYxpiWP rk1jdzRIrl6P9vbQAkC9Ge2+m/3qsmxeDudagGehKAKZpzWpQj+Fqv7BLj0ABgjwod0h 7Yq1EvjNLN7pqcGXTISrJgp5RwwMUkTMQDCuo4BRkQqIsUce2g1L5PlWxbBDTEm5OpHs uivQ== X-Gm-Message-State: ALyK8tJVv5uDZelByy+ZGYGBy9nuLVTDlqaexXJWqt9N1zaA/UV7rOZnTBnAybrWV25XovPv/opLFC9oh++rUEdd X-Received: by 10.194.82.161 with SMTP id j1mr1817418wjy.65.1467044737989; Mon, 27 Jun 2016 09:25:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.28.36.215 with HTTP; Mon, 27 Jun 2016 09:25:37 -0700 (PDT) In-Reply-To: <8760suk3qg.fsf@tromey.com> References: <8760suk3qg.fsf@tromey.com> From: Manish Goregaokar Date: Mon, 27 Jun 2016 21:55:37 +0530 Message-ID: Subject: Re: [PATCH] Print void types correctly in Rust To: Tom Tromey Cc: gdb-patches@sourceware.org X-IsSubscribed: yes > This is fine but there is another case in rust_val_print: > > case TYPE_CODE_INT: > /* Recognize the unit type. */ > if (TYPE_UNSIGNED (type) && TYPE_LENGTH (type) == 0 > && TYPE_NAME (type) != NULL && strcmp (TYPE_NAME (type), "()") == 0) > { > fputs_filtered ("()", stream); > break; > } > goto generic_print; > > > ... I wonder if that is something that changed after 1.8, or if it's the > case that the unit type can be represented in multiple ways. (Or maybe > this only handles the unit type constructed by rust_language_arch_info?) > > Anyway I wonder if a case like this is needed in rust_print_type, or if > the one in rust_val_print can be removed or changed. I don't think this has changed. The issue is with functions, where the return type of functions that return unit is called void because we pretend to be C. Values have a different type code. > Manish> + if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID) > Manish> + { > Manish> + fputs_filtered (" -> ", stream); > Manish> + rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags); > > This should be formatted like: > > + if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_VOID) > + { > + fputs_filtered (" -> ", stream); > + rust_print_type (TYPE_TARGET_TYPE (type), "", stream, -1, 0, flags); > > Manish> +// Empty function, should not have "void" > Manish> +// or "()" in its return type > Manish> +fn empty() { > > I'm curious what happens if it does say "-> ()"? It's okay to do so, just not idiomatic Rust. You rarely see `-> ()` for void functions. ========= new patch below (changelog and formatting fixed): 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: 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 --- gdb/rust-lang.c | 22 ++++++++++++++++++---- gdb/testsuite/gdb.rust/simple.exp | 1 + gdb/testsuite/gdb.rust/simple.rs | 7 +++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index 0c56a0f..23ddd5a 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/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index 88f1c89..4622f75 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 32da580..3d28e27 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(); }