rust: Add refutability check support and enforce irrefutability#4571
Open
riogu wants to merge 3 commits into
Open
rust: Add refutability check support and enforce irrefutability#4571riogu wants to merge 3 commits into
riogu wants to merge 3 commits into
Conversation
3ff9c6f to
9097b3e
Compare
9097b3e to
a475b34
Compare
dkm
reviewed
Jun 3, 2026
dkm
left a comment
Member
There was a problem hiding this comment.
Very (very) minor comments (drive-by review 😅 )
| @@ -0,0 +1,326 @@ | |||
| // Copyright (C) 2026-2026 Free Software Foundation, Inc. | |||
Member
There was a problem hiding this comment.
I believe only 2026 will do :)
Suggested change
| // Copyright (C) 2026-2026 Free Software Foundation, Inc. | |
| // Copyright (C) 2026 Free Software Foundation, Inc. |
| return adt.number_of_variants () > 1; | ||
| } | ||
| } | ||
| /* cannot have a Path that is neither a constant or an enum-like ADT */ |
Member
There was a problem hiding this comment.
I know we have other occurrences of mixing both C++ and C style comment, but I think it would be nice, in particular for a new file, to have only C++ style comment 😅
a475b34 to
27b33af
Compare
|
I have fixed both of those things as requested! |
These const overloads are needed by the type-aware refutability check in HIR pattern nodes, which accesses pattern fields through const references. Addresses: Rust-GCC#2082 gcc/rust/ChangeLog: * hir/tree/rust-hir-pattern.h: Add const overloads. Co-authored-by: João Novo <joao.c.novo@tecnico.ulisboa.pt> Signed-off-by: Egas Ribeiro <egas.g.ribeiro@tecnico.ulisboa.pt>
This adds the is_refutable() and is_refutable(BaseType&) virtual methods to HIR pattern nodes, distinguishing patterns whose refutability can be determined syntactically from those requiring type context. The syntactic overload returns a definitive answer for wildcards and literals. Other patterns that depend on type context to be checked for refutability, or call is_refutable() recursively, implement the typed overload and ICE if called via the syntactic overload. Addresses: Rust-GCC#2082 gcc/rust/ChangeLog: * Make-lang.in: Add rust-hir-pattern-abstract.o. * hir/tree/rust-hir-path.h: Add is_refutable overrides. * hir/tree/rust-hir-pattern-abstract.h (class BaseType): Add typed is_refutable virtual method. * hir/tree/rust-hir-pattern.h: Add is_refutable to all pattern kinds. * hir/tree/rust-hir-pattern-abstract.cc: New file. Implement typed is_refutable for PathPattern, SlicePattern, TupleStructPattern. Co-authored-by: João Novo <joao.c.novo@tecnico.ulisboa.pt> Signed-off-by: Egas Ribeiro <egas.g.ribeiro@tecnico.ulisboa.pt>
Add diagnostics for refutable patterns appearing in positions that require irrefutability: let bindings (without an else clause) and function parameters. Uses the type-aware is_refutable(BaseType&) introduced in the previous commit. The existing syntactic check for the function parameters case done during AST lowering is reduced to handle only bare literal patterns, with the comprehensive check deferred to PatternChecker after the type checker populates the necessary type information. Add comprehensive tests that cover the implemented refutability checks. Addresses: Rust-GCC#2082 gcc/rust/ChangeLog: * checks/errors/rust-hir-pattern-analysis.cc (PatternChecker::visit): Diagnose refutable patterns in Function parameters and LetStmt bindings. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Remove pattern kind whitelist; only error on bare literal patterns at lowering. gcc/testsuite/ChangeLog: * rust/compile/irrefutable-alt.rs: New test. * rust/compile/irrefutable-basic.rs: New test. * rust/compile/irrefutable-identifier-binding.rs: New test. * rust/compile/irrefutable-nested.rs: New test. * rust/compile/irrefutable-path.rs: New test. * rust/compile/irrefutable-range.rs: New test. * rust/compile/irrefutable-reference.rs: New test. * rust/compile/irrefutable-slice.rs: New test. * rust/compile/irrefutable-struct.rs: New test. * rust/compile/irrefutable-tuple.rs: New test. * rust/compile/refutable-alt.rs: New test. * rust/compile/refutable-binding.rs: New test. * rust/compile/refutable-literal-let.rs: New test. * rust/compile/refutable-literal-parameter.rs: New test. * rust/compile/refutable-path.rs: New test. * rust/compile/refutable-range.rs: New test. * rust/compile/refutable-reference.rs: New test. * rust/compile/refutable-struct.rs: New test. * rust/compile/refutable-tuple.rs: New test. Co-authored-by: João Novo <joao.c.novo@tecnico.ulisboa.pt> Signed-off-by: Egas Ribeiro <egas.g.ribeiro@tecnico.ulisboa.pt>
27b33af to
6bb7966
Compare
Member
Thanks! I'll let the main devs to the actual code review. |
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.
This PR consists of 3 commits:
It adds is_refutable methods to HIR pattern nodes, and it enforces
irrefutability in let bindings and function parameters in the codebase where
relevant.
It also adds several tests for pattern feature coverage, which should all pass
once the relevant missing parts of #2082 are implemented.