From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 3944D3858D33; Thu, 28 Jul 2022 20:47:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3944D3858D33 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] unsafe: Use StackedContexts class X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 6d5eb739f069e41a8789c15b199893acf06915a8 X-Git-Newrev: 957e6180e12245719602597984a499485e2c758a Message-Id: <20220728204741.3944D3858D33@sourceware.org> Date: Thu, 28 Jul 2022 20:47:41 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jul 2022 20:47:41 -0000 https://gcc.gnu.org/g:957e6180e12245719602597984a499485e2c758a commit 957e6180e12245719602597984a499485e2c758a Author: Arthur Cohen 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 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 unsafe_context; Resolver::TypeCheckContext &context; Resolver::Resolver &resolver; Analysis::Mappings &mappings;