From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 1E48E385742E; Mon, 29 Aug 2022 15:36:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E48E385742E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661787377; bh=SQnnOiy5BplLU1jijNE4nZ3j0UB8a120wt+gCTFoBHA=; h=From:To:Subject:Date:From; b=YzsEJRctjj/XZ305JmfjnkR2hVus3nFS/uov1PnAtOGPRMY92INZvDwSf6sJ7ewjr MBOGzy4dFvBElMDSCNYSlfseSGwiiEOYPCz1kGZF9oIYd6bqQvV9iUJ2u1fD0xS98A 4lTxcKDDfusFnG5YudJCGRu7tgKHRtjYYfuKL2Pw= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Desugar double borrows into two HIR:BorrowExpr's X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: d3cf195ab46d7effe806990aa6b7a409bf8e46df X-Git-Newrev: 727afe64d6c6ccc6c360da0707de4b2bd74a96d9 Message-Id: <20220829153617.1E48E385742E@sourceware.org> Date: Mon, 29 Aug 2022 15:36:17 +0000 (GMT) List-Id: https://gcc.gnu.org/g:727afe64d6c6ccc6c360da0707de4b2bd74a96d9 commit 727afe64d6c6ccc6c360da0707de4b2bd74a96d9 Author: Philip Herron Date: Thu Aug 25 15:33:02 2022 +0100 Desugar double borrows into two HIR:BorrowExpr's 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 Diff: --- gcc/rust/hir/rust-ast-lower-expr.h | 27 ++++++++++++++++++++---- gcc/rust/hir/tree/rust-hir-expr.h | 5 ++--- gcc/rust/typecheck/rust-hir-type-check-expr.cc | 6 ------ gcc/testsuite/rust/compile/torture/issue-1506.rs | 3 +++ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h index 4f7f40f27e4..be045e930d1 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 83278529646..e173d376674 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 4371f5a59a5..9dc72a1472b 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; +}