Skip to content

Commit 7b21c18

Browse files
committed
Auto merge of rust-lang#126996 - oli-obk:do_not_count_errors, r=nnethercote
Automatically taint InferCtxt when errors are emitted r? `@nnethercote` Basically `InferCtxt::dcx` now returns a `DiagCtxt` that refers back to the `Cell<Option<ErrorGuaranteed>>` of the `InferCtxt` and thus when invoking `Diag::emit`, and the diagnostic is an error, we taint the `InferCtxt` directly. That change on its own has no effect at all, because `InferCtxt` already tracks whether errors have been emitted by recording the global error count when it gets opened, and checking at the end whether the count changed. So I removed that error count check, which had a bit of fallout that I immediately fixed by invoking `InferCtxt::dcx` instead of `TyCtxt::dcx` in a bunch of places. The remaining new errors are because an error was reported in another query, and never bubbled up. I think they are minor enough for this to be ok, and sometimes it actually improves diagnostics, by not silencing useful diagnostics anymore. fixes rust-lang#126485 (cc `@olafes)` There are more improvements we can do (like tainting in hir ty lowering), but I would rather do that in follow up PRs, because it requires some refactorings.
2 parents f92a6c4 + bd111f5 commit 7b21c18

File tree

77 files changed

+886
-816
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+886
-816
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_middle::span_bug;
66
use rustc_middle::ty::{self, Ty, TyCtxt};
77
use rustc_span::Span;
88

9-
impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
10-
pub fn dcx(&self) -> DiagCtxtHandle<'tcx> {
9+
impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
10+
pub fn dcx(&self) -> DiagCtxtHandle<'infcx> {
1111
self.infcx.dcx()
1212
}
1313

@@ -18,7 +18,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
1818
place: &str,
1919
borrow_place: &str,
2020
value_place: &str,
21-
) -> Diag<'tcx> {
21+
) -> Diag<'infcx> {
2222
self.dcx().create_err(crate::session_diagnostics::MoveBorrow {
2323
place,
2424
span,
@@ -34,7 +34,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
3434
desc: &str,
3535
borrow_span: Span,
3636
borrow_desc: &str,
37-
) -> Diag<'tcx> {
37+
) -> Diag<'infcx> {
3838
struct_span_code_err!(
3939
self.dcx(),
4040
span,
@@ -54,7 +54,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
5454
old_loan_span: Span,
5555
old_opt_via: &str,
5656
old_load_end_span: Option<Span>,
57-
) -> Diag<'tcx> {
57+
) -> Diag<'infcx> {
5858
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
5959
let mut err = struct_span_code_err!(
6060
self.dcx(),
@@ -101,7 +101,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
101101
desc: &str,
102102
old_loan_span: Span,
103103
old_load_end_span: Option<Span>,
104-
) -> Diag<'tcx> {
104+
) -> Diag<'infcx> {
105105
let mut err = struct_span_code_err!(
106106
self.dcx(),
107107
new_loan_span,
@@ -134,7 +134,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
134134
noun_old: &str,
135135
old_opt_via: &str,
136136
previous_end_span: Option<Span>,
137-
) -> Diag<'tcx> {
137+
) -> Diag<'infcx> {
138138
let mut err = struct_span_code_err!(
139139
self.dcx(),
140140
new_loan_span,
@@ -166,7 +166,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
166166
old_opt_via: &str,
167167
previous_end_span: Option<Span>,
168168
second_borrow_desc: &str,
169-
) -> Diag<'tcx> {
169+
) -> Diag<'infcx> {
170170
let mut err = struct_span_code_err!(
171171
self.dcx(),
172172
new_loan_span,
@@ -198,7 +198,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
198198
kind_old: &str,
199199
msg_old: &str,
200200
old_load_end_span: Option<Span>,
201-
) -> Diag<'tcx> {
201+
) -> Diag<'infcx> {
202202
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
203203
let mut err = struct_span_code_err!(
204204
self.dcx(),
@@ -239,7 +239,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
239239
span: Span,
240240
borrow_span: Span,
241241
desc: &str,
242-
) -> Diag<'tcx> {
242+
) -> Diag<'infcx> {
243243
struct_span_code_err!(
244244
self.dcx(),
245245
span,
@@ -256,20 +256,20 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
256256
span: Span,
257257
desc: &str,
258258
is_arg: bool,
259-
) -> Diag<'tcx> {
259+
) -> Diag<'infcx> {
260260
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
261261
struct_span_code_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
262262
}
263263

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

