]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Prevent raw reference from being lowered silently
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 21 May 2024 08:38:16 +0000 (10:38 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Mar 2025 15:35:33 +0000 (16:35 +0100)
We do not handle those kind of references yet, we shall not let them
pass as a regular reference.

gcc/rust/ChangeLog:

* ast/rust-expr.h: Add a getter for mutability.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Panic when a
raw reference is met.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ast/rust-expr.h
gcc/rust/hir/rust-ast-lower-expr.cc

index 6609ad80b37e44aec43fe799eca8c0c088af3c96..a5afbffee99be6d1467dc72c5a1fd0a6de20685d 100644 (file)
@@ -398,6 +398,8 @@ public:
 
   bool get_is_mut () const { return mutability == Mutability::Mut; }
 
+  Mutability get_mutability () const { return mutability; }
+
   bool get_is_double_borrow () const { return double_borrow; }
   bool is_raw_borrow () const { return raw_borrow; }
 
index 515d36a839f3ed1c4b69d2fc5b007ff5cec7dc62..a0eb5e32f25b7ff36c4068a12a8010d9234ad102 100644 (file)
@@ -628,6 +628,9 @@ ASTLoweringExpr::visit (AST::ContinueExpr &expr)
 void
 ASTLoweringExpr::visit (AST::BorrowExpr &expr)
 {
+  if (expr.is_raw_borrow ())
+    rust_unreachable ();
+
   HIR::Expr *borrow_lvalue
     = ASTLoweringExpr::translate (expr.get_borrowed_expr ());
 
@@ -638,9 +641,8 @@ ASTLoweringExpr::visit (AST::BorrowExpr &expr)
 
   auto *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 ());
+                          expr.get_mutability (), expr.get_outer_attrs (),
+                          expr.get_locus ());
 
   if (expr.get_is_double_borrow ())
     {
@@ -652,9 +654,8 @@ ASTLoweringExpr::visit (AST::BorrowExpr &expr)
       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 ());
+                              expr.get_mutability (), expr.get_outer_attrs (),
+                              expr.get_locus ());
     }
 
   translated = borrow_expr;