Skip to content

gccrs: Fix ICE in path pattern refutability - #4808

Open
u7k4rs6 wants to merge 1 commit into
Rust-GCC:masterfrom
u7k4rs6:fix-4732-path-pattern-refutable
Open

gccrs: Fix ICE in path pattern refutability#4808
u7k4rs6 wants to merge 1 commit into
Rust-GCC:masterfrom
u7k4rs6:fix-4732-path-pattern-refutable

Conversation

@u7k4rs6

@u7k4rs6 u7k4rs6 commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #4732

PathPattern::is_refutable looked up the final path segment's HirId in the HIR item map. That map is keyed on each item's own HirId, and a path segment never carries one, so lookup_hir_item always failed and every path pattern in a let binding or a function parameter reached rust_internal_error_at. The enum check below it was consequently unreachable dead code.

The fix resolves the path before looking it up. Late::visit (AST::PathInExpression&) maps usage on the path expression's NodeId rather than on any segment, so the lookup now uses get_mappings ().get_nodeid () and maps the resolved definition back through lookup_node_to_hir to reach the HIR item. It covers both Values and Types because resolve_path is called with both namespaces and records whichever one matched.

Enum variants are registered with insert_hir_enumitem rather than insert_hir_item, so lookup_hir_item legitimately fails for them and the code falls through to the scrutinee check by design. That check also needed a non-enum ADT case: unit struct path patterns reach it too, and without returning false for them, struct S; let S: S; would hit rust_unreachable, trading one ICE for another on a case no test currently covers.

This requires including rust-finalized-name-resolution-context.h from hir/tree. No file under gcc/rust/hir currently includes anything from gcc/rust/resolve, so this would be the first such dependency. I am open to moving the lookup somewhere else if that layering is unwanted.

refutable-path.rs loses its dg-ice and now asserts the four expected E0005 diagnostics, covering both the let bindings and the function parameters. irrefutable-path.rs keeps its dg-ice but reports a different ICE: with a single-variant enum the parameter is irrefutable, so compilation proceeds into the backend and hits the pre-existing function_set_parameters assert from #4553, which irrefutable-slice.rs already XFAILs on.

Validation passes increased from 11,450 to 11,458 with zero unexpected failures, measured against a baseline taken at e56b411. Four of the new PASS lines are refutable-path.rs. The other four are empty_path2.rs and empty_path3.rs, which arrived upstream in #4799 after that baseline was taken and are unrelated to this change.

PathPattern::is_refutable looked up the final path segment's HirId in the
HIR item map, where it is never a key, so every path pattern in a let or
function parameter hit rust_internal_error_at.

Resolve path patterns before looking up their HIR items, checking both
namespaces. Handle non-enum ADTs as irrefutable to avoid rust_unreachable.

Update path pattern tests to assert the expected E0005 diagnostics.

Fixes Rust-GCC#4732

gcc/rust/ChangeLog:

	* hir/tree/rust-hir-pattern-abstract.cc
	(PathPattern::is_refutable): Resolve the path before looking up its
	HIR item, and treat non-enum ADTs as irrefutable.

gcc/testsuite/ChangeLog:

	* rust/compile/refutable-path.rs: Replace dg-ice with the expected
	E0005 diagnostics.

Signed-off-by: Utkarsh Bahuguna <utkarshbahuguna10@gmail.com>
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 failed to lookup hir item during refutability checks

1 participant