268268
pub(crate) fn cannot_move_out_of(
269269
&self,
270270
move_from_span: Span,
271271
move_from_desc: &str,
272-
) -> Diag<'tcx> {
272+
) -> Diag<'infcx> {
273273
struct_span_code_err!(
274274
self.dcx(),
275275
move_from_span,
@@ -287,7 +287,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
287287
move_from_span: Span,
288288
ty: Ty<'_>,
289289
is_index: Option<bool>,
290-
) -> Diag<'tcx> {
290+
) -> Diag<'infcx> {
291291
let type_name = match (&ty.kind(), is_index) {
292292
(&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
293293
(&ty::Slice(_), _) => "slice",
@@ -308,7 +308,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
308308
&self,
309309
move_from_span: Span,
310310
container_ty: Ty<'_>,
311-
) -> Diag<'tcx> {
311+
) -> Diag<'infcx> {
312312
struct_span_code_err!(
313313
self.dcx(),
314314
move_from_span,
@@ -325,7 +325,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
325325
verb: &str,
326326
optional_adverb_for_moved: &str,
327327
moved_path: Option<String>,
328-
) -> Diag<'tcx> {
328+
) -> Diag<'infcx> {
329329
let moved_path = moved_path.map(|mp| format!(": `{mp}`")).unwrap_or_default();
330330

331331
struct_span_code_err!(
@@ -344,7 +344,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
344344
span: Span,
345345
path: &str,
346346
reason: &str,
347-
) -> Diag<'tcx> {
347+
) -> Diag<'infcx> {
348348
struct_span_code_err!(
349349
self.dcx(),
350350
span,
@@ -362,7 +362,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
362362
immutable_place: &str,
363363
immutable_section: &str,
364364
action: &str,
365-
) -> Diag<'tcx> {
365+
) -> Diag<'infcx> {
366366
struct_span_code_err!(
367367
self.dcx(),
368368
mutate_span,
@@ -380,7 +380,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
380380
&self,
381381
span: Span,
382382
yield_span: Span,
383-
) -> Diag<'tcx> {
383+
) -> Diag<'infcx> {
384384
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
385385
struct_span_code_err!(
386386
self.dcx(),
@@ -391,7 +391,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
391391
.with_span_label(yield_span, "possible yield occurs here")
392392
}
393393

394-
pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'tcx> {
394+
pub(crate) fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> Diag<'infcx> {
395395
struct_span_code_err!(
396396
self.dcx(),
397397
borrow_span,
@@ -400,7 +400,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
400400
)
401401
}
402402

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

@@ -410,7 +410,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
410410
return_kind: &str,
411411
reference_desc: &str,
412412
path_desc: &str,
413-
) -> Diag<'tcx> {
413+
) -> Diag<'infcx> {
414414
struct_span_code_err!(
415415
self.dcx(),
416416
span,
@@ -433,7 +433,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
433433
borrowed_path: &str,
434434
capture_span: Span,
435435
scope: &str,
436-
) -> Diag<'tcx> {
436+
) -> Diag<'infcx> {
437437
struct_span_code_err!(
438438
self.dcx(),
439439
closure_span,
@@ -445,7 +445,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
445445
.with_span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"))
446446
}
447447

448-
pub(crate) fn thread_local_value_does_not_live_long_enough(&self, span: Span) -> Diag<'tcx> {
448+
pub(crate) fn thread_local_value_does_not_live_long_enough(&self, span: Span) -> Diag<'infcx> {
449449
struct_span_code_err!(
450450
self.dcx(),
451451
span,
@@ -454,7 +454,7 @@ impl<'tcx> crate::MirBorrowckCtxt<'_, '_, '_, 'tcx> {
454454
)
455455
}
456456

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

compiler/rustc_borrowck/src/dataflow.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ use std::fmt;
1515
use crate::{places_conflict, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext};
1616

1717
/// The results of the dataflow analyses used by the borrow checker.
18-
pub struct BorrowckResults<'mir, 'tcx> {
19-
pub(crate) borrows: Results<'tcx, Borrows<'mir, 'tcx>>,
20-
pub(crate) uninits: Results<'tcx, MaybeUninitializedPlaces<'mir, 'tcx>>,
21-
pub(crate) ever_inits: Results<'tcx, EverInitializedPlaces<'mir, 'tcx>>,
18+
pub struct BorrowckResults<'a, 'mir, 'tcx> {
19+
pub(crate) borrows: Results<'tcx, Borrows<'a, 'mir, 'tcx>>,
20+
pub(crate) uninits: Results<'tcx, MaybeUninitializedPlaces<'a, 'mir, 'tcx>>,
21+
pub(crate) ever_inits: Results<'tcx, EverInitializedPlaces<'a, 'mir, 'tcx>>,
2222
}
2323

