Skip to content

Commit d51db05

Browse files
committed
Remove ParseSess methods that duplicate DiagCtxt methods.
Also add missing `#[track_caller]` attributes to `DiagCtxt` methods as necessary to keep tests working.
1 parent ec9af0d commit d51db05

File tree

22 files changed

+256
-308
lines changed

22 files changed

+256
-308
lines changed

compiler/rustc_attr/src/builtin.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ pub fn eval_condition(
588588
features: Option<&Features>,
589589
eval: &mut impl FnMut(Condition) -> bool,
590590
) -> bool {
591+
let dcx = &sess.dcx;
591592
match &cfg.kind {
592593
ast::MetaItemKind::List(mis) if cfg.name_or_empty() == sym::version => {
593594
try_gate_cfg(sym::version, cfg.span, sess, features);
@@ -599,18 +600,18 @@ pub fn eval_condition(
599600
NestedMetaItem::Lit(MetaItemLit { span, .. })
600601
| NestedMetaItem::MetaItem(MetaItem { span, .. }),
601602
] => {
602-
sess.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span });
603+
dcx.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span });
603604
return false;
604605
}
605606
[..] => {
606-
sess.emit_err(session_diagnostics::ExpectedSingleVersionLiteral {
607+
dcx.emit_err(session_diagnostics::ExpectedSingleVersionLiteral {
607608
span: cfg.span,
608609
});
609610
return false;
610611
}
611612
};
612613
let Some(min_version) = parse_version(*min_version) else {
613-
sess.emit_warning(session_diagnostics::UnknownVersionLiteral { span: *span });
614+
dcx.emit_warning(session_diagnostics::UnknownVersionLiteral { span: *span });
614615
return false;
615616
};
616617

@@ -624,7 +625,7 @@ pub fn eval_condition(
624625
ast::MetaItemKind::List(mis) => {
625626
for mi in mis.iter() {
626627
if !mi.is_meta_item() {
627-
sess.emit_err(session_diagnostics::UnsupportedLiteral {
628+
dcx.emit_err(session_diagnostics::UnsupportedLiteral {
628629
span: mi.span(),
629630
reason: UnsupportedLiteralReason::Generic,
630631
is_bytestr: false,
@@ -653,9 +654,7 @@ pub fn eval_condition(
653654
}),
654655
sym::not => {
655656
if mis.len() != 1 {
656-
sess.emit_err(session_diagnostics::ExpectedOneCfgPattern {
657-
span: cfg.span,
658-
});
657+
dcx.emit_err(session_diagnostics::ExpectedOneCfgPattern { span: cfg.span });
659658
return false;
660659
}
661660

@@ -684,7 +683,7 @@ pub fn eval_condition(
684683
})
685684
}
686685
_ => {
687-
sess.emit_err(session_diagnostics::InvalidPredicate {
686+
dcx.emit_err(session_diagnostics::InvalidPredicate {
688687
span: cfg.span,
689688
predicate: pprust::path_to_string(&cfg.path),
690689
});
@@ -693,11 +692,11 @@ pub fn eval_condition(
693692
}
694693
}
695694
ast::MetaItemKind::Word | MetaItemKind::NameValue(..) if cfg.path.segments.len() != 1 => {
696-
sess.emit_err(session_diagnostics::CfgPredicateIdentifier { span: cfg.path.span });
695+
dcx.emit_err(session_diagnostics::CfgPredicateIdentifier { span: cfg.path.span });
697696
true
698697
}
699698
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
700-
sess.emit_err(session_diagnostics::UnsupportedLiteral {
699+
dcx.emit_err(session_diagnostics::UnsupportedLiteral {
701700
span: lit.span,
702701
reason: UnsupportedLiteralReason::CfgString,
703702
is_bytestr: lit.kind.is_bytestr(),
@@ -945,7 +944,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
945944
assert!(attr.has_name(sym::repr), "expected `#[repr(..)]`, found: {attr:?}");
946945
use ReprAttr::*;
947946
let mut acc = Vec::new();
948-
let diagnostic = sess.dcx();
947+
let dcx = sess.dcx();
949948

950949
if let Some(items) = attr.meta_item_list() {
951950
for item in items {
@@ -1062,7 +1061,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10621061
// (e.g. if we only pretty-print the source), so we have to gate
10631062
// the `span_delayed_bug` call as follows:
10641063
if sess.opts.pretty.map_or(true, |pp| pp.needs_analysis()) {
1065-
diagnostic.span_delayed_bug(item.span(), "unrecognized representation hint");
1064+
dcx.span_delayed_bug(item.span(), "unrecognized representation hint");
10661065
}
10671066
}
10681067
}

compiler/rustc_builtin_macros/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ fn parse_reg<'a>(
422422
ast::InlineAsmRegOrRegClass::Reg(symbol)
423423
}
424424
_ => {
425-
return Err(p.sess.create_err(errors::ExpectedRegisterClassOrExplicitRegister {
425+
return Err(p.dcx().create_err(errors::ExpectedRegisterClassOrExplicitRegister {
426426
span: p.token.span,
427427
}));
428428
}

compiler/rustc_errors/src/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ impl DiagCtxt {
12031203
self.inner.borrow_mut().emit_diagnostic_without_consuming(diagnostic)
12041204
}
12051205

1206+
#[track_caller]
12061207
pub fn emit_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> ErrorGuaranteed {
12071208
self.create_err(err).emit()
12081209
}
@@ -1212,57 +1213,67 @@ impl DiagCtxt {
12121213
err.into_diagnostic(self, Error { lint: false })
12131214
}
12141215

1216+
#[track_caller]
12151217
pub fn create_warning<'a>(
12161218
&'a self,
12171219
warning: impl IntoDiagnostic<'a, ()>,
12181220
) -> DiagnosticBuilder<'a, ()> {
12191221
warning.into_diagnostic(self, Warning(None))
12201222
}
12211223

1224+
#[track_caller]
12221225
pub fn emit_warning<'a>(&'a self, warning: impl IntoDiagnostic<'a, ()>) {
12231226
self.create_warning(warning).emit()
12241227
}
12251228

1229+
#[track_caller]
12261230
pub fn create_almost_fatal<'a>(
12271231
&'a self,
12281232
fatal: impl IntoDiagnostic<'a, FatalError>,
12291233
) -> DiagnosticBuilder<'a, FatalError> {
12301234
fatal.into_diagnostic(self, Fatal)
12311235
}
12321236

1237+
#[track_caller]
12331238
pub fn emit_almost_fatal<'a>(
12341239
&'a self,
12351240
fatal: impl IntoDiagnostic<'a, FatalError>,
12361241
) -> FatalError {
12371242
self.create_almost_fatal(fatal).emit()
12381243
}
12391244

1245+
#[track_caller]
12401246
pub fn create_fatal<'a>(
12411247
&'a self,
12421248
fatal: impl IntoDiagnostic<'a, FatalAbort>,
12431249
) -> DiagnosticBuilder<'a, FatalAbort> {
12441250
fatal.into_diagnostic(self, Fatal)
12451251
}
12461252

1253+
#[track_caller]
12471254
pub fn emit_fatal<'a>(&'a self, fatal: impl IntoDiagnostic<'a, FatalAbort>) -> ! {
12481255
self.create_fatal(fatal).emit()
12491256
}
12501257

1258+
#[track_caller]
12511259
pub fn create_bug<'a>(
12521260
&'a self,
12531261
bug: impl IntoDiagnostic<'a, BugAbort>,
12541262
) -> DiagnosticBuilder<'a, BugAbort> {
12551263
bug.into_diagnostic(self, Bug)
12561264
}
12571265

1258-
pub fn emit_bug<'a>(&'a self, bug: impl IntoDiagnostic<'a, BugAbort>) -> ! {
1266+
#[track_caller]
1267+
pub fn emit_bug<'a>(&'a self, bug: impl IntoDiagnostic<'a, diagnostic_builder::BugAbort>) -> ! {
12591268
self.create_bug(bug).emit()
12601269
}
12611270

1271+
#[track_caller]
12621272
pub fn emit_note<'a>(&'a self, note: impl IntoDiagnostic<'a, ()>) {
12631273
self.create_note(note).emit()
12641274
}
12651275

1276+
#[track_caller]
12661277
pub fn create_note<'a>(
12671278
&'a self,
12681279
note: impl IntoDiagnostic<'a, ()>,

compiler/rustc_expand/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ impl<'a> ExtCtxt<'a> {
11151115

11161116
pub fn trace_macros_diag(&mut self) {
11171117
for (span, notes) in self.expansions.iter() {
1118-
let mut db = self.sess.parse_sess.create_note(errors::TraceMacro { span: *span });
1118+
let mut db = self.dcx().create_note(errors::TraceMacro { span: *span });
11191119
for note in notes {
11201120
db.note(note.clone());
11211121
}

compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ pub fn ensure_complete_parse<'a>(
957957

958958
let expands_to_match_arm = kind_name == "pattern" && parser.token == token::FatArrow;
959959

960-
parser.sess.emit_err(IncompleteParse {
960+
parser.dcx().emit_err(IncompleteParse {
961961
span: def_site_span,
962962
token,
963963
label_span: span,

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
8888
},
8989
None => ExplicitLifetimeRequired::WithParamType { span, named, new_ty_span, new_ty },
9090
};
91-
Some(self.tcx().sess.parse_sess.create_err(err))
91+
Some(self.tcx().sess.dcx().create_err(err))
9292
}
9393
}

compiler/rustc_parse/src/lexer/mod.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast::util::unicode::contains_text_flow_control_chars;
1010
use rustc_errors::{
11-
error_code, Applicability, Diagnostic, DiagnosticBuilder, FatalAbort, StashKey,
11+
error_code, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder, FatalAbort, StashKey,
1212
};
1313
use rustc_lexer::unescape::{self, EscapeError, Mode};
1414
use rustc_lexer::{Base, DocStyle, RawStrError};
@@ -110,6 +110,10 @@ struct StringReader<'a> {
110110
}
111111

112112
impl<'a> StringReader<'a> {
113+
pub fn dcx(&self) -> &'a DiagCtxt {
114+
&self.sess.dcx
115+
}
116+
113117
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
114118
self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
115119
}
@@ -176,7 +180,7 @@ impl<'a> StringReader<'a> {
176180
let span = self.mk_sp(start, self.pos);
177181
self.sess.symbol_gallery.insert(sym, span);
178182
if !sym.can_be_raw() {
179-
self.sess.emit_err(errors::CannotBeRawIdent { span, ident: sym });
183+
self.dcx().emit_err(errors::CannotBeRawIdent { span, ident: sym });
180184
}
181185
self.sess.raw_identifier_spans.push(span);
182186
token::Ident(sym, true)
@@ -247,7 +251,7 @@ impl<'a> StringReader<'a> {
247251
let lifetime_name = self.str_from(start);
248252
if starts_with_number {
249253
let span = self.mk_sp(start, self.pos);
250-
let mut diag = self.sess.struct_err("lifetimes cannot start with a number");
254+
let mut diag = self.dcx().struct_err("lifetimes cannot start with a number");
251255
diag.set_span(span);
252256
diag.stash(span, StashKey::LifetimeIsChar);
253257
}
@@ -308,7 +312,7 @@ impl<'a> StringReader<'a> {
308312
// fancier error recovery to it, as there will be less overall work to do this
309313
// way.
310314
let (token, sugg) = unicode_chars::check_for_substitution(self, start, c, repeats+1);
311-
self.sess.emit_err(errors::UnknownTokenStart {
315+
self.dcx().emit_err(errors::UnknownTokenStart {
312316
span: self.mk_sp(start, self.pos + Pos::from_usize(repeats * c.len_utf8())),
313317
escaped: escaped_char(c),
314318
sugg,
@@ -384,7 +388,7 @@ impl<'a> StringReader<'a> {
384388
content_start + BytePos(idx as u32 + 1),
385389
);
386390
let block = matches!(comment_kind, CommentKind::Block);
387-
self.sess.emit_err(errors::CrDocComment { span, block });
391+
self.dcx().emit_err(errors::CrDocComment { span, block });
388392
}
389393
}
390394

@@ -483,7 +487,7 @@ impl<'a> StringReader<'a> {
483487
rustc_lexer::LiteralKind::Int { base, empty_int } => {
484488
if empty_int {
485489
let span = self.mk_sp(start, end);
486-
self.sess.emit_err(errors::NoDigitsLiteral { span });
490+
self.dcx().emit_err(errors::NoDigitsLiteral { span });
487491
(token::Integer, sym::integer(0))
488492
} else {
489493
if matches!(base, Base::Binary | Base::Octal) {
@@ -495,7 +499,7 @@ impl<'a> StringReader<'a> {
495499
start + BytePos::from_usize(2 + idx + c.len_utf8()),
496500
);
497501
if c != '_' && c.to_digit(base).is_none() {
498-
self.sess.emit_err(errors::InvalidDigitLiteral { span, base });
502+
self.dcx().emit_err(errors::InvalidDigitLiteral { span, base });
499503
}
500504
}
501505
}
@@ -505,7 +509,7 @@ impl<'a> StringReader<'a> {
505509
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
506510
if empty_exponent {
507511
let span = self.mk_sp(start, self.pos);
508-
self.sess.emit_err(errors::EmptyExponentFloat { span });
512+
self.dcx().emit_err(errors::EmptyExponentFloat { span });
509513
}
510514
let base = match base {
511515
Base::Hexadecimal => Some("hexadecimal"),
@@ -515,7 +519,7 @@ impl<'a> StringReader<'a> {
515519
};
516520
if let Some(base) = base {
517521
let span = self.mk_sp(start, end);
518-
self.sess.emit_err(errors::FloatLiteralUnsupportedBase { span, base });
522+
self.dcx().emit_err(errors::FloatLiteralUnsupportedBase { span, base });
519523
}
520524
(token::Float, self.symbol_from_to(start, end))
521525
}
@@ -678,7 +682,7 @@ impl<'a> StringReader<'a> {
678682
} else {
679683
None
680684
};
681-
self.sess.emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
685+
self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
682686
} else {
683687
// Before Rust 2021, only emit a lint for migration.
684688
self.sess.buffer_lint_with_diagnostic(
@@ -692,7 +696,7 @@ impl<'a> StringReader<'a> {
692696
}
693697

694698
fn report_too_many_hashes(&self, start: BytePos, num: u32) -> ! {
695-
self.sess.emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
699+
self.dcx().emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
696700
}
697701

698702
fn cook_common(

compiler/rustc_parse/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,5 @@ const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
263263
#the-cfg_attr-attribute>";
264264

265265
fn error_malformed_cfg_attr_missing(span: Span, parse_sess: &ParseSess) {
266-
parse_sess.emit_err(errors::MalformedCfgAttr { span, sugg: CFG_ATTR_GRAMMAR_HELP });
266+
parse_sess.dcx.emit_err(errors::MalformedCfgAttr { span, sugg: CFG_ATTR_GRAMMAR_HELP });
267267
}

compiler/rustc_parse/src/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'a> Parser<'a> {
323323
debug!("checking if {:?} is unsuffixed", lit);
324324

325325
if !lit.kind.is_unsuffixed() {
326-
self.sess.emit_err(SuffixedLiteralInAttribute { span: lit.span });
326+
self.dcx().emit_err(SuffixedLiteralInAttribute { span: lit.span });
327327
}
328328

329329
Ok(lit)

0 commit comments

Comments
 (0)