From patchwork Mon Oct 2 20:10:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 23285 Received: (qmail 12304 invoked by alias); 2 Oct 2017 20:11:00 -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 12010 invoked by uid 89); 2 Oct 2017 20:10:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gproxy10-pub.mail.unifiedlayer.com Received: from gproxy10-pub.mail.unifiedlayer.com (HELO gproxy10-pub.mail.unifiedlayer.com) (69.89.20.226) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 02 Oct 2017 20:10:58 +0000 Received: from cmgw4 (unknown [10.0.90.85]) by gproxy10.mail.unifiedlayer.com (Postfix) with ESMTP id 40EDE140718 for ; Mon, 2 Oct 2017 14:10:57 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id GkAt1w01F2f2jeq01kAwF0; Mon, 02 Oct 2017 14:10:57 -0600 X-Authority-Analysis: v=2.2 cv=OZLoNlbY c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=02M-m0pO-4AA:10 a=zstS-IiYAAAA:8 a=TndGVgo6SW66PD8Ho-oA:9 a=GAS8ZqL-gVDr0AFU:21 a=eoNy8RE4dFIQbTMm:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-0-208.hlrn.qwest.net ([75.166.0.208]:55566 helo=pokyo.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dz73F-003jcm-Oh; Mon, 02 Oct 2017 14:10:53 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [FYI 2/3] Fix ptype of Rust slices Date: Mon, 2 Oct 2017 14:10:40 -0600 Message-Id: <20171002201041.4815-3-tom@tromey.com> In-Reply-To: <20171002201041.4815-1-tom@tromey.com> References: <20171002201041.4815-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dz73F-003jcm-Oh X-Source-Sender: 75-166-0-208.hlrn.qwest.net (pokyo.Home) [75.166.0.208]:55566 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes 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 * rust-lang.c (rust_subscript): Handle slices in EVAL_AVOID_SIDE_EFFECTS case. 2017-10-02 Tom Tromey * 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 + * rust-lang.c (rust_subscript): Handle slices in + EVAL_AVOID_SIDE_EFFECTS case. + +2017-10-02 Tom Tromey + * rust-lang.c (rust_slice_type_p): Recognize &str as a slice type. 2017-10-02 Tom Tromey 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 + * gdb.rust/simple.exp: Test ptype of a slice. + +2017-10-02 Tom Tromey + * gdb.rust/simple.exp: Test index of slice. 2017-09-27 Tom Tromey 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\\)"