Skip to content

Commit 97bfc59

Browse files
committed
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]>
1 parent cf48d44 commit 97bfc59

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

gcc/rust/expand/rust-derive-eq.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,15 @@ DeriveEq::visit_enum (Enum &item)
207207
void
208208
DeriveEq::visit_union (Union &item)
209209
{
210-
rust_error_at (item.get_locus (), "derive(Eq) cannot be used on unions");
210+
auto types = std::vector<std::unique_ptr<Type>> ();
211+
212+
for (auto &field : item.get_variants ())
213+
types.emplace_back (field.get_field_type ().reconstruct ());
214+
215+
expanded = eq_impls (assert_receiver_is_total_eq_fn (std::move (types)),
216+
item.get_identifier ().as_string (),
217+
item.get_generic_params ());
211218
}
212219

213220
} // namespace AST
214221
} // namespace Rust
215-
g

0 commit comments

Comments
 (0)