Skip to content

Conversation

@Pasta-coder
Copy link
Contributor

gccrs: Emit error when borrowing immutable variable as mutable

Fixes #4289

Rust rules strictly forbid creating a mutable reference ('&mut T') to an
immutable binding. Previously, the compiler failed to validate the
mutability of the source variable when using a 'ref mut' pattern.
This patch adds verification logic to TypeCheckStmt to check the
mutability status of the variable definition.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit):
Add check to ensure 'ref mut' patterns bind to mutable variables.

gcc/testsuite/ChangeLog:

* rust/compile/issue-4289.rs: New test.

Signed-off-by: Jayant Chauhan [email protected]

@Pasta-coder Pasta-coder changed the title Issue 4289 gccrs: Emit error when borrowing immutable variable as mutable Jan 15, 2026
@Pasta-coder Pasta-coder force-pushed the issue-4289 branch 14 times, most recently from 3b13ee4 to 99d54f0 Compare January 15, 2026 18:10
@Pasta-coder Pasta-coder marked this pull request as draft January 15, 2026 18:19
@Pasta-coder Pasta-coder force-pushed the issue-4289 branch 2 times, most recently from 15c8a07 to 3613182 Compare January 16, 2026 09:25
Fixes Rust-GCC#4289

Rust rules strictly forbid creating a mutable reference ('&mut T') to an
immutable binding. Previously, the compiler failed to validate the
mutability of the source variable when using a 'ref mut' pattern.
This patch adds verification logic to TypeCheckStmt to check the
mutability status of the variable definition.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-type-check-stmt.cc (TypeCheckStmt::visit):
	Add check to ensure 'ref mut' patterns bind to mutable variables.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4289.rs: New test.

Signed-off-by: Jayant Chauhan <[email protected]>
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.

mut ref variable should give error if borrowing a non mutable variable

1 participant