diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-07-28 19:06:19 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-28 19:06:19 +0000 |
commit | 0632b1aa5f0ea1d0c9c4411e9ff3d05336736fbb (patch) | |
tree | 60a7e720e153695f37ee378c88f8a83b39b62611 /gcc | |
parent | 6d5eb739f069e41a8789c15b199893acf06915a8 (diff) | |
parent | 957e6180e12245719602597984a499485e2c758a (diff) | |
download | gcc-0632b1aa5f0ea1d0c9c4411e9ff3d05336736fbb.zip gcc-0632b1aa5f0ea1d0c9c4411e9ff3d05336736fbb.tar.gz gcc-0632b1aa5f0ea1d0c9c4411e9ff3d05336736fbb.tar.bz2 |
Merge #1415
1415: Add generic `StackedContexts<T>` type r=CohenArthur a=CohenArthur
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/checks/errors/rust-unsafe-checker.cc | 37 | ||||
-rw-r--r-- | 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 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; |