Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(typecheck): StackedContexts for blocks #3305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading