public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: Arthur Cohen <arthur.cohen@embecosm.com>
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, Philip Herron <philip.herron@embecosm.com>
Subject: [COMMITTED] gccrs: Desugar double borrows into two HIR:BorrowExpr's
Date: Tue, 31 Jan 2023 14:23:24 +0100	[thread overview]
Message-ID: <20230131132324.661030-1-arthur.cohen@embecosm.com> (raw)

From: Philip Herron <philip.herron@embecosm.com>

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<HIR::Expr> (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<HIR::Expr> (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<HIR::Expr> (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<Expr> 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;
+}
-- 
2.39.1


                 reply	other threads:[~2023-01-31 13:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230131132324.661030-1-arthur.cohen@embecosm.com \
    --to=arthur.cohen@embecosm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc-rust@gcc.gnu.org \
    --cc=philip.herron@embecosm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).