public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] unsafe: Use StackedContexts class Date: Thu, 28 Jul 2022 20:47:41 +0000 (GMT) [thread overview] Message-ID: <20220728204741.3944D3858D33@sourceware.org> (raw) 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;
reply other threads:[~2022-07-28 20:47 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=20220728204741.3944D3858D33@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /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: linkBe 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).