Skip to content

Commit dd104ef

Browse files
committed
Auto merge of #126623 - oli-obk:do_not_count_errors, r=davidtwco
Replace all `&DiagCtxt` with a `DiagCtxtHandle<'_>` wrapper type r? `@davidtwco` This paves the way for tracking more state (e.g. error tainting) in the diagnostic context handle Basically I will add a field to the `DiagCtxtHandle` that refers back to the `InferCtxt`'s (and others) `Option<ErrorHandled>`, allowing us to immediately taint these contexts when emitting an error and not needing manual tainting anymore (which is easy to forget and we don't do in general anyway)
2 parents 8814b92 + 3f34196 commit dd104ef

File tree

118 files changed

+773
-915
lines changed

Some content is hidden

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

118 files changed

+773
-915
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc_data_structures::fx::FxIndexSet;
5050
use rustc_data_structures::sorted_map::SortedMap;
5151
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5252
use rustc_data_structures::sync::Lrc;
53-
use rustc_errors::{DiagArgFromDisplay, DiagCtxt, StashKey};
53+
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5454
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5555
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
5656
use rustc_hir::{self as hir};
@@ -188,7 +188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
188188
}
189189
}
190190

191-
pub(crate) fn dcx(&self) -> &'hir DiagCtxt {
191+
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'hir> {
192192
self.tcx.dcx()
193193
}
194194
}

compiler/rustc_ast_passes/src/ast_validation.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_ast::visit::{walk_list, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor}
1212
use rustc_ast::*;
1313
use rustc_ast_pretty::pprust::{self, State};
1414
use rustc_data_structures::fx::FxIndexMap;
15+
use rustc_errors::DiagCtxtHandle;
1516
use rustc_feature::Features;
1617
use rustc_parse::validate_attr;
1718
use rustc_session::lint::builtin::{
@@ -269,7 +270,7 @@ impl<'a> AstValidator<'a> {
269270
}
270271
}
271272

272-
fn dcx(&self) -> &rustc_errors::DiagCtxt {
273+
fn dcx(&self) -> DiagCtxtHandle<'a> {
273274
self.session.dcx()
274275
}
275276

@@ -809,11 +810,7 @@ impl<'a> AstValidator<'a> {
809810

810811
/// Checks that generic parameters are in the correct order,
811812
/// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`)
812-
fn validate_generic_param_order(
813-
dcx: &rustc_errors::DiagCtxt,
814-
generics: &[GenericParam],
815-
span: Span,
816-
) {
813+
fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericParam], span: Span) {
817814
let mut max_param: Option<ParamKindOrd> = None;
818815
let mut out_of_order = FxIndexMap::default();
819816
let mut param_idents = Vec::with_capacity(generics.len());

compiler/rustc_ast_passes/src/show_span.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::str::FromStr;
88
use rustc_ast as ast;
99
use rustc_ast::visit;
1010
use rustc_ast::visit::Visitor;
11+
use rustc_errors::DiagCtxtHandle;
1112

1213
use crate::errors;
1314

@@ -31,7 +32,7 @@ impl FromStr for Mode {
3132
}
3233

3334
struct ShowSpanVisitor<'a> {
34-
dcx: &'a rustc_errors::DiagCtxt,
35+
dcx: DiagCtxtHandle<'a>,
3536
mode: Mode,
3637
}
3738

@@ -58,7 +59,7 @@ impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
5859
}
5960
}
6061

