Skip to content

Commit 9420731

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 61a8d6a commit 9420731

File tree

22 files changed

+257
-311
lines changed

22 files changed

+257
-311
lines changed

compiler/rustc_attr/src/builtin.rs

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,71 +1270,83 @@ impl DiagCtxt {
12701270
self.inner.borrow_mut().emit_diagnostic_without_consuming(diagnostic)
12711271
}
12721272

1273+
#[track_caller]
12731274
pub fn emit_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> ErrorGuaranteed {
12741275
self.create_err(err).emit()
12751276
}
12761277

1278+
#[track_caller]
12771279
pub fn create_err<'a>(
12781280
&'a self,
12791281
err: impl IntoDiagnostic<'a>,
12801282
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
12811283
err.into_diagnostic(self)
12821284
}
12831285

1286+
#[track_caller]
12841287
pub fn create_warning<'a>(
12851288
&'a self,
12861289
warning: impl IntoDiagnostic<'a, ()>,
12871290
) -> DiagnosticBuilder<'a, ()> {
12881291
warning.into_diagnostic(self)
12891292
}
12901293

1294+
#[track_caller]
12911295
pub fn emit_warning<'a>(&'a self, warning: impl IntoDiagnostic<'a, ()>) {
12921296
self.create_warning(warning).emit()
12931297
}
12941298

1299+
#[track_caller]
12951300
pub fn create_almost_fatal<'a>(
12961301
&'a self,
12971302
fatal: impl IntoDiagnostic<'a, FatalError>,
12981303
) -> DiagnosticBuilder<'a, FatalError> {
12991304
fatal.into_diagnostic(self)
13001305
}
13011306

1307+
#[track_caller]
13021308
pub fn emit_almost_fatal<'a>(
13031309
&'a self,
13041310
fatal: impl IntoDiagnostic<'a, FatalError>,
13051311
) -> FatalError {
13061312
self.create_almost_fatal(fatal).emit()
13071313
}
13081314

1315+
#[track_caller]
13091316
pub fn create_fatal<'a>(
13101317
&'a self,
13111318
fatal: impl IntoDiagnostic<'a, !>,
13121319
) -> DiagnosticBuilder<'a, !> {
13131320
fatal.into_diagnostic(self)
13141321
}
13151322

1323+
#[track_caller]
13161324
pub fn emit_fatal<'a>(&'a self, fatal: impl IntoDiagnostic<'a, !>) -> ! {
13171325
self.create_fatal(fatal).emit()
13181326
}
13191327

1328+
#[track_caller]
13201329
pub fn create_bug<'a>(
13211330
&'a self,
13221331
bug: impl IntoDiagnostic<'a, diagnostic_builder::Bug>,
13231332
) -> DiagnosticBuilder<'a, diagnostic_builder::Bug> {
13241333
bug.into_diagnostic(self)
13251334
}
13261335

1336+
#[track_caller]
13271337
pub fn emit_bug<'a>(
13281338
&'a self,
13291339
bug: impl IntoDiagnostic<'a, diagnostic_builder::Bug>,
13301340
) -> diagnostic_builder::Bug {
13311341
self.create_bug(bug).emit()
13321342
}
13331343

1344+
#[track_caller]
13341345
pub fn emit_note<'a>(&'a self, note: impl IntoDiagnostic<'a, Noted>) -> Noted {
13351346
self.create_note(note).emit()
13361347
}
13371348

1349+
#[track_caller]
13381350
pub fn create_note<'a>(
13391351
&'a self,
13401352
note: impl IntoDiagnostic<'a, Noted>,

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ impl<'a> ExtCtxt<'a> {
11171117

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

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ pub fn ensure_complete_parse<'a>(
10831083

10841084
let expands_to_match_arm = kind_name == "pattern" && parser.token == token::FatArrow;
10851085

1086-
parser.sess.emit_err(IncompleteParse {
1086+
parser.dcx().emit_err(IncompleteParse {
10871087
span: def_site_span,
10881088
token,
10891089
label_span: span,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
9090
},
9191
None => ExplicitLifetimeRequired::WithParamType { span, named, new_ty_span, new_ty },
9292
};
93-
Some(self.tcx().sess.parse_sess.create_err(err))
93+
Some(self.tcx().sess.dcx().create_err(err))
9494
}
9595
}

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast::{self, AttrStyle};
77
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;
10-
use rustc_errors::{error_code, Applicability, Diagnostic, DiagnosticBuilder, StashKey};
10+
use rustc_errors::{error_code, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder, StashKey};
1111
use rustc_lexer::unescape::{self, EscapeError, Mode};
1212
use rustc_lexer::{Base, DocStyle, RawStrError};
1313
use rustc_lexer::{Cursor, LiteralKind};
@@ -108,6 +108,10 @@ struct StringReader<'a> {
108108
}
109109

