diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index 470e9b45919d..8b90039f2835 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -278,7 +278,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) } self->inherit_bounds (specified_bounds); - context->push_block_context (TypeCheckBlockContextItem (trait_reference)); + context->block_context ().enter (TypeCheckBlockContextItem (trait_reference)); std::vector item_refs; for (auto &item : trait_reference->get_trait_items ()) { @@ -308,7 +308,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) // resolve the blocks of functions etc because it can end up in a recursive // loop of trying to resolve traits as required by the types tref->on_resolved (); - context->pop_block_context (); + context->block_context ().exit (); return tref; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index a72ab9731d1d..148127d86a94 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -335,9 +335,10 @@ TypeCheckImplItem::Resolve ( // resolve TypeCheckImplItem resolver (parent, self, substitutions); - resolver.context->push_block_context (TypeCheckBlockContextItem (&parent)); + resolver.context->block_context ().enter ( + TypeCheckBlockContextItem (&parent)); item.accept_vis (resolver); - resolver.context->pop_block_context (); + resolver.context->block_context ().exit (); return resolver.result; } diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 2962674da62f..738d7d41b55e 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -591,10 +591,11 @@ TypeCheckType::resolve_segments ( bool first_segment = i == offset; bool selfResolveOk = false; - if (first_segment && tySegIsBigSelf && context->have_block_context () - && context->peek_block_context ().is_impl_block ()) + if (first_segment && tySegIsBigSelf + && context->block_context ().is_in_context () + && context->block_context ().peek ().is_impl_block ()) { - TypeCheckBlockContextItem ctx = context->peek_block_context (); + TypeCheckBlockContextItem ctx = context->block_context ().peek (); TyTy::BaseType *lookup = nullptr; selfResolveOk = resolve_associated_type (seg->as_string (), ctx, &lookup); diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h index 65c929af9477..42ef2d14325f 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.h +++ b/gcc/rust/typecheck/rust-hir-type-check.h @@ -22,6 +22,7 @@ #include "rust-hir-map.h" #include "rust-tyty.h" #include "rust-hir-trait-reference.h" +#include "rust-stacked-contexts.h" #include "rust-autoderef.h" #include "rust-tyty-region.h" #include "rust-tyty-variance-analysis.h" @@ -186,10 +187,7 @@ class TypeCheckContext TyTy::BaseType *return_type); void pop_return_type (); - bool have_block_context () const; - TypeCheckBlockContextItem peek_block_context (); - void push_block_context (TypeCheckBlockContextItem item); - void pop_block_context (); + StackedContexts &block_context (); void iterate (std::function cb); @@ -282,7 +280,7 @@ class TypeCheckContext std::vector> return_type_stack; std::vector loop_type_stack; - std::vector block_stack; + StackedContexts block_stack; std::map trait_context; std::map receiver_context; std::map associated_impl_traits; diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc index b37be49c749a..d3ff29a4bdb3 100644 --- a/gcc/rust/typecheck/rust-typecheck-context.cc +++ b/gcc/rust/typecheck/rust-typecheck-context.cc @@ -177,30 +177,10 @@ TypeCheckContext::peek_context () return return_type_stack.back ().first; } -bool -TypeCheckContext::have_block_context () const -{ - return !block_stack.empty (); -} - -TypeCheckBlockContextItem -TypeCheckContext::peek_block_context () -{ - rust_assert (!block_stack.empty ()); - return block_stack.back (); -} - -void -TypeCheckContext::push_block_context (TypeCheckBlockContextItem block) -{ - block_stack.push_back (block); -} - -void -TypeCheckContext::pop_block_context () +StackedContexts & +TypeCheckContext::block_context () { - rust_assert (!block_stack.empty ()); - block_stack.pop_back (); + return block_stack; } void