Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7ba82d6

Browse files
committed
Use a dedicated type instead of a reference for the diagnostic context
This paves the way for tracking more state (e.g. error tainting) in the diagnostic context handle
1 parent c91edc3 commit 7ba82d6

File tree

77 files changed

+363
-328
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

+363
-328
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 3 additions & 2 deletions
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/session_diagnostics.rs

Lines changed: 4 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::session_diagnostics::{
44
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
55
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
66
};
7-
use rustc_errors::{Applicability, Diag};
8-
use rustc_errors::{DiagCtxt, MultiSpan};
7+
use rustc_errors::MultiSpan;
8+
use rustc_errors::{Applicability, Diag, DiagCtxtHandle};
99
use rustc_hir::def::{CtorKind, Namespace};
1010
use rustc_hir::CoroutineKind;
1111
use rustc_hir::{self as hir, LangItem};
@@ -593,7 +593,7 @@ impl UseSpans<'_> {
593593
#[allow(rustc::diagnostic_outside_of_impl)]
594594
pub(super) fn args_subdiag(
595595
self,
596-
dcx: &DiagCtxt,
596+
dcx: DiagCtxtHandle<'_>,
597597
err: &mut Diag<'_>,
598598
f: impl FnOnce(Span) -> CaptureArgLabel,
599599
) {
@@ -607,7 +607,7 @@ impl UseSpans<'_> {
607607
#[allow(rustc::diagnostic_outside_of_impl)]
608608
pub(super) fn var_path_only_subdiag(
609609
self,
610-
dcx: &DiagCtxt,
610+
dcx: DiagCtxtHandle<'_>,
611611
err: &mut Diag<'_>,
612612
action: crate::InitializationRequiringAction,
613613
) {
@@ -645,7 +645,7 @@ impl UseSpans<'_> {
645645
#[allow(rustc::diagnostic_outside_of_impl)]
646646
pub(super) fn var_subdiag(
647647
self,
648-
dcx: &DiagCtxt,
648+
dcx: DiagCtxtHandle<'_>,
649649
err: &mut Diag<'_>,
650650
kind: Option<rustc_middle::mir::BorrowKind>,
651651
f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause,

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_errors::{
2-
codes::*, Diag, DiagCtxt, Diagnostic, EmissionGuarantee, Level, MultiSpan,
2+
codes::*, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan,
33
SingleLabelManySpans, SubdiagMessageOp, Subdiagnostic,
44
};
55
use rustc_macros::{Diagnostic, Subdiagnostic};
@@ -434,7 +434,7 @@ pub(crate) struct EnvNotDefinedWithUserMessage {
434434
// Hand-written implementation to support custom user messages.
435435
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessage {
436436
#[track_caller]
437-
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
437+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
438438
#[expect(
439439
rustc::untranslatable_diagnostic,
440440
reason = "cannot translate user-provided messages"
@@ -801,7 +801,7 @@ pub(crate) struct AsmClobberNoReg {
801801
}
802802

803803
impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AsmClobberNoReg {
804-
fn into_diag(self, dcx: &'a DiagCtxt, level: Level) -> Diag<'a, G> {
804+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
805805
// eager translation as `span_labels` takes `AsRef<str>`
806806
let lbl1 = dcx.eagerly_translate_to_string(
807807
crate::fluent_generated::builtin_macros_asm_clobber_abi,

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast::ptr::P;
33
use rustc_ast::visit::{self, Visitor};
44
use rustc_ast::{self as ast, attr, NodeId};
55
use rustc_ast_pretty::pprust;
6+
use rustc_errors::DiagCtxtHandle;
67
use rustc_expand::base::{parse_macro_name_and_helper_attrs, ExtCtxt, ResolverExpand};
78
use rustc_expand::expand::{AstFragment, ExpansionConfig};
89
use rustc_feature::Features;
@@ -38,7 +39,7 @@ enum ProcMacro {
3839
struct CollectProcMacros<'a> {
3940
macros: Vec<ProcMacro>,
4041
in_root: bool,
41-
dcx: &'a rustc_errors::DiagCtxt,
42+
dcx: DiagCtxtHandle<'a>,
4243
source_map: &'a SourceMap,
4344
is_proc_macro_crate: bool,
4445
is_test_crate: bool,
@@ -52,7 +53,7 @@ pub fn inject(
5253
is_proc_macro_crate: bool,
5354
has_proc_macro_decls: bool,
5455
is_test_crate: bool,
55-
dcx: &rustc_errors::DiagCtxt,
56+
dcx: DiagCtxtHandle<'_>,
5657
) {
5758
let ecfg = ExpansionConfig::default("proc_macro".to_string(), features);
5859
let mut cx = ExtCtxt::new(sess, ecfg, resolver, None);

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::mut_visit::*;
66
use rustc_ast::ptr::P;
77
use rustc_ast::visit::{walk_item, Visitor};
88
use rustc_ast::{attr, ModKind};
9+
use rustc_errors::DiagCtxtHandle;
910
use rustc_expand::base::{ExtCtxt, ResolverExpand};
1011
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1112
use rustc_feature::Features;
@@ -391,7 +392,7 @@ fn get_test_name(i: &ast::Item) -> Option<Symbol> {
391392
attr::first_attr_value_str_by_name(&i.attrs, sym::rustc_test_marker)
392393
}
393394

394-
fn get_test_runner(dcx: &rustc_errors::DiagCtxt, krate: &ast::Crate) -> Option<ast::Path> {
395+
fn get_test_runner(dcx: DiagCtxtHandle<'_>, krate: &ast::Crate) -> Option<ast::Path> {
395396
let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
396397
let meta_list = test_attr.meta_item_list()?;
397398
let span = test_attr.span;

compiler/rustc_codegen_cranelift/src/concurrency_limiter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::{Arc, Condvar, Mutex};
22

33
use jobserver::HelperThread;
4+
use rustc_errors::DiagCtxtHandle;
45
use rustc_session::Session;
56

67
// FIXME don't panic when a worker thread panics
@@ -46,7 +47,7 @@ impl ConcurrencyLimiter {
4647
}
4748
}
4849

49-
pub(super) fn acquire(&self, dcx: &rustc_errors::DiagCtxt) -> ConcurrencyLimiterToken {
50+
pub(super) fn acquire(&self, dcx: DiagCtxtHandle<'_>) -> ConcurrencyLimiterToken {
5051
let mut state = self.state.lock().unwrap();
5152
loop {
5253
state.assert_invariants();

0 commit comments

Comments
 (0)