From patchwork Tue Jan 31 13:23:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 64010 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 656D6392AF99 for ; Tue, 31 Jan 2023 13:28:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 5B247382D72F for ; Tue, 31 Jan 2023 13:28:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B247382D72F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x42e.google.com with SMTP id m14so13735376wrg.13 for ; Tue, 31 Jan 2023 05:28:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5B/njqPnvjCHbeGaEsTVNQxCRdEFNZOmPX9OjKzlFg4=; b=ad2daeH2N/aBvxt4oMLVgv6cRESSQb5GFBdNlJJ1zvdIqIOzPXG4KiLQ4hcSFl04y2 MtLyFI6zcGWL0BqcY9+Z5BmmIrnY6VyFzeihWiUW4qRyb13uQG8Jkxgq6fda8QRzyKjG wadFbGWld1OgX4s2puxr6nKviBWrEaa23QcJ2mel0sxOQjXkn0GXQ+Rw+DW/5G73BY+b G8BUDBpJ0Qi/cpG+SiwF0OILsVajFgmKOMjf3bAe0RowKBoSrAnZY2cf6TARA9uUpcUr FpP0LNLo+kryTo+pe5LsKT1UxoTi86QCG9BzkVdLC+cZ5gVm2vjZiBXiUjug/g7qhXPd Tzaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5B/njqPnvjCHbeGaEsTVNQxCRdEFNZOmPX9OjKzlFg4=; b=ORh6O+wUR21G1RtzeAfUA7S/zZm01l/VzdjfnFfipNMVGmgYw6kKMTcr0NuvQiNfoh FvKnsT3SYpjHbDWC9yFa0QZUzjl1hZUmQP1/eUSXT6Yqsri/tcA3Lo+H72EbECPOQr9a R132WqwIYYPurWhvGnrUr5i+N7XBR5mFDuE6lar+6aPHhmJS7PZCjxIgpZqyqwGjN/xK CzWPNmQ91FR0VngG8PpDXxhd96XmoV0Mb0kDG34D2xthftKnZFgLB64MsLtC/HLyJ64W 0bciOsJ6tAuo3bg2SY8bePCASCBInP6yq6rAU3ZOso458Dg7nKhfieIh/Xxf99qJCU0P 4wKQ== X-Gm-Message-State: AO0yUKUAOiIU+FzBcP4BBRFMegQAyMD0R5r/DwhYVc9Q9E61zooP5H6m QXFZBYDqFegdJ9lxulK2FLXZHpY6rxDflVEiWg== X-Google-Smtp-Source: AMrXdXub8aEu7tJwJ9OlyJWnmhZX6cVoEQG8baaeIvFo3h+7GzVDtOKkdNbS6nmBeaY4fhu9FW5C4Q== X-Received: by 2002:a05:600c:3c92:b0:3d3:4007:9c88 with SMTP id bg18-20020a05600c3c9200b003d340079c88mr57484746wmb.18.1675171179915; Tue, 31 Jan 2023 05:19:39 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.gmail.com with ESMTPSA id l21-20020a05600c4f1500b003b47b80cec3sm21361521wmq.42.2023.01.31.05.19.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 05:19:39 -0800 (PST) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [COMMITTED] gccrs: Desugar double borrows into two HIR:BorrowExpr's Date: Tue, 31 Jan 2023 14:23:24 +0100 Message-Id: <20230131132324.661030-1-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-15.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Philip Herron We simply hit a gcc_unreachable() on double borrows but it seems reasonable to just desugar the AST into a borrow of a borrow to foo. Instead of a borrow expression with a flag to be respected. Fixes #1506 gcc/rust/ChangeLog: * hir/rust-ast-lower-expr.h: Lower double borrow expressions to two `HIR::BorrowExpr`s. * hir/tree/rust-hir-expr.h: Remove `is_double_borrow` field from `HIR::BorrowExpr`. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Remove call to `gcc_unreachable` on double borrow expressions. gcc/testsuite/ChangeLog: * rust/compile/torture/issue-1506.rs: New test. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/rust/hir/rust-ast-lower-expr.h | 27 ++++++++++++++++--- gcc/rust/hir/tree/rust-hir-expr.h | 5 ++-- .../typecheck/rust-hir-type-check-expr.cc | 6 ----- .../rust/compile/torture/issue-1506.rs | 3 +++ 4 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/rust/compile/torture/issue-1506.rs diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h index 92773b6f3a1..b0ab409646d 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.h +++ b/gcc/rust/hir/rust-ast-lower-expr.h @@ -643,10 +643,29 @@ public: mappings->get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - translated = new HIR::BorrowExpr ( - mapping, std::unique_ptr (borrow_lvalue), - expr.get_is_mut () ? Mutability::Mut : Mutability::Imm, - expr.get_is_double_borrow (), expr.get_outer_attrs (), expr.get_locus ()); + HIR::BorrowExpr *borrow_expr + = new HIR::BorrowExpr (mapping, + std::unique_ptr (borrow_lvalue), + expr.get_is_mut () ? Mutability::Mut + : Mutability::Imm, + expr.get_outer_attrs (), expr.get_locus ()); + + if (expr.get_is_double_borrow ()) + { + NodeId artifical_bouble_borrow_id = mappings->get_next_node_id (); + Analysis::NodeMapping mapping (crate_num, artifical_bouble_borrow_id, + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + borrow_expr + = new HIR::BorrowExpr (mapping, + std::unique_ptr (borrow_expr), + expr.get_is_mut () ? Mutability::Mut + : Mutability::Imm, + expr.get_outer_attrs (), expr.get_locus ()); + } + + translated = borrow_expr; } void visit (AST::DereferenceExpr &expr) override diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index 5a1e9768526..564d5215724 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -192,10 +192,10 @@ public: BorrowExpr (Analysis::NodeMapping mappings, std::unique_ptr borrow_lvalue, Mutability mut, - bool is_double_borrow, AST::AttrVec outer_attribs, Location locus) + AST::AttrVec outer_attribs, Location locus) : OperatorExpr (std::move (mappings), std::move (borrow_lvalue), std::move (outer_attribs), locus), - mut (mut), double_borrow (is_double_borrow) + mut (mut) {} void accept_vis (HIRFullVisitor &vis) override; @@ -203,7 +203,6 @@ public: Mutability get_mut () const { return mut; } bool is_mut () const { return mut == Mutability::Mut; } - bool get_is_double_borrow () const { return double_borrow; } protected: /* Use covariance to implement clone function as returning this object rather diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 41c8cd1f19b..c415ae98a7a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -1209,12 +1209,6 @@ TypeCheckExpr::visit (HIR::BorrowExpr &expr) } } - if (expr.get_is_double_borrow ()) - { - // FIXME double_reference - gcc_unreachable (); - } - infered = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), TyTy::TyVar (resolved_base->get_ref ()), expr.get_mut ()); diff --git a/gcc/testsuite/rust/compile/torture/issue-1506.rs b/gcc/testsuite/rust/compile/torture/issue-1506.rs new file mode 100644 index 00000000000..a38f23144ed --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/issue-1506.rs @@ -0,0 +1,3 @@ +pub fn main() { + let _: &i32 = &&&&1; +}