From de60e47a93a7eae52010a700d21cf867ffdc2e9d Mon Sep 17 00:00:00 2001 From: WaterWhisperer Date: Wed, 27 May 2026 15:50:10 +0800 Subject: [PATCH 1/2] feat: add diagnostic for E0596 --- crates/hir-ty/src/infer.rs | 4 ++ crates/hir-ty/src/infer/pat.rs | 2 +- crates/hir/src/diagnostics.rs | 10 +++++ .../src/handlers/cannot_borrow_as_mutable.rs | 37 +++++++++++++++++++ crates/ide-diagnostics/src/lib.rs | 2 + 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs index 2df2789a2eee..6179d8dcf832 100644 --- a/crates/hir-ty/src/infer.rs +++ b/crates/hir-ty/src/infer.rs @@ -405,6 +405,10 @@ pub enum InferenceDiagnostic { expr: ExprId, found: StoredTy, }, + CannotBorrowAsMutable { + #[type_visitable(ignore)] + pat: PatId, + }, CannotImplicitlyDerefTraitObject { #[type_visitable(ignore)] pat: PatId, diff --git a/crates/hir-ty/src/infer/pat.rs b/crates/hir-ty/src/infer/pat.rs index c36c29d6c729..543de847cd22 100644 --- a/crates/hir-ty/src/infer/pat.rs +++ b/crates/hir-ty/src/infer/pat.rs @@ -916,7 +916,7 @@ impl<'a, 'db> InferenceContext<'a, 'db> { if matches!(bm.0, ByRef::Yes(Mutability::Mut)) && let MutblCap::WeaklyNot = pat_info.max_ref_mutbl { - // FIXME: Emit an error: cannot borrow as mutable inside an `&` pattern. + self.push_diagnostic(InferenceDiagnostic::CannotBorrowAsMutable { pat }); } // ...and store it in a side table: diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index f3188c9aada5..edd24442c425 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -104,6 +104,7 @@ diagnostics![AnyDiagnostic<'db> -> AwaitOutsideOfAsync, BreakOutsideOfLoop, CannotBeDereferenced<'db>, + CannotBorrowAsMutable, CannotImplicitlyDerefTraitObject<'db>, CannotIndexInto<'db>, CastToUnsized<'db>, @@ -335,6 +336,11 @@ pub struct CannotBeDereferenced<'db> { pub found: Type<'db>, } +#[derive(Debug)] +pub struct CannotBorrowAsMutable { + pub pat: InFile, +} + #[derive(Debug)] pub struct CannotImplicitlyDerefTraitObject<'db> { pub pat: InFile, @@ -980,6 +986,10 @@ impl<'db> AnyDiagnostic<'db> { let expr = expr_syntax(*expr)?; CannotBeDereferenced { expr, found: new_ty(found.as_ref()) }.into() } + InferenceDiagnostic::CannotBorrowAsMutable { pat } => { + let pat = pat_syntax(*pat)?.map(Into::into); + CannotBorrowAsMutable { pat }.into() + } InferenceDiagnostic::CannotImplicitlyDerefTraitObject { pat, found } => { let pat = pat_syntax(*pat)?.map(Into::into); CannotImplicitlyDerefTraitObject { pat, found: new_ty(found.as_ref()) }.into() diff --git a/crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs b/crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs new file mode 100644 index 000000000000..f1ae2c420398 --- /dev/null +++ b/crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs @@ -0,0 +1,37 @@ +use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext}; + +// Diagnostic: cannot-borrow-as-mutable +// +// This diagnostic is triggered when a binding tries to mutably borrow through +// an `&` pattern. +pub(crate) fn cannot_borrow_as_mutable( + ctx: &DiagnosticsContext<'_, '_>, + d: &hir::CannotBorrowAsMutable, +) -> Diagnostic { + Diagnostic::new_with_syntax_node_ptr( + ctx, + DiagnosticCode::RustcHardError("E0596"), + "cannot borrow as mutable inside an `&` pattern", + d.pat.map(Into::into), + ) + .stable() +} + +#[cfg(test)] +mod tests { + use crate::tests::check_diagnostics; + + #[test] + fn cannot_borrow_as_mutable_inside_shared_ref_pattern() { + check_diagnostics( + r#" +#![feature(ref_pat_eat_one_layer_2024)] + +fn main() { + let &ref mut _x = &mut 0; + //^^^^^^^^^^ error: cannot borrow as mutable inside an `&` pattern +} +"#, + ); + } +} diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index aec68b55c78d..18adc3e1f6d3 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -34,6 +34,7 @@ mod handlers { pub(crate) mod bad_rtn; pub(crate) mod break_outside_of_loop; pub(crate) mod cannot_be_dereferenced; + pub(crate) mod cannot_borrow_as_mutable; pub(crate) mod cannot_implicitly_deref_trait_object; pub(crate) mod cannot_index_into; pub(crate) mod duplicate_field; @@ -437,6 +438,7 @@ pub fn semantic_diagnostics( let d = match diag { AnyDiagnostic::AwaitOutsideOfAsync(d) => handlers::await_outside_of_async::await_outside_of_async(&ctx, &d), AnyDiagnostic::CannotBeDereferenced(d) => handlers::cannot_be_dereferenced::cannot_be_dereferenced(&ctx, &d), + AnyDiagnostic::CannotBorrowAsMutable(d) => handlers::cannot_borrow_as_mutable::cannot_borrow_as_mutable(&ctx, &d), AnyDiagnostic::CannotImplicitlyDerefTraitObject(d) => handlers::cannot_implicitly_deref_trait_object::cannot_implicitly_deref_trait_object(&ctx, &d), AnyDiagnostic::CannotIndexInto(d) => handlers::cannot_index_into::cannot_index_into(&ctx, &d), AnyDiagnostic::CastToUnsized(d) => handlers::invalid_cast::cast_to_unsized(&ctx, &d), From 3b5a1b3b37220e7984e45c409959e016eef8dd62 Mon Sep 17 00:00:00 2001 From: WaterWhisperer Date: Fri, 29 May 2026 21:20:17 +0800 Subject: [PATCH 2/2] rename E0596 ref pattern diagnostic --- crates/hir-ty/src/infer.rs | 2 +- crates/hir-ty/src/infer/pat.rs | 2 +- crates/hir/src/diagnostics.rs | 8 ++++---- ...not_borrow_as_mutable.rs => mut_ref_in_imm_ref_pat.rs} | 8 ++++---- crates/ide-diagnostics/src/lib.rs | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) rename crates/ide-diagnostics/src/handlers/{cannot_borrow_as_mutable.rs => mut_ref_in_imm_ref_pat.rs} (80%) diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs index 6179d8dcf832..e6c821457ca1 100644 --- a/crates/hir-ty/src/infer.rs +++ b/crates/hir-ty/src/infer.rs @@ -405,7 +405,7 @@ pub enum InferenceDiagnostic { expr: ExprId, found: StoredTy, }, - CannotBorrowAsMutable { + MutRefInImmRefPat { #[type_visitable(ignore)] pat: PatId, }, diff --git a/crates/hir-ty/src/infer/pat.rs b/crates/hir-ty/src/infer/pat.rs index 543de847cd22..25f33e87e753 100644 --- a/crates/hir-ty/src/infer/pat.rs +++ b/crates/hir-ty/src/infer/pat.rs @@ -916,7 +916,7 @@ impl<'a, 'db> InferenceContext<'a, 'db> { if matches!(bm.0, ByRef::Yes(Mutability::Mut)) && let MutblCap::WeaklyNot = pat_info.max_ref_mutbl { - self.push_diagnostic(InferenceDiagnostic::CannotBorrowAsMutable { pat }); + self.push_diagnostic(InferenceDiagnostic::MutRefInImmRefPat { pat }); } // ...and store it in a side table: diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index edd24442c425..9367fcf789e2 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -104,7 +104,6 @@ diagnostics![AnyDiagnostic<'db> -> AwaitOutsideOfAsync, BreakOutsideOfLoop, CannotBeDereferenced<'db>, - CannotBorrowAsMutable, CannotImplicitlyDerefTraitObject<'db>, CannotIndexInto<'db>, CastToUnsized<'db>, @@ -134,6 +133,7 @@ diagnostics![AnyDiagnostic<'db> -> MissingMatchArms, MissingUnsafe, MovedOutOfRef<'db>, + MutRefInImmRefPat, MutableRefBinding, NeedMut, NonExhaustiveLet, @@ -337,7 +337,7 @@ pub struct CannotBeDereferenced<'db> { } #[derive(Debug)] -pub struct CannotBorrowAsMutable { +pub struct MutRefInImmRefPat { pub pat: InFile, } @@ -986,9 +986,9 @@ impl<'db> AnyDiagnostic<'db> { let expr = expr_syntax(*expr)?; CannotBeDereferenced { expr, found: new_ty(found.as_ref()) }.into() } - InferenceDiagnostic::CannotBorrowAsMutable { pat } => { + InferenceDiagnostic::MutRefInImmRefPat { pat } => { let pat = pat_syntax(*pat)?.map(Into::into); - CannotBorrowAsMutable { pat }.into() + MutRefInImmRefPat { pat }.into() } InferenceDiagnostic::CannotImplicitlyDerefTraitObject { pat, found } => { let pat = pat_syntax(*pat)?.map(Into::into); diff --git a/crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs b/crates/ide-diagnostics/src/handlers/mut_ref_in_imm_ref_pat.rs similarity index 80% rename from crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs rename to crates/ide-diagnostics/src/handlers/mut_ref_in_imm_ref_pat.rs index f1ae2c420398..d3c83be3597d 100644 --- a/crates/ide-diagnostics/src/handlers/cannot_borrow_as_mutable.rs +++ b/crates/ide-diagnostics/src/handlers/mut_ref_in_imm_ref_pat.rs @@ -1,12 +1,12 @@ use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext}; -// Diagnostic: cannot-borrow-as-mutable +// Diagnostic: mut-ref-in-imm-ref-pat // // This diagnostic is triggered when a binding tries to mutably borrow through // an `&` pattern. -pub(crate) fn cannot_borrow_as_mutable( +pub(crate) fn mut_ref_in_imm_ref_pat( ctx: &DiagnosticsContext<'_, '_>, - d: &hir::CannotBorrowAsMutable, + d: &hir::MutRefInImmRefPat, ) -> Diagnostic { Diagnostic::new_with_syntax_node_ptr( ctx, @@ -22,7 +22,7 @@ mod tests { use crate::tests::check_diagnostics; #[test] - fn cannot_borrow_as_mutable_inside_shared_ref_pattern() { + fn mut_ref_in_imm_ref_pat() { check_diagnostics( r#" #![feature(ref_pat_eat_one_layer_2024)] diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index 18adc3e1f6d3..e43594a8147d 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -34,7 +34,6 @@ mod handlers { pub(crate) mod bad_rtn; pub(crate) mod break_outside_of_loop; pub(crate) mod cannot_be_dereferenced; - pub(crate) mod cannot_borrow_as_mutable; pub(crate) mod cannot_implicitly_deref_trait_object; pub(crate) mod cannot_index_into; pub(crate) mod duplicate_field; @@ -65,6 +64,7 @@ mod handlers { pub(crate) mod missing_match_arms; pub(crate) mod missing_unsafe; pub(crate) mod moved_out_of_ref; + pub(crate) mod mut_ref_in_imm_ref_pat; pub(crate) mod mutability_errors; pub(crate) mod mutable_ref; pub(crate) mod no_such_field; @@ -438,7 +438,6 @@ pub fn semantic_diagnostics( let d = match diag { AnyDiagnostic::AwaitOutsideOfAsync(d) => handlers::await_outside_of_async::await_outside_of_async(&ctx, &d), AnyDiagnostic::CannotBeDereferenced(d) => handlers::cannot_be_dereferenced::cannot_be_dereferenced(&ctx, &d), - AnyDiagnostic::CannotBorrowAsMutable(d) => handlers::cannot_borrow_as_mutable::cannot_borrow_as_mutable(&ctx, &d), AnyDiagnostic::CannotImplicitlyDerefTraitObject(d) => handlers::cannot_implicitly_deref_trait_object::cannot_implicitly_deref_trait_object(&ctx, &d), AnyDiagnostic::CannotIndexInto(d) => handlers::cannot_index_into::cannot_index_into(&ctx, &d), AnyDiagnostic::CastToUnsized(d) => handlers::invalid_cast::cast_to_unsized(&ctx, &d), @@ -479,6 +478,7 @@ pub fn semantic_diagnostics( AnyDiagnostic::MissingMatchArms(d) => handlers::missing_match_arms::missing_match_arms(&ctx, &d), AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d), AnyDiagnostic::MovedOutOfRef(d) => handlers::moved_out_of_ref::moved_out_of_ref(&ctx, &d), + AnyDiagnostic::MutRefInImmRefPat(d) => handlers::mut_ref_in_imm_ref_pat::mut_ref_in_imm_ref_pat(&ctx, &d), AnyDiagnostic::MutableRefBinding(d) => handlers::mutable_ref::mutable_ref_binding(&ctx, &d), AnyDiagnostic::NeedMut(d) => match handlers::mutability_errors::need_mut(&ctx, &d) { Some(it) => it,