Skip to content

Change mutation of non-mutable locals from an error to a lint #134233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
103 changes: 95 additions & 8 deletions compiler/rustc_borrowck/src/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,96 @@
#![allow(rustc::untranslatable_diagnostic)]

use rustc_errors::codes::*;
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, struct_span_code_err};
use rustc_errors::{
Applicability, Diag,
DiagCtxtHandle, DiagMessage,
ErrorGuaranteed, MultiSpan,
struct_span_code_err, struct_span_code_warn
};
use rustc_hir as hir;
use rustc_middle::span_bug;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;

pub(crate) trait BorrowckDiag {
type Guarantee: rustc_errors::EmissionGuarantee;

fn take_existing<'infcx>(cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>, span: Span)
-> Option<(Diag<'infcx, Self::Guarantee>, usize)>;

fn struct_span_diag<'a>(cx: DiagCtxtHandle<'a>, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>)
-> Diag<'a, Self::Guarantee>;

fn buffer_error<'infcx>(cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>, err: Diag<'infcx, Self::Guarantee>);

fn buffer_mut_error<'infcx>(
cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>,
span: Span, diag: Diag<'infcx, Self::Guarantee>, count: usize);
}

pub(crate) struct BError;
impl BorrowckDiag for BError {
type Guarantee = ErrorGuaranteed;
fn take_existing<'infcx>(cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>, span: Span)
-> Option<(Diag<'infcx>, usize)>
{
cx.get_buffered_mut_error(span)
}

fn struct_span_diag<'a>(cx: DiagCtxtHandle<'a>, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>)
-> Diag<'a, Self::Guarantee>
{
cx.struct_span_err(span, msg)
}

fn buffer_error<'infcx>(cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>, err: Diag<'infcx, Self::Guarantee>){
cx.buffer_error(err);
}

fn buffer_mut_error<'infcx>(
cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>,
span: Span, diag: Diag<'infcx, Self::Guarantee>, count: usize)
{
cx.buffer_mut_error(span, diag, count);
}
}

pub(crate) struct BWarn;
impl BorrowckDiag for BWarn {
type Guarantee = ();
fn take_existing<'infcx>(cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>, span: Span)
-> Option<(Diag<'infcx, Self::Guarantee>, usize)>
{
cx.get_buffered_mut_non_error(span)
}

fn struct_span_diag<'a>(cx: DiagCtxtHandle<'a>, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>)
-> Diag<'a, Self::Guarantee>
{
let mut ret = cx.struct_span_warn(span, msg);
ret.is_lint("mut_non_mut".into(), false); // note: just a placeholder so we can detect it later
ret
}

fn buffer_error<'infcx>(cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>, err: Diag<'infcx, Self::Guarantee>){
cx.buffer_non_error(err);
}

fn buffer_mut_error<'infcx>(
cx: &mut crate::MirBorrowckCtxt<'_, 'infcx, '_>,
span: Span, diag: Diag<'infcx, Self::Guarantee>, count: usize)
{
cx.buffer_mut_non_error(span, diag, count);
}
}

#[macro_export]
macro_rules! struct_span_code_diag {
($t:ty, $dcx:expr, $span:expr, $code:expr, $($message:tt)*) => ({
<$t>::struct_span_diag($dcx, $span, format!($($message)*)).with_code($code)
})
}

impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'infcx> {
self.infcx.dcx()
Expand Down Expand Up @@ -258,13 +342,15 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
span: Span,
desc: &str,
is_arg: bool,
) -> Diag<'infcx> {
) -> Diag<'infcx, ()> {
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
struct_span_code_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
let mut ret = struct_span_code_warn!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc);
ret.is_lint("mut_non_mut".into(), false); // note: just a placeholder so we can detect it later
ret
}

pub(crate) fn cannot_assign(&self, span: Span, desc: &str) -> Diag<'infcx> {
struct_span_code_err!(self.dcx(), span, E0594, "cannot assign to {}", desc)
pub(crate) fn cannot_assign<T: BorrowckDiag>(&self, span: Span, desc: &str) -> Diag<'infcx, T::Guarantee> {
struct_span_code_diag!(T, self.dcx(), span, E0594, "cannot assign to {}", desc)
}

pub(crate) fn cannot_move_out_of(
Expand Down Expand Up @@ -341,13 +427,14 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
)
}

pub(crate) fn cannot_borrow_path_as_mutable_because(
pub(crate) fn cannot_borrow_path_as_mutable_because<T: BorrowckDiag>(
&self,
span: Span,
path: &str,
reason: &str,
) -> Diag<'infcx> {
struct_span_code_err!(
) -> Diag<'infcx, T::Guarantee> {
struct_span_code_diag!(
T,
self.dcx(),
span,
E0596,
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3856,7 +3856,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
(place, span): (Place<'tcx>, Span),
assigned_span: Span,
err_place: Place<'tcx>,
) {
) -> Diag<'infcx, ()> {
let (from_arg, local_decl) = match err_place.as_local() {
Some(local) => {
(self.body.local_kind(local) == LocalKind::Arg, Some(&self.body.local_decls[local]))
Expand Down Expand Up @@ -3884,11 +3884,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
Some(decl) => (self.describe_any_place(err_place.as_ref()), decl.source_info.span),
};
let mut err = self.cannot_reassign_immutable(span, &place_description, from_arg);
let msg = if from_arg {
"cannot assign to immutable argument"
} else {
"cannot assign twice to immutable variable"
};
if span != assigned_span && !from_arg {
err.span_label(assigned_span, format!("first assignment to {place_description}"));
}
Expand Down Expand Up @@ -3918,8 +3913,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
);
}
}
let msg =
if from_arg { "cannot assign to immutable argument" }
else { "cannot assign twice to immutable variable" };
err.span_label(span, msg);
self.buffer_error(err);
err
}

fn classify_drop_access_kind(&self, place: PlaceRef<'tcx>) -> StorageDeadOrDrop<'tcx> {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Borrow checker diagnostics.

use rustc_abi::{FieldIdx, VariantIdx};
use rustc_errors::{Applicability, Diag, MultiSpan};
use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan};
use rustc_hir::def::{CtorKind, Namespace};
use rustc_hir::{self as hir, CoroutineKind, LangItem};
use rustc_index::IndexSlice;
Expand Down Expand Up @@ -626,9 +626,9 @@ impl UseSpans<'_> {

/// Add a subdiagnostic to the use of the captured variable, if it exists.
#[allow(rustc::diagnostic_outside_of_impl)]
pub(super) fn var_subdiag(
pub(super) fn var_subdiag<G: EmissionGuarantee>(
self,
err: &mut Diag<'_>,
err: &mut Diag<'_, G>,
kind: Option<rustc_middle::mir::BorrowKind>,
f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause,
) {
Expand Down
Loading