gccrs: Fix ICE in path pattern refutability - #4808
Open
u7k4rs6 wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.