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

gccrs: improve handling of Self Type paths #3269

Merged
merged 1 commit into from
Nov 27, 2024
Merged

Conversation

philberty
Copy link
Member

TypePaths have special handling for Self where we can look at the current ctx for more acurate TypeAlias information if required. We cant do this for Impl contexts but not for Traits as we might as well fall back to the TypePathProbe.

The other issue was the dyn type comming in because Foo::foo and Foo is a trait reference we represent this as a dyn type as the root resolved path but then find the associated impl block for this but we cannot do this when we have resolved to a Dyn because this is just a representation that we know we are talking about a trait not because we are actually working with a real Dyn type.

Fixes #2907

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): track trait
* typecheck/rust-hir-type-check-implitem.cc: trait block
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): dont when dyn
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): look at Self contenxt (TypeCheckType::resolve_root_path): track if Self (TypeCheckType::resolve_associated_type): look at current context for associated types
* typecheck/rust-hir-type-check-type.h: change prototype
* typecheck/rust-hir-type-check.h (class TypeCheckBlockContextItem): new context system to track current state
* typecheck/rust-typecheck-context.cc (TypeCheckContext::have_block_context): likewise (TypeCheckContext::peek_block_context): likewise (TypeCheckContext::push_block_context): likewise (TypeCheckContext::pop_block_context): likewise (TypeCheckBlockContextItem::Item::Item): likewise (TypeCheckBlockContextItem::TypeCheckBlockContextItem): likewise (TypeCheckBlockContextItem::is_impl_block): likewise (TypeCheckBlockContextItem::is_trait_block): likewise (TypeCheckBlockContextItem::get_impl_block): likewise (TypeCheckBlockContextItem::get_trait): likewise

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-2907.rs: New test.

TypePaths have special handling for Self where we can look at the current ctx
for more acurate TypeAlias information if required. We cant do this for Impl
contexts but not for Traits as we might as well fall back to the TypePathProbe.

The other issue was the dyn type comming in because Foo::foo and Foo is a trait
reference we represent this as a dyn type as the root resolved path but then find
the associated impl block for this but we cannot do this when we have resolved to
a Dyn because this is just a representation that we know we are talking about a
trait not because we are actually working with a real Dyn type.

Fixes #2907

gcc/rust/ChangeLog:

	* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): track trait
	* typecheck/rust-hir-type-check-implitem.cc: trait block
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments): dont when dyn
	* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): look at Self contenxt
	(TypeCheckType::resolve_root_path): track if Self
	(TypeCheckType::resolve_associated_type): look at current context for associated types
	* typecheck/rust-hir-type-check-type.h: change prototype
	* typecheck/rust-hir-type-check.h (class TypeCheckBlockContextItem):
	new context system to track current state
	* typecheck/rust-typecheck-context.cc (TypeCheckContext::have_block_context): likewise
	(TypeCheckContext::peek_block_context): likewise
	(TypeCheckContext::push_block_context): likewise
	(TypeCheckContext::pop_block_context): likewise
	(TypeCheckBlockContextItem::Item::Item): likewise
	(TypeCheckBlockContextItem::TypeCheckBlockContextItem): likewise
	(TypeCheckBlockContextItem::is_impl_block): likewise
	(TypeCheckBlockContextItem::is_trait_block): likewise
	(TypeCheckBlockContextItem::get_impl_block): likewise
	(TypeCheckBlockContextItem::get_trait): likewise

gcc/testsuite/ChangeLog:

	* rust/compile/nr2/exclude: nr2 cant handle this
	* rust/compile/issue-2907.rs: New test.

Signed-off-by: Philip Herron <[email protected]>
@philberty philberty force-pushed the phil/self-path-pr-fix branch from 0670652 to 1b3374d Compare November 27, 2024 16:44
@philberty philberty added this pull request to the merge queue Nov 27, 2024
Merged via the queue into master with commit c11ee4f Nov 27, 2024
13 checks passed
Comment on lines +180 to +205
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 ()
{
rust_assert (!block_stack.empty ());
block_stack.pop_back ();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we can remove all of that code if we use the StackedContexts class we have: https://github.com/Rust-GCC/gccrs/blob/master/gcc/rust/util/rust-stacked-contexts.h

would remove some of the lines of code. I can turn this into a good-first-pr issue if you'd like

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philberty philberty deleted the phil/self-path-pr-fix branch December 12, 2024 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Unable to call trait method as function when implemented on bound
2 participants