Skip to content

Fix segfault when processing types with unresolved lifetimes in generic arguments#4436

Open
v1bh475u wants to merge 3 commits intoRust-GCC:masterfrom
v1bh475u:fix/typecheck-unresolved-lifetime-segfault
Open

Fix segfault when processing types with unresolved lifetimes in generic arguments#4436
v1bh475u wants to merge 3 commits intoRust-GCC:masterfrom
v1bh475u:fix/typecheck-unresolved-lifetime-segfault

Conversation

@v1bh475u
Copy link


Fixes #3611

When resolving type paths with generic arguments, if a lifetime fails to resolve, regions_from_generic_args() returns an empty vector. Previously, this empty vector was passed to SubstMapper::Resolve, creating types with uninitialized Region data. Later, variance analysis would crash when processing these malformed types.

Fix:

  • resolve_root_path() : check if regions_from_generic_args() failed and return ErrorType immediately instead of creating malformed types
  • visit(ReferenceType&): propagate ErrorType when encountered

@v1bh475u v1bh475u force-pushed the fix/typecheck-unresolved-lifetime-segfault branch 2 times, most recently from b9b1d7c to d4b0c14 Compare February 13, 2026 22:08
@v1bh475u
Copy link
Author

The build fails due to filename. Will fix it but need help for the changelog. After we are certain about the changelog, I will do a final rebase to finish all the changes.

generic_segment.get_generic_args ());
// regions_from_generic_args returns empty vector on error
if (!generic_segment.get_generic_args ().get_lifetime_args ().empty ()
&& regions.empty ())
Copy link
Member

Choose a reason for hiding this comment

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

This change doesnt seem right to me, if the regions are empty and no lifetimes are specified i feel like we should continue on anyway it shouldnt make us return early.

Copy link
Author

Choose a reason for hiding this comment

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

This handles situation when when the lifetimes are specified and we get no regions. This is due to the fact that whenever regions_from_generic_args is unable to resolve any lifetime, it logs error and returns empty vector.

auto resolved = lookup_and_resolve_lifetime (lifetime);
if (!resolved)
{
rust_error_at (lifetime.get_locus (), "unresolved lifetime");
return {};

Hence, I am checking if we had lifetimes in generic args but didn't receive any regions implying that there is some unresolved region.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd probably be best to make regions_from_generic_args return a tl::optional, with tl::nullopt indicating an error.

Copy link
Author

Choose a reason for hiding this comment

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

That would require a lot of changes in the codebase. There are a lot of places where it is always expected to return a value instead of having optional value. Is this what we are aiming to do here?

{
TyTy::BaseType *base = TypeCheckType::Resolve (type.get_base_type ());

if (base->get_kind () == TyTy::TypeKind::ERROR)
Copy link
Member

Choose a reason for hiding this comment

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

i think remove the new line in between base and this check this seems like the fix.

you can also do:

base->isTyTy::ErrorType () is a nicerway to do this if statement.

Copy link
Author

Choose a reason for hiding this comment

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

I am unable to find this function. Do you mean the following?

template <typename T> WARN_UNUSED_RESULT bool is () const

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's probably what phil meant

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-type.cc
	(TypeCheckType::resolve_root_path): check for unresolved
	lifetimes.

Signed-off-by: vibhatsu <maulikbarot2915@gmail.com>
gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-type.cc
	(TypeCheckType::visit): propagate error type in ReferenceType
	resolution

Signed-off-by: vibhatsu <maulikbarot2915@gmail.com>
gcc/testsuite/ChangeLog:

* rust/compile/unresolved-lifetime-in-generic-arg.rs: New test.

Signed-off-by: vibhatsu <maulikbarot2915@gmail.com>
@v1bh475u v1bh475u force-pushed the fix/typecheck-unresolved-lifetime-segfault branch from d4b0c14 to e2c1bf5 Compare February 16, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: segfault with recursive enum

3 participants