Skip to content

rust: Add refutability check support and enforce irrefutability#4571

Open
riogu wants to merge 3 commits into
Rust-GCC:masterfrom
riogu:irrefutability-checks
Open

rust: Add refutability check support and enforce irrefutability#4571
riogu wants to merge 3 commits into
Rust-GCC:masterfrom
riogu:irrefutability-checks

Conversation

@riogu

@riogu riogu commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

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.

@joao-novo joao-novo force-pushed the irrefutability-checks branch from 3ff9c6f to 9097b3e Compare June 2, 2026 15:50
@riogu riogu force-pushed the irrefutability-checks branch from 9097b3e to a475b34 Compare June 2, 2026 16:11

@dkm dkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very (very) minor comments (drive-by review 😅 )

@@ -0,0 +1,326 @@
// Copyright (C) 2026-2026 Free Software Foundation, Inc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 😅

@joao-novo joao-novo force-pushed the irrefutability-checks branch from a475b34 to 27b33af Compare June 3, 2026 10:20
@joao-novo

Copy link
Copy Markdown

I have fixed both of those things as requested!

riogu and others added 3 commits June 3, 2026 11:42
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>
@joao-novo joao-novo force-pushed the irrefutability-checks branch from 27b33af to 6bb7966 Compare June 3, 2026 10:43
@dkm

dkm commented Jun 5, 2026

Copy link
Copy Markdown
Member

I have fixed both of those things as requested!

Thanks! I'll let the main devs to the actual code review.

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.

3 participants