public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] unsafe: Use StackedContexts class
@ 2022-07-28 20:47 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-07-28 20:47 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:957e6180e12245719602597984a499485e2c758a

commit 957e6180e12245719602597984a499485e2c758a
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Jul 27 14:59:53 2022 +0200

    unsafe: Use StackedContexts class

Diff:
---
 gcc/rust/checks/errors/rust-unsafe-checker.cc | 37 +++++----------------------
 gcc/rust/checks/errors/rust-unsafe-checker.h  | 21 ++-------------
 2 files changed, 9 insertions(+), 49 deletions(-)

diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index 683a803c3b2..0d1e0e928e1 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -65,7 +65,7 @@ check_extern_static (HIR::ExternalItem *maybe_static, Location locus)
 void
 UnsafeChecker::check_use_of_static (HirId node_id, Location locus)
 {
-  if (is_unsafe_context ())
+  if (unsafe_context.is_in_context ())
     return;
 
   auto maybe_static_mut = mappings.lookup_hir_item (node_id);
@@ -79,29 +79,6 @@ UnsafeChecker::check_use_of_static (HirId node_id, Location locus)
 			 locus);
 }
 
-void
-UnsafeChecker::push_unsafe (HirId id)
-{
-  unsafe_contexts.emplace_back (id);
-}
-
-HirId
-UnsafeChecker::pop_unsafe ()
-{
-  rust_assert (!unsafe_contexts.empty ());
-
-  auto last = unsafe_contexts.back ();
-  unsafe_contexts.pop_back ();
-
-  return last;
-}
-
-bool
-UnsafeChecker::is_unsafe_context ()
-{
-  return !unsafe_contexts.empty ();
-}
-
 void
 UnsafeChecker::visit (IdentifierExpr &ident_expr)
 {
@@ -183,7 +160,7 @@ UnsafeChecker::visit (DereferenceExpr &expr)
   rust_assert (context.lookup_type (to_deref, &to_deref_type));
 
   if (to_deref_type->get_kind () == TyTy::TypeKind::POINTER
-      && !is_unsafe_context ())
+      && !unsafe_context.is_in_context ())
     rust_error_at (expr.get_locus (), "dereference of raw pointer requires "
 				      "unsafe function or block");
 }
@@ -334,7 +311,7 @@ UnsafeChecker::visit (FieldAccessExpr &expr)
 {
   expr.get_receiver_expr ()->accept_vis (*this);
 
-  if (is_unsafe_context ())
+  if (unsafe_context.is_in_context ())
     return;
 
   TyTy::BaseType *receiver_ty;
@@ -427,11 +404,11 @@ UnsafeChecker::visit (ReturnExpr &expr)
 void
 UnsafeChecker::visit (UnsafeBlockExpr &expr)
 {
-  push_unsafe (expr.get_mappings ().get_hirid ());
+  unsafe_context.enter (expr.get_mappings ().get_hirid ());
 
   expr.get_block_expr ()->accept_vis (*this);
 
-  pop_unsafe ();
+  unsafe_context.exit ();
 }
 
 void
@@ -595,12 +572,12 @@ UnsafeChecker::visit (Function &function)
   auto is_unsafe_fn = function.get_qualifiers ().is_unsafe ();
 
   if (is_unsafe_fn)
-    push_unsafe (function.get_mappings ().get_hirid ());
+    unsafe_context.enter (function.get_mappings ().get_hirid ());
 
   function.get_definition ()->accept_vis (*this);
 
   if (is_unsafe_fn)
-    pop_unsafe ();
+    unsafe_context.exit ();
 }
 
 void
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h
index 21ec0f46eda..b9d06ef33d1 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.h
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.h
@@ -22,6 +22,7 @@
 #include "rust-hir-visitor.h"
 #include "rust-name-resolver.h"
 #include "rust-hir-type-check.h"
+#include "rust-stacked-contexts.h"
 
 namespace Rust {
 namespace HIR {
@@ -33,31 +34,13 @@ public:
   void go (HIR::Crate &crate);
 
 private:
-  /* Stack of unsafe contexts */
-  std::vector<HirId> unsafe_contexts;
-
-  /**
-   * Add an unsafe context to the stack. To call when entering unsafe blocks
-   */
-  void push_unsafe (HirId id);
-
-  /**
-   * Remove an unsafe context from the stack. Call this when exiting unsafe
-   * blocks
-   */
-  HirId pop_unsafe ();
-
-  /**
-   * Are we currently in an unsafe context or not
-   */
-  bool is_unsafe_context ();
-
   /**
    * Check if a mutable static or external static item is used outside of an
    * unsafe context
    */
   void check_use_of_static (HirId node_id, Location locus);
 
+  StackedContexts<HirId> unsafe_context;
   Resolver::TypeCheckContext &context;
   Resolver::Resolver &resolver;
   Analysis::Mappings &mappings;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-28 20:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-28 20:47 [gcc/devel/rust/master] unsafe: Use StackedContexts class Thomas Schwinge

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).