110110
impl<'a> StringReader<'a> {
111+
pub fn dcx(&self) -> &'a DiagCtxt {
112+
&self.sess.dcx
113+
}
114+
111115
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
112116
self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
113117
}
@@ -174,7 +178,7 @@ impl<'a> StringReader<'a> {
174178
let span = self.mk_sp(start, self.pos);
175179
self.sess.symbol_gallery.insert(sym, span);
176180
if !sym.can_be_raw() {
177-
self.sess.emit_err(errors::CannotBeRawIdent { span, ident: sym });
181+
self.dcx().emit_err(errors::CannotBeRawIdent { span, ident: sym });
178182
}
179183
self.sess.raw_identifier_spans.push(span);
180184
token::Ident(sym, true)
@@ -245,7 +249,7 @@ impl<'a> StringReader<'a> {
245249
let lifetime_name = self.str_from(start);
246250
if starts_with_number {
247251
let span = self.mk_sp(start, self.pos);
248-
let mut diag = self.sess.struct_err("lifetimes cannot start with a number");
252+
let mut diag = self.dcx().struct_err("lifetimes cannot start with a number");
249253
diag.set_span(span);
250254
diag.stash(span, StashKey::LifetimeIsChar);
251255
}
@@ -306,7 +310,7 @@ impl<'a> StringReader<'a> {
306310
// fancier error recovery to it, as there will be less overall work to do this
307311
// way.
308312
let (token, sugg) = unicode_chars::check_for_substitution(self, start, c, repeats+1);
309-
self.sess.emit_err(errors::UnknownTokenStart {
313+
self.dcx().emit_err(errors::UnknownTokenStart {
310314
span: self.mk_sp(start, self.pos + Pos::from_usize(repeats * c.len_utf8())),
311315
escaped: escaped_char(c),
312316
sugg,
@@ -382,7 +386,7 @@ impl<'a> StringReader<'a> {
382386
content_start + BytePos(idx as u32 + 1),
383387
);
384388
let block = matches!(comment_kind, CommentKind::Block);
385-
self.sess.emit_err(errors::CrDocComment { span, block });
389+
self.dcx().emit_err(errors::CrDocComment { span, block });
386390
}
387391
}
388392

@@ -481,7 +485,7 @@ impl<'a> StringReader<'a> {
481485
rustc_lexer::LiteralKind::Int { base, empty_int } => {
482486
if empty_int {
483487
let span = self.mk_sp(start, end);
484-
self.sess.emit_err(errors::NoDigitsLiteral { span });
488+
self.dcx().emit_err(errors::NoDigitsLiteral { span });
485489
(token::Integer, sym::integer(0))
486490
} else {
487491
if matches!(base, Base::Binary | Base::Octal) {
@@ -493,7 +497,7 @@ impl<'a> StringReader<'a> {
493497
start + BytePos::from_usize(2 + idx + c.len_utf8()),
494498
);
495499
if c != '_' && c.to_digit(base).is_none() {
496-
self.sess.emit_err(errors::InvalidDigitLiteral { span, base });
500+
self.dcx().emit_err(errors::InvalidDigitLiteral { span, base });
497501
}
498502
}
499503
}
@@ -503,7 +507,7 @@ impl<'a> StringReader<'a> {
503507
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
504508
if empty_exponent {
505509
let span = self.mk_sp(start, self.pos);
506-
self.sess.emit_err(errors::EmptyExponentFloat { span });
510+
self.dcx().emit_err(errors::EmptyExponentFloat { span });
507511
}
508512
let base = match base {
509513
Base::Hexadecimal => Some("hexadecimal"),
@@ -513,7 +517,7 @@ impl<'a> StringReader<'a> {
513517
};
514518
if let Some(base) = base {
515519
let span = self.mk_sp(start, end);
516-
self.sess.emit_err(errors::FloatLiteralUnsupportedBase { span, base });
520+
self.dcx().emit_err(errors::FloatLiteralUnsupportedBase { span, base });
517521
}
518522
(token::Float, self.symbol_from_to(start, end))
519523
}
@@ -676,7 +680,7 @@ impl<'a> StringReader<'a> {
676680
} else {
677681
None
678682
};
679-
self.sess.emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
683+
self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
680684
} else {
681685
// Before Rust 2021, only emit a lint for migration.
682686
self.sess.buffer_lint_with_diagnostic(
@@ -690,7 +694,7 @@ impl<'a> StringReader<'a> {
690694
}
691695

692696
fn report_too_many_hashes(&self, start: BytePos, num: u32) -> ! {
693-
self.sess.emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
697+
self.dcx().emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
694698
}
695699

696700
fn cook_common(

compiler/rustc_parse/src/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)