Skip to content

Commit

Permalink
gccrs: use StackedContexts for block context
Browse files Browse the repository at this point in the history
Replaces the DIY vector stack with a StackedContexts object for block
scopes in the type checker context.

Fixes #3284.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check.h (class TypeCheckContext): add
	header file and use StackedContexts for blocks
	* typecheck/rust-typecheck-context.cc: update methods
	* typecheck/rust-hir-trait-resolve.cc: refactor function calls
	* typecheck/rust-hir-type-check-implitem.cc: refactor function calls
	* typecheck/rust-hir-type-check-type.cc: refactor function calls

Signed-off-by: Prajwal S N <[email protected]>
  • Loading branch information
snprajwal committed Dec 16, 2024
1 parent daa2977 commit a4be57a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 35 deletions.
4 changes: 2 additions & 2 deletions gcc/rust/typecheck/rust-hir-trait-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<TraitItemReference> item_refs;
for (auto &item : trait_reference->get_trait_items ())
{
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-implitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions gcc/rust/typecheck/rust-hir-type-check.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<TypeCheckBlockContextItem> &block_context ();

void iterate (std::function<bool (HirId, TyTy::BaseType *)> cb);

Expand Down Expand Up @@ -282,7 +280,7 @@ class TypeCheckContext
std::vector<std::pair<TypeCheckContextItem, TyTy::BaseType *>>
return_type_stack;
std::vector<TyTy::BaseType *> loop_type_stack;
std::vector<TypeCheckBlockContextItem> block_stack;
StackedContexts<TypeCheckBlockContextItem> block_stack;
std::map<DefId, TraitReference> trait_context;
std::map<HirId, TyTy::BaseType *> receiver_context;
std::map<HirId, AssociatedImplTrait> associated_impl_traits;
Expand Down
26 changes: 3 additions & 23 deletions gcc/rust/typecheck/rust-typecheck-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeCheckBlockContextItem> &
TypeCheckContext::block_context ()
{
rust_assert (!block_stack.empty ());
block_stack.pop_back ();
return block_stack;
}

void
Expand Down

0 comments on commit a4be57a

Please sign in to comment.