aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/rust/checks/errors/rust-unsafe-checker.cc37
-rw-r--r--gcc/rust/checks/errors/rust-unsafe-checker.h21
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 683a803..0d1e0e9 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);
@@ -80,29 +80,6 @@ UnsafeChecker::check_use_of_static (HirId node_id, Location 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)
{
NodeId ast_node_id = ident_expr.get_mappings ().get_nodeid ();
@@ -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 21ec0f4..b9d06ef 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;