Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions compiler/rustc_borrowck/src/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use rustc_errors::codes::*;
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, struct_span_code_err};
use rustc_hir as hir;
use rustc_middle::span_bug;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;

impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'infcx> {
self.infcx.dcx()
impl<'diag, 'tcx> crate::MirBorrowckCtxt<'_, 'diag, 'tcx> {
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'diag> {
self.root_cx.dcx()

@lcnr lcnr Jul 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

}

pub(crate) fn cannot_move_when_borrowed(
Expand All @@ -17,7 +17,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
place: &str,
borrow_place: &str,
value_place: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
self.dcx().create_err(crate::session_diagnostics::MoveBorrow {
place,
span,
Expand All @@ -33,7 +33,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
desc: &str,
borrow_span: Span,
borrow_desc: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
span,
Expand All @@ -53,7 +53,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
old_loan_span: Span,
old_opt_via: &str,
old_load_end_span: Option<Span>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
let mut err = struct_span_code_err!(
self.dcx(),
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
desc: &str,
old_loan_span: Span,
old_load_end_span: Option<Span>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
noun_old: &str,
old_opt_via: &str,
previous_end_span: Option<Span>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
old_opt_via: &str,
previous_end_span: Option<Span>,
second_borrow_desc: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let mut err = struct_span_code_err!(
self.dcx(),
new_loan_span,
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
kind_old: &str,
msg_old: &str,
old_load_end_span: Option<Span>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
let mut err = struct_span_code_err!(
self.dcx(),
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
span: Span,
borrow_span: Span,
desc: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
span,
Expand All @@ -255,20 +255,20 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
span: Span,
desc: &str,
is_arg: bool,
) -> Diag<'infcx> {
) -> Diag<'diag> {
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)
}

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

pub(crate) fn cannot_move_out_of(
&self,
move_from_span: Span,
move_from_desc: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
move_from_span,
Expand All @@ -286,7 +286,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
move_from_span: Span,
ty: Ty<'_>,
is_index: Option<bool>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let type_name = match (ty.kind(), is_index) {
(&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
(&ty::Slice(_), _) => "slice",
Expand All @@ -307,7 +307,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
&self,
move_from_span: Span,
container_ty: Ty<'_>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
move_from_span,
Expand All @@ -325,7 +325,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
optional_adverb_for_moved: &str,
moved_path: Option<String>,
primary_message: Option<String>,
) -> Diag<'infcx> {
) -> Diag<'diag> {
if let Some(primary_message) = primary_message {
struct_span_code_err!(self.dcx(), use_span, E0382, "{}", primary_message)
} else {
Expand All @@ -348,7 +348,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
span: Span,
path: &str,
reason: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
span,
Expand All @@ -366,7 +366,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
immutable_place: &str,
immutable_section: &str,
action: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
mutate_span,
Expand All @@ -384,7 +384,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
&self,
span: Span,
yield_span: Span,
) -> Diag<'infcx> {
) -> Diag<'diag> {
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
let mut diag = struct_span_code_err!(
self.dcx(),
Expand Down Expand Up @@ -418,7 +418,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
diag
}

pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'infcx> {
pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
borrow_span,
Expand All @@ -427,7 +427,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
)
}

pub(crate) fn path_does_not_live_long_enough(&self, span: Span, path: &str) -> Diag<'infcx> {
pub(crate) fn path_does_not_live_long_enough(&self, span: Span, path: &str) -> Diag<'diag> {
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path)
}

Expand All @@ -437,7 +437,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return_kind: &str,
reference_desc: &str,
path_desc: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
span,
Expand All @@ -460,7 +460,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
borrowed_path: &str,
capture_span: Span,
scope: &str,
) -> Diag<'infcx> {
) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
closure_span,
Expand All @@ -472,7 +472,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
.with_span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"))
}

pub(crate) fn thread_local_value_does_not_live_long_enough(&self, span: Span) -> Diag<'infcx> {
pub(crate) fn thread_local_value_does_not_live_long_enough(&self, span: Span) -> Diag<'diag> {
struct_span_code_err!(
self.dcx(),
span,
Expand All @@ -481,18 +481,18 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
)
}

pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'infcx> {
pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'diag> {
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed")
}
}