2424
/// The transient state of the dataflow analyses used by the borrow checker.
2525
#[derive(Debug)]
26-
pub struct BorrowckFlowState<'mir, 'tcx> {
27-
pub(crate) borrows: <Borrows<'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
28-
pub(crate) uninits: <MaybeUninitializedPlaces<'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
29-
pub(crate) ever_inits: <EverInitializedPlaces<'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
26+
pub struct BorrowckFlowState<'a, 'mir, 'tcx> {
27+
pub(crate) borrows: <Borrows<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
28+
pub(crate) uninits: <MaybeUninitializedPlaces<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
29+
pub(crate) ever_inits: <EverInitializedPlaces<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Domain,
3030
}
3131

32-
impl<'mir, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'mir, 'tcx> {
32+
impl<'a, 'mir, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'a, 'mir, 'tcx> {
3333
// All three analyses are forward, but we have to use just one here.
34-
type Direction = <Borrows<'mir, 'tcx> as AnalysisDomain<'tcx>>::Direction;
35-
type FlowState = BorrowckFlowState<'mir, 'tcx>;
34+
type Direction = <Borrows<'a, 'mir, 'tcx> as AnalysisDomain<'tcx>>::Direction;
35+
type FlowState = BorrowckFlowState<'a, 'mir, 'tcx>;
3636

3737
fn new_flow_state(&self, body: &mir::Body<'tcx>) -> Self::FlowState {
3838
BorrowckFlowState {
@@ -106,11 +106,11 @@ rustc_index::newtype_index! {
106106
/// `BorrowIndex`, and maps each such index to a `BorrowData`
107107
/// describing the borrow. These indexes are used for representing the
108108
/// borrows in compact bitvectors.
109-
pub struct Borrows<'mir, 'tcx> {
109+
pub struct Borrows<'a, 'mir, 'tcx> {
110110
tcx: TyCtxt<'tcx>,
111111
body: &'mir Body<'tcx>,
112112

113-
borrow_set: &'mir BorrowSet<'tcx>,
113+
borrow_set: &'a BorrowSet<'tcx>,
114114
borrows_out_of_scope_at_location: FxIndexMap<Location, Vec<BorrowIndex>>,
115115
}
116116

@@ -389,12 +389,12 @@ impl<'tcx> PoloniusOutOfScopePrecomputer<'_, 'tcx> {
389389
}
390390
}
391391

392-
impl<'mir, 'tcx> Borrows<'mir, 'tcx> {
392+
impl<'a, 'mir, 'tcx> Borrows<'a, 'mir, 'tcx> {
393393
pub fn new(
394394
tcx: TyCtxt<'tcx>,
395395
body: &'mir Body<'tcx>,
396-
regioncx: &'mir RegionInferenceContext<'tcx>,
397-
borrow_set: &'mir BorrowSet<'tcx>,
396+
regioncx: &RegionInferenceContext<'tcx>,
397+
borrow_set: &'a BorrowSet<'tcx>,
398398
) -> Self {
399399
let mut borrows_out_of_scope_at_location =
400400
calculate_borrows_out_of_scope_at_location(body, regioncx, borrow_set);
@@ -494,7 +494,7 @@ impl<'mir, 'tcx> Borrows<'mir, 'tcx> {
494494
}
495495
}
496496

497-
impl<'tcx> rustc_mir_dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
497+
impl<'tcx> rustc_mir_dataflow::AnalysisDomain<'tcx> for Borrows<'_, '_, 'tcx> {
498498
type Domain = BitSet<BorrowIndex>;
499499

500500
const NAME: &'static str = "borrows";
@@ -517,7 +517,7 @@ impl<'tcx> rustc_mir_dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
517517
/// region stops containing the CFG points reachable from the issuing location.
518518
/// - we also kill loans of conflicting places when overwriting a shared path: e.g. borrows of
519519
/// `a.b.c` when `a` is overwritten.
520-
impl<'tcx> rustc_mir_dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
520+
impl<'tcx> rustc_mir_dataflow::GenKillAnalysis<'tcx> for Borrows<'_, '_, 'tcx> {
521521
type Idx = BorrowIndex;
522522

523523
fn domain_size(&self, _: &mir::Body<'tcx>) -> usize {
@@ -617,8 +617,8 @@ impl<'tcx> rustc_mir_dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
617617
}
618618
}
619619

620-
impl DebugWithContext<Borrows<'_, '_>> for BorrowIndex {
621-
fn fmt_with(&self, ctxt: &Borrows<'_, '_>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
620+
impl DebugWithContext<Borrows<'_, '_, '_>> for BorrowIndex {
621+
fn fmt_with(&self, ctxt: &Borrows<'_, '_, '_>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
622622
write!(f, "{:?}", ctxt.location(*self))
623623
}
624624
}

0 commit comments

Comments
 (0)