61-
pub fn run(dcx: &rustc_errors::DiagCtxt, mode: &str, krate: &ast::Crate) {
62+
pub fn run(dcx: DiagCtxtHandle<'_>, mode: &str, krate: &ast::Crate) {
6263
let Ok(mode) = mode.parse() else {
6364
return;
6465
};

compiler/rustc_attr/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ pub fn eval_condition(
596596
features: Option<&Features>,
597597
eval: &mut impl FnMut(Condition) -> bool,
598598
) -> bool {
599-
let dcx = &sess.psess.dcx;
599+
let dcx = sess.dcx();
600600
match &cfg.kind {
601601
ast::MetaItemKind::List(mis) if cfg.name_or_empty() == sym::version => {
602602
try_gate_cfg(sym::version, cfg.span, sess, features);

compiler/rustc_attr/src/session_diagnostics.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::num::IntErrorKind;
22

33
use rustc_ast as ast;
4-
use rustc_errors::{codes::*, Applicability, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level};
4+
use rustc_errors::DiagCtxtHandle;
5+
use rustc_errors::{codes::*, Applicability, Diag, Diagnostic, EmissionGuarantee, Level};
56
use rustc_macros::{Diagnostic, Subdiagnostic};
67
use rustc_span::{Span, Symbol};
78

@@ -49,7 +50,7 @@ pub(crate) struct UnknownMetaItem<'a> {
4950

5051
// Manual implementation to be able to format `expected` items correctly.
5152
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnknownMetaItem<'_> {
52-
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
53+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
5354
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
5455
Diag::new(dcx, level, fluent::attr_unknown_meta_item)
5556
.with_span(self.span)
@@ -202,7 +203,7 @@ pub(crate) struct UnsupportedLiteral {
202203
}
203204

204205
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for UnsupportedLiteral {
205-
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
206+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
206207
let mut diag = Diag::new(
207208
dcx,
208209
level,

compiler/rustc_borrowck/src/borrowck_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#![allow(rustc::diagnostic_outside_of_impl)]
22
#![allow(rustc::untranslatable_diagnostic)]
33

4-
use rustc_errors::{codes::*, struct_span_code_err, Diag, DiagCtxt};
4+
use rustc_errors::{codes::*, struct_span_code_err, Diag, DiagCtxtHandle};
55
use rustc_middle::span_bug;
66
use rustc_middle::ty::{self, Ty, TyCtxt};
77
use rustc_span::Span;
88

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

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+58-88
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
228228
seen_spans.insert(move_span);
229229
}
230230

231-
use_spans.var_path_only_subdiag(self.dcx(), &mut err, desired_action);
231+
use_spans.var_path_only_subdiag(&mut err, desired_action);
232232

233233
if !is_loop_move {
234234
err.span_label(
@@ -303,24 +303,18 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
303303
if needs_note {
304304
if let Some(local) = place.as_local() {
305305
let span = self.body.local_decls[local].source_info.span;
306-
err.subdiagnostic(
307-
self.dcx(),
308-
crate::session_diagnostics::TypeNoCopy::Label {
309-
is_partial_move,
310-
ty,
311-
place: &note_msg,
312-
span,
313-
},
314-
);
306+
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
307+
is_partial_move,
308+
ty,
309+
place: &note_msg,
310+
span,
311+
});
315312
} else {
316-
err.subdiagnostic(
317-
self.dcx(),
318-
crate::session_diagnostics::TypeNoCopy::Note {
319-
is_partial_move,
320-
ty,
321-
place: &note_msg,
322-
},
323-
);
313+
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Note {
314+
is_partial_move,
315+
ty,
316+
place: &note_msg,
317+
});
324318
};
325319
}
326320

@@ -597,7 +591,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
597591
E0381,
598592
"{used} binding {desc}{isnt_initialized}"
599593
);
600-
use_spans.var_path_only_subdiag(self.dcx(), &mut err, desired_action);
594+
use_spans.var_path_only_subdiag(&mut err, desired_action);
601595

602596
if let InitializationRequiringAction::PartialAssignment
603597
| InitializationRequiringAction::Assignment = desired_action
@@ -996,7 +990,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
996990
&self,
997991
err: &mut Diag<'_>,
998992
ty: Ty<'tcx>,
999-
expr: &'cx hir::Expr<'cx>,
993+
expr: &hir::Expr<'_>,
1000994
) {
1001995
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
1002996
let hir::ExprKind::Struct(struct_qpath, fields, Some(base)) = expr.kind else { return };
@@ -1084,8 +1078,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10841078
&self,
10851079
err: &mut Diag<'_>,
10861080
ty: Ty<'tcx>,
1087-
mut expr: &'cx hir::Expr<'cx>,
1088-
mut other_expr: Option<&'cx hir::Expr<'cx>>,
1081+
mut expr: &'tcx hir::Expr<'tcx>,
1082+
mut other_expr: Option<&'tcx hir::Expr<'tcx>>,
10891083
use_spans: Option<UseSpans<'tcx>>,
10901084
) {
10911085
if let hir::ExprKind::Struct(_, _, Some(_)) = expr.kind {
@@ -1410,13 +1404,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14101404
&value_msg,
14111405
);
14121406

1413-
borrow_spans.var_path_only_subdiag(
1414-
self.dcx(),
1415-
&mut err,
1416-
crate::InitializationRequiringAction::Borrow,
1417-
);
1407+
borrow_spans.var_path_only_subdiag(&mut err, crate::InitializationRequiringAction::Borrow);
14181408

1419-
move_spans.var_subdiag(self.dcx(), &mut err, None, |kind, var_span| {
1409+
move_spans.var_subdiag(&mut err, None, |kind, var_span| {
14201410
use crate::session_diagnostics::CaptureVarCause::*;
14211411
match kind {
14221412
hir::ClosureKind::Coroutine(_) => MoveUseInCoroutine { var_span },
@@ -1468,7 +1458,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14681458
borrow_span,
14691459
&self.describe_any_place(borrow.borrowed_place.as_ref()),
14701460
);
1471-
borrow_spans.var_subdiag(self.dcx(), &mut err, Some(borrow.kind), |kind, var_span| {
1461+
borrow_spans.var_subdiag(&mut err, Some(borrow.kind), |kind, var_span| {
14721462
use crate::session_diagnostics::CaptureVarCause::*;
14731463
let place = &borrow.borrowed_place;
14741464
let desc_place = self.describe_any_place(place.as_ref());
@@ -1633,7 +1623,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16331623
"mutably borrow",
16341624
);
16351625
borrow_spans.var_subdiag(
1636-
self.dcx(),
16371626
&mut err,
16381627
Some(BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture }),
16391628
|kind, var_span| {
@@ -1730,64 +1719,45 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17301719
};
17311720

17321721
if issued_spans == borrow_spans {
1733-
borrow_spans.var_subdiag(
1734-
self.dcx(),
1735-
&mut err,
1736-
Some(gen_borrow_kind),
1737-
|kind, var_span| {
1738-
use crate::session_diagnostics::CaptureVarCause::*;
1739-
match kind {
1740-
hir::ClosureKind::Coroutine(_) => BorrowUsePlaceCoroutine {
1741-
place: desc_place,
1742-
var_span,
1743-
is_single_var: false,
1744-
},
1745-
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1746-
BorrowUsePlaceClosure {
1747-
place: desc_place,
1748-
var_span,
1749-
is_single_var: false,
1750-
}
1751-
}
1722+
borrow_spans.var_subdiag(&mut err, Some(gen_borrow_kind), |kind, var_span| {
1723+
use crate::session_diagnostics::CaptureVarCause::*;
1724+
match kind {
1725+
hir::ClosureKind::Coroutine(_) => BorrowUsePlaceCoroutine {
1726+
place: desc_place,
1727+
var_span,
1728+
is_single_var: false,
1729+
},
1730+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1731+
BorrowUsePlaceClosure { place: desc_place, var_span, is_single_var: false }
17521732
}
1753-
},
1754-
);
1733+
}
1734+
});
17551735
} else {
1756-
issued_spans.var_subdiag(
1757-
self.dcx(),
1758-
&mut err,
1759-
Some(issued_borrow.kind),
1760-
|kind, var_span| {
1761-
use crate::session_diagnostics::CaptureVarCause::*;
1762-
let borrow_place = &issued_borrow.borrowed_place;
1763-
let borrow_place_desc = self.describe_any_place(borrow_place.as_ref());
1764-
match kind {
1765-
hir::ClosureKind::Coroutine(_) => {
1766-
FirstBorrowUsePlaceCoroutine { place: borrow_place_desc, var_span }
1767-
}
1768-
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1769-
FirstBorrowUsePlaceClosure { place: borrow_place_desc, var_span }
1770-
}
1736+
issued_spans.var_subdiag(&mut err, Some(issued_borrow.kind), |kind, var_span| {
1737+
use crate::session_diagnostics::CaptureVarCause::*;
1738+
let borrow_place = &issued_borrow.borrowed_place;
1739+
let borrow_place_desc = self.describe_any_place(borrow_place.as_ref());
1740+
match kind {
1741+
hir::ClosureKind::Coroutine(_) => {
1742+
FirstBorrowUsePlaceCoroutine { place: borrow_place_desc, var_span }
17711743
}
1772-
},
1773-
);
1744+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1745+
FirstBorrowUsePlaceClosure { place: borrow_place_desc, var_span }
1746+
}
1747+
}
1748+
});
17741749

1775-
borrow_spans.var_subdiag(
1776-
self.dcx(),
1777-
&mut err,
1778-
Some(gen_borrow_kind),
1779-
|kind, var_span| {
1780-
use crate::session_diagnostics::CaptureVarCause::*;
1781-
match kind {
1782-
hir::ClosureKind::Coroutine(_) => {
1783-
SecondBorrowUsePlaceCoroutine { place: desc_place, var_span }
1784-
}
1785-
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1786-
SecondBorrowUsePlaceClosure { place: desc_place, var_span }
1787-
}
1750+
borrow_spans.var_subdiag(&mut err, Some(gen_borrow_kind), |kind, var_span| {
1751+
use crate::session_diagnostics::CaptureVarCause::*;
1752+
match kind {
1753+
hir::ClosureKind::Coroutine(_) => {
1754+
SecondBorrowUsePlaceCoroutine { place: desc_place, var_span }
17881755
}
1789-
},
1790-
);
1756+
hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1757+
SecondBorrowUsePlaceClosure { place: desc_place, var_span }
1758+
}
1759+
}
1760+
});
17911761
}
17921762

17931763
if union_type_name != "" {
@@ -2016,7 +1986,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20161986
);
20171987
}
20181988

2019-
pub(crate) fn find_expr(&self, span: Span) -> Option<&hir::Expr<'_>> {
1989+
pub(crate) fn find_expr(&self, span: Span) -> Option<&'tcx hir::Expr<'tcx>> {
20201990
let tcx = self.infcx.tcx;
20211991
let body_id = tcx.hir_node(self.mir_hir_id()).body_id()?;
20221992
let mut expr_finder = FindExprBySpan::new(span, tcx);
@@ -2961,7 +2931,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
29612931
err.span_label(borrow_span, "borrowed value does not live long enough");
29622932
err.span_label(drop_span, format!("`{name}` dropped here while still borrowed"));
29632933

2964-
borrow_spans.args_subdiag(self.dcx(), &mut err, |args_span| {
2934+
borrow_spans.args_subdiag(&mut err, |args_span| {
29652935
crate::session_diagnostics::CaptureArgLabel::Capture {
29662936
is_within: borrow_spans.for_coroutine(),
29672937
args_span,
@@ -3219,7 +3189,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
32193189
None,
32203190
);
32213191

3222-
borrow_spans.args_subdiag(self.dcx(), &mut err, |args_span| {
3192+
borrow_spans.args_subdiag(&mut err, |args_span| {
32233193
crate::session_diagnostics::CaptureArgLabel::Capture {
32243194
is_within: borrow_spans.for_coroutine(),
32253195
args_span,
@@ -3680,7 +3650,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
36803650
"assign",
36813651
);
36823652

3683-
loan_spans.var_subdiag(self.dcx(), &mut err, Some(loan.kind), |kind, var_span| {
3653+
loan_spans.var_subdiag(&mut err, Some(loan.kind), |kind, var_span| {
36843654
use crate::session_diagnostics::CaptureVarCause::*;
36853655
match kind {
36863656
hir::ClosureKind::Coroutine(_) => BorrowUseInCoroutine { var_span },
@@ -3698,7 +3668,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
36983668

36993669
let mut err = self.cannot_assign_to_borrowed(span, loan_span, &descr_place);
37003670

3701-
loan_spans.var_subdiag(self.dcx(), &mut err, Some(loan.kind), |kind, var_span| {
3671+
loan_spans.var_subdiag(&mut err, Some(loan.kind), |kind, var_span| {
37023672
use crate::session_diagnostics::CaptureVarCause::*;
37033673
match kind {
37043674
hir::ClosureKind::Coroutine(_) => BorrowUseInCoroutine { var_span },

0 commit comments

Comments
 (0)