pub(crate) fn borrowed_data_escapes_closure<'tcx>(
tcx: TyCtxt<'tcx>,
pub(crate) fn borrowed_data_escapes_closure<'diag>(
dcx: DiagCtxtHandle<'diag>,
escape_span: Span,
escapes_from: &str,
) -> Diag<'tcx> {
) -> Diag<'diag> {
struct_span_code_err!(
tcx.dcx(),
dcx,
escape_span,
E0521,
"borrowed data escapes outside of {}",
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_borrowck/src/consumers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,13 @@ pub fn get_bodies_with_borrowck_facts(
root_def_id: LocalDefId,
options: ConsumerOptions,
) -> FxHashMap<LocalDefId, BodyWithBorrowckFacts<'_>> {
let mut root_cx =
BorrowCheckRootCtxt::new(tcx, root_def_id, Some(BorrowckConsumer::new(options)));
let tainted_by_errors = Default::default();
let mut root_cx = BorrowCheckRootCtxt::new(
tcx,
root_def_id,
Some(BorrowckConsumer::new(options)),
&tainted_by_errors,
);
root_cx.do_mir_borrowck();
root_cx.consumer.unwrap().bodies
}
37 changes: 19 additions & 18 deletions compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ pub(crate) trait TypeOpInfo<'tcx> {

fn base_universe(&self) -> ty::UniverseIndex;

fn nice_error<'infcx>(
fn nice_error<'diag>(
&self,
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
mbcx: &mut MirBorrowckCtxt<'_, 'diag, 'tcx>,
cause: ObligationCause<'tcx>,
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
) -> Option<Diag<'infcx>>;
) -> Option<Diag<'diag>>;

/// Constraints require that `error_element` appear in the
/// values of `placeholder`, but this cannot be proven to
Expand Down Expand Up @@ -213,13 +213,13 @@ impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
self.base_universe
}

fn nice_error<'infcx>(
fn nice_error<'diag>(
&self,
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
mbcx: &mut MirBorrowckCtxt<'_, 'diag, 'tcx>,
cause: ObligationCause<'tcx>,
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
) -> Option<Diag<'infcx>> {
) -> Option<Diag<'diag>> {
let (infcx, key, _) =
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
let ocx = ObligationCtxt::new(&infcx);
Expand Down Expand Up @@ -257,13 +257,13 @@ where
self.base_universe
}

fn nice_error<'infcx>(
fn nice_error<'diag>(
&self,
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
mbcx: &mut MirBorrowckCtxt<'_, 'diag, 'tcx>,
cause: ObligationCause<'tcx>,
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
) -> Option<Diag<'infcx>> {
) -> Option<Diag<'diag>> {
let (infcx, key, _) =
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
let ocx = ObligationCtxt::new(&infcx);
Expand Down Expand Up @@ -310,13 +310,13 @@ where
self.base_universe
}

fn nice_error<'infcx>(
fn nice_error<'diag>(
&self,
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
mbcx: &mut MirBorrowckCtxt<'_, 'diag, 'tcx>,
cause: ObligationCause<'tcx>,
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
) -> Option<Diag<'infcx>> {
) -> Option<Diag<'diag>> {
let (infcx, key, _) =
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
let ocx = ObligationCtxt::new(&infcx);
Expand Down Expand Up @@ -351,13 +351,13 @@ impl<'tcx> TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> {
self.base_universe
}

fn nice_error<'infcx>(
fn nice_error<'diag>(
&self,
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
mbcx: &mut MirBorrowckCtxt<'_, 'diag, 'tcx>,
cause: ObligationCause<'tcx>,
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
) -> Option<Diag<'infcx>> {
) -> Option<Diag<'diag>> {
let (infcx, key, _) =
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
let ocx = ObligationCtxt::new(&infcx);
Expand All @@ -384,13 +384,13 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
self.base_universe.unwrap()
}

fn nice_error<'infcx>(
fn nice_error<'diag>(
&self,
mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
mbcx: &mut MirBorrowckCtxt<'_, 'diag, 'tcx>,
_cause: ObligationCause<'tcx>,
placeholder_region: ty::Region<'tcx>,
error_region: Option<ty::Region<'tcx>>,
) -> Option<Diag<'infcx>> {
) -> Option<Diag<'diag>> {
try_extract_error_from_region_constraints(
mbcx.infcx,
mbcx.mir_def_id(),
Expand All @@ -404,6 +404,7 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
|vid| RegionVariableOrigin::Nll(mbcx.regioncx.definitions[vid].origin),
|vid| mbcx.regioncx.definitions[vid].universe,
)
.map(|d| d.with_dcx(mbcx.dcx()))
}
}

Expand Down
Loading
Loading