From patchwork Wed May 17 15:10:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 69520 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 093E43853839 for ; Wed, 17 May 2023 15:10:57 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from qproxy1-pub.mail.unifiedlayer.com (qproxy1-pub.mail.unifiedlayer.com [173.254.64.10]) by sourceware.org (Postfix) with ESMTPS id 0F4EA3858C41 for ; Wed, 17 May 2023 15:10:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0F4EA3858C41 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from gproxy4-pub.mail.unifiedlayer.com (unknown [69.89.23.142]) by qproxy1.mail.unifiedlayer.com (Postfix) with ESMTP id 24B3C8034118 for ; Wed, 17 May 2023 15:10:42 +0000 (UTC) Received: from cmgw10.mail.unifiedlayer.com (unknown [10.0.90.125]) by progateway6.mail.pro1.eigbox.com (Postfix) with ESMTP id 193F0100443F4 for ; Wed, 17 May 2023 15:10:10 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id zIn3pYx2loWVUzIn3pmTlx; Wed, 17 May 2023 15:10:09 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=aeOkITkt c=1 sm=1 tr=0 ts=6464ee51 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=P0xRbXHiH_UA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=CCpqsmhAAAAA:8 a=0jOwZ_ICioZBqwsImqMA:9 a=ul9cdbp4aOFLsgKbc677:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=AjXjgjEK14hCB7MHN/pkTM3pgHEXygR57OHYpBEvhEA=; b=Gnb8r4AHDeKIeqo1Uicl1bl/YG iwVjmBTxTMENnUuonya6j4I/ta3KIpgeSPQ26UhgZYqN1p9/hMvS5QoX6RpqHhgDbZCd8ZhgZgGOS xSmrtvNEimoYICRW7KBox05j0; Received: from 75-166-157-55.hlrn.qwest.net ([75.166.157.55]:54282 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pzIn3-000jxk-LH; Wed, 17 May 2023 09:10:09 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Special case "&str" in Rust parser Date: Wed, 17 May 2023 09:10:00 -0600 Message-Id: <20230517151000.3405149-1-tom@tromey.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.157.55 X-Source-L: No X-Exim-ID: 1pzIn3-000jxk-LH X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-157-55.hlrn.qwest.net (localhost.localdomain) [75.166.157.55]:54282 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3025.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" "&str" is an important type in Rust -- it's the type of string literals. However, the compiler puts it in the DWARF in a funny way. The slice itself is present and named "&str". However, the Rust parser doesn't look for types with names like this, but instead tries to construct them from components. In this case it tries to make a pointer-to-"str" -- but "str" isn't always available, and in any case that wouldn't yield the best result. This patch adds a special case for &str. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22251 Reviewed-By: Andrew Burgess --- gdb/rust-parse.c | 10 ++++++++++ gdb/testsuite/gdb.rust/simple.exp | 3 +++ 2 files changed, 13 insertions(+) diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c index 648e48dda40..427169611d3 100644 --- a/gdb/rust-parse.c +++ b/gdb/rust-parse.c @@ -1682,6 +1682,16 @@ rust_parser::parse_slice_type () { assume ('&'); + /* Handle &str specially. This is an important type in Rust. While + the compiler does emit the "&str" type in the DWARF, just "str" + itself isn't always available -- but it's handy if this works + seamlessly. */ + if (current_token == IDENT && get_string () == "str") + { + lex (); + return rust_slice_type ("&str", get_type ("u8"), get_type ("usize")); + } + bool is_slice = current_token == '['; if (is_slice) lex (); diff --git a/gdb/testsuite/gdb.rust/simple.exp b/gdb/testsuite/gdb.rust/simple.exp index 08ebed3f103..a615b92ec47 100644 --- a/gdb/testsuite/gdb.rust/simple.exp +++ b/gdb/testsuite/gdb.rust/simple.exp @@ -68,6 +68,9 @@ gdb_test "print simple::Unit{23}" "'}', '\.\.', or identifier expected" gdb_test "print f" " = \"hi bob\"" gdb_test "print fslice" " = \"bob\"" gdb_test "print &f\[3..\]" " = \"bob\"" +gdb_test "whatis f" "type = &str" +gdb_test "print *(&f as *mut &str)" " = \"hi bob\"" \ + "print via cast to &str" gdb_test "print g" " = \\(\\*mut \\\[u8; 6\\\]\\) $hex b\"hi bob\"" gdb_test "ptype g" " = \\*mut \\\[u8; 6\\\]"