Skip to content

Commit 4d830b7

Browse files
Rollup merge of #101434 - JhonnyBillM:replace-session-for-handler-in-into-diagnostic, r=davidtwco
Update `SessionDiagnostic::into_diagnostic` to take `Handler` instead of `ParseSess` Suggested by the team in [this Zulip Topic](https://rust-lang.zulipchat.com/#narrow/stream/336883-i18n/topic/.23100717.20SessionDiagnostic.20on.20Handler). `Handler` already has almost all the capabilities of `ParseSess` when it comes to diagnostic emission, in this migration we only needed to add the ability to access `source_map` from the emitter in order to get a `Snippet` and the `start_point`. Not sure if adding these two methods [`span_to_snippet_from_emitter` and `span_start_point_from_emitter`] is the best way to address this gap. P.S. If this goes in the right direction, then we probably may want to move `SessionDiagnostic` to `rustc_errors` and rename it to `DiagnosticHandler` or something similar. r? `@davidtwco` r? `@compiler-errors`
2 parents d13aefd + 46ba27d commit 4d830b7

File tree

16 files changed

+86
-75
lines changed

16 files changed

+86
-75
lines changed

compiler/rustc_attr/src/builtin.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
6363
sess.emit_err(session_diagnostics::MultipleStabilityLevels { span });
6464
}
6565
AttrError::UnsupportedLiteral(reason, is_bytestr) => {
66-
sess.emit_err(session_diagnostics::UnsupportedLiteral { span, reason, is_bytestr });
66+
sess.emit_err(session_diagnostics::UnsupportedLiteral {
67+
span,
68+
reason,
69+
is_bytestr,
70+
start_point_span: sess.source_map().start_point(span),
71+
});
6772
}
6873
}
6974
}

compiler/rustc_attr/src/session_diagnostics.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::num::IntErrorKind;
22

33
use rustc_ast as ast;
4-
use rustc_errors::{error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed};
4+
use rustc_errors::{
5+
error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler,
6+
};
57
use rustc_macros::SessionDiagnostic;
6-
use rustc_session::{parse::ParseSess, SessionDiagnostic};
8+
use rustc_session::SessionDiagnostic;
79
use rustc_span::{Span, Symbol};
810

911
use crate::UnsupportedLiteralReason;
@@ -49,9 +51,9 @@ pub(crate) struct UnknownMetaItem<'a> {
4951

5052
// Manual implementation to be able to format `expected` items correctly.
5153
impl<'a> SessionDiagnostic<'a> for UnknownMetaItem<'_> {
52-
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
54+
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
5355
let expected = self.expected.iter().map(|name| format!("`{}`", name)).collect::<Vec<_>>();
54-
let mut diag = sess.span_diagnostic.struct_span_err_with_code(
56+
let mut diag = handler.struct_span_err_with_code(
5557
self.span,
5658
fluent::attr::unknown_meta_item,
5759
error_code!(E0541),
@@ -204,11 +206,12 @@ pub(crate) struct UnsupportedLiteral {
204206
pub span: Span,
205207
pub reason: UnsupportedLiteralReason,
206208
pub is_bytestr: bool,
209+
pub start_point_span: Span,
207210
}
208211

209212
impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
210-
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
211-
let mut diag = sess.span_diagnostic.struct_span_err_with_code(
213+
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
214+
let mut diag = handler.struct_span_err_with_code(
212215
self.span,
213216
match self.reason {
214217
UnsupportedLiteralReason::Generic => fluent::attr::unsupported_literal_generic,
@@ -224,7 +227,7 @@ impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
224227
);
225228
if self.is_bytestr {
226229
diag.span_suggestion(
227-
sess.source_map().start_point(self.span),
230+
self.start_point_span,
228231
fluent::attr::unsupported_literal_suggestion,
229232
"",
230233
Applicability::MaybeIncorrect,

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
341341
multi_suggestions,
342342
bad_label,
343343
}
344-
.into_diagnostic(&self.tcx.sess.parse_sess),
344+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
345345
TypeAnnotationNeeded::E0283 => AmbigousImpl {
346346
span,
347347
source_kind,
@@ -351,7 +351,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
351351
multi_suggestions,
352352
bad_label,
353353
}
354-
.into_diagnostic(&self.tcx.sess.parse_sess),
354+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
355355
TypeAnnotationNeeded::E0284 => AmbigousReturn {
356356
span,
357357
source_kind,
@@ -361,7 +361,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
361361
multi_suggestions,
362362
bad_label,
363363
}
364-
.into_diagnostic(&self.tcx.sess.parse_sess),
364+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
365365
}
366366
}
367367

@@ -537,7 +537,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
537537
multi_suggestions,
538538
bad_label: None,
539539
}
540-
.into_diagnostic(&self.tcx.sess.parse_sess),
540+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
541541
TypeAnnotationNeeded::E0283 => AmbigousImpl {
542542
span,
543543
source_kind,
@@ -547,7 +547,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
547547
multi_suggestions,
548548
bad_label: None,
549549
}
550-
.into_diagnostic(&self.tcx.sess.parse_sess),
550+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
551551
TypeAnnotationNeeded::E0284 => AmbigousReturn {
552552
span,
553553
source_kind,
@@ -557,7 +557,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
557557
multi_suggestions,
558558
bad_label: None,
559559
}
560-
.into_diagnostic(&self.tcx.sess.parse_sess),
560+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
561561
}
562562
}
563563

@@ -575,7 +575,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
575575
span,
576576
generator_kind: GeneratorKindAsDiagArg(kind),
577577
}
578-
.into_diagnostic(&self.tcx.sess.parse_sess)
578+
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
579579
}
580580
}
581581

compiler/rustc_lint/src/errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed};
1+
use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed, Handler};
22
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3-
use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic};
3+
use rustc_session::{lint::Level, SessionDiagnostic};
44
use rustc_span::{Span, Symbol};
55

66
#[derive(SessionDiagnostic)]
@@ -122,9 +122,9 @@ pub struct CheckNameUnknown {
122122
impl SessionDiagnostic<'_> for CheckNameUnknown {
123123
fn into_diagnostic(
124124
self,
125-
sess: &ParseSess,
125+
handler: &Handler,
126126
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
127-
let mut diag = sess.struct_err(fluent::lint::check_name_unknown);
127+
let mut diag = handler.struct_err(fluent::lint::check_name_unknown);
128128
diag.code(rustc_errors::error_code!(E0602));
129129
if let Some(suggestion) = self.suggestion {
130130
diag.help(fluent::lint::help);

compiler/rustc_macros/src/diagnostics/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
8888
{
8989
fn into_diagnostic(
9090
self,
91-
#sess: &'__session_diagnostic_sess rustc_session::parse::ParseSess
91+
#sess: &'__session_diagnostic_sess rustc_errors::Handler
9292
) -> rustc_errors::DiagnosticBuilder<'__session_diagnostic_sess, G> {
9393
use rustc_errors::IntoDiagnosticArg;
9494
#implementation

compiler/rustc_metadata/src/errors.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ pub(crate) struct MultipleCandidates {
424424
impl SessionDiagnostic<'_> for MultipleCandidates {
425425
fn into_diagnostic(
426426
self,
427-
sess: &'_ rustc_session::parse::ParseSess,
427+
handler: &'_ rustc_errors::Handler,
428428
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
429-
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::multiple_candidates);
429+
let mut diag = handler.struct_err(rustc_errors::fluent::metadata::multiple_candidates);
430430
diag.set_arg("crate_name", self.crate_name);
431431
diag.set_arg("flavor", self.flavor);
432432
diag.code(error_code!(E0465));
@@ -540,9 +540,9 @@ pub struct InvalidMetadataFiles {
540540
impl SessionDiagnostic<'_> for InvalidMetadataFiles {
541541
fn into_diagnostic(
542542
self,
543-
sess: &'_ rustc_session::parse::ParseSess,
543+
handler: &'_ rustc_errors::Handler,
544544
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
545-
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::invalid_meta_files);
545+
let mut diag = handler.struct_err(rustc_errors::fluent::metadata::invalid_meta_files);
546546
diag.set_arg("crate_name", self.crate_name);
547547
diag.set_arg("add_info", self.add_info);
548548
diag.code(error_code!(E0786));
@@ -568,9 +568,9 @@ pub struct CannotFindCrate {
568568
impl SessionDiagnostic<'_> for CannotFindCrate {
569569
fn into_diagnostic(
570570
self,
571-
sess: &'_ rustc_session::parse::ParseSess,
571+
handler: &'_ rustc_errors::Handler,
572572
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
573-
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::cannot_find_crate);
573+
let mut diag = handler.struct_err(rustc_errors::fluent::metadata::cannot_find_crate);
574574
diag.set_arg("crate_name", self.crate_name);
575575
diag.set_arg("add_info", self.add_info);
576576
diag.set_arg("locator_triple", self.locator_triple.triple());

compiler/rustc_monomorphize/src/errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ pub struct UnusedGenericParams {
4747
impl SessionDiagnostic<'_> for UnusedGenericParams {
4848
fn into_diagnostic(
4949
self,
50-
sess: &'_ rustc_session::parse::ParseSess,
50+
handler: &'_ rustc_errors::Handler,
5151
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
52-
let mut diag = sess.struct_err(rustc_errors::fluent::monomorphize::unused_generic_params);
52+
let mut diag =
53+
handler.struct_err(rustc_errors::fluent::monomorphize::unused_generic_params);
5354
diag.set_span(self.span);
5455
for (span, name) in self.param_spans.into_iter().zip(self.param_names) {
5556
// FIXME: I can figure out how to do a label with a fluent string with a fixed message,

compiler/rustc_parse/src/parser/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ impl<'a> Parser<'a> {
19971997
return Err(MissingSemicolonBeforeArray {
19981998
open_delim: open_delim_span,
19991999
semicolon: prev_span.shrink_to_hi(),
2000-
}.into_diagnostic(self.sess));
2000+
}.into_diagnostic(&self.sess.span_diagnostic));
20012001
}
20022002
Ok(_) => (),
20032003
Err(err) => err.cancel(),
@@ -2745,7 +2745,8 @@ impl<'a> Parser<'a> {
27452745
fn parse_try_block(&mut self, span_lo: Span) -> PResult<'a, P<Expr>> {
27462746
let (attrs, body) = self.parse_inner_attrs_and_block()?;
27472747
if self.eat_keyword(kw::Catch) {
2748-
Err(CatchAfterTry { span: self.prev_token.span }.into_diagnostic(self.sess))
2748+
Err(CatchAfterTry { span: self.prev_token.span }
2749+
.into_diagnostic(&self.sess.span_diagnostic))
27492750
} else {
27502751
let span = span_lo.to(body.span);
27512752
self.sess.gated_spans.gate(sym::try_blocks, span);

compiler/rustc_query_system/src/query/job.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ pub(crate) fn report_cycle<'a>(
572572
stack_count,
573573
};
574574

575-
cycle_diag.into_diagnostic(&sess.parse_sess)
575+
cycle_diag.into_diagnostic(&sess.parse_sess.span_diagnostic)
576576
}
577577

578578
pub fn print_query_stack<CTX: QueryContext>(

compiler/rustc_session/src/parse.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl ParseSess {
343343
&'a self,
344344
err: impl SessionDiagnostic<'a>,
345345
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
346-
err.into_diagnostic(self)
346+
err.into_diagnostic(&self.span_diagnostic)
347347
}
348348

349349
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
@@ -354,7 +354,7 @@ impl ParseSess {
354354
&'a self,
355355
warning: impl SessionDiagnostic<'a, ()>,
356356
) -> DiagnosticBuilder<'a, ()> {
357-
warning.into_diagnostic(self)
357+
warning.into_diagnostic(&self.span_diagnostic)
358358
}
359359

360360
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
@@ -365,7 +365,7 @@ impl ParseSess {
365365
&'a self,
366366
fatal: impl SessionDiagnostic<'a, !>,
367367
) -> DiagnosticBuilder<'a, !> {
368-
fatal.into_diagnostic(self)
368+
fatal.into_diagnostic(&self.span_diagnostic)
369369
}
370370

371371
pub fn emit_fatal<'a>(&'a self, fatal: impl SessionDiagnostic<'a, !>) -> ! {

compiler/rustc_session/src/session.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_errors::json::JsonEmitter;
2121
use rustc_errors::registry::Registry;
2222
use rustc_errors::{
2323
error_code, fallback_fluent_bundle, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
24-
EmissionGuarantee, ErrorGuaranteed, FluentBundle, LazyFallbackBundle, MultiSpan,
24+
EmissionGuarantee, ErrorGuaranteed, FluentBundle, Handler, LazyFallbackBundle, MultiSpan,
2525
};
2626
use rustc_macros::HashStable_Generic;
2727
pub use rustc_span::def_id::StableCrateId;
@@ -220,9 +220,9 @@ pub struct PerfStats {
220220
/// `#[derive(SessionDiagnostic)]` -- see [rustc_macros::SessionDiagnostic].
221221
#[rustc_diagnostic_item = "SessionDiagnostic"]
222222
pub trait SessionDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> {
223-
/// Write out as a diagnostic out of `sess`.
223+
/// Write out as a diagnostic out of `Handler`.
224224
#[must_use]
225-
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, T>;
225+
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, T>;
226226
}
227227

228228
impl Session {

compiler/rustc_trait_selection/src/errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use rustc_errors::{fluent, ErrorGuaranteed};
1+
use rustc_errors::{fluent, ErrorGuaranteed, Handler};
22
use rustc_macros::SessionDiagnostic;
33
use rustc_middle::ty::{PolyTraitRef, Ty, Unevaluated};
4-
use rustc_session::{parse::ParseSess, Limit, SessionDiagnostic};
4+
use rustc_session::{Limit, SessionDiagnostic};
55
use rustc_span::{Span, Symbol};
66

77
#[derive(SessionDiagnostic)]
@@ -69,9 +69,9 @@ pub struct NegativePositiveConflict<'a> {
6969
impl SessionDiagnostic<'_> for NegativePositiveConflict<'_> {
7070
fn into_diagnostic(
7171
self,
72-
sess: &ParseSess,
72+
handler: &Handler,
7373
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
74-
let mut diag = sess.struct_err(fluent::trait_selection::negative_positive_conflict);
74+
let mut diag = handler.struct_err(fluent::trait_selection::negative_positive_conflict);
7575
diag.set_arg("trait_desc", self.trait_desc);
7676
diag.set_arg(
7777
"self_desc",

compiler/rustc_typeck/src/astconv/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
2929
self.tcx().sess.emit_err(MissingTypeParams {
3030
span,
3131
def_span: self.tcx().def_span(def_id),
32+
span_snippet: self.tcx().sess.source_map().span_to_snippet(span).ok(),
3233
missing_type_params,
3334
empty_generic_args,
3435
});

compiler/rustc_typeck/src/errors.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Errors emitted by typeck.
2-
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed};
2+
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
33
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
44
use rustc_middle::ty::Ty;
5-
use rustc_session::{parse::ParseSess, SessionDiagnostic};
5+
use rustc_session::SessionDiagnostic;
66
use rustc_span::{symbol::Ident, Span, Symbol};
77

88
#[derive(SessionDiagnostic)]
@@ -244,14 +244,15 @@ pub struct UnconstrainedOpaqueType {
244244
pub struct MissingTypeParams {
245245
pub span: Span,
246246
pub def_span: Span,
247+
pub span_snippet: Option<String>,
247248
pub missing_type_params: Vec<Symbol>,
248249
pub empty_generic_args: bool,
249250
}
250251

251252
// Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
252253
impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
253-
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
254-
let mut err = sess.span_diagnostic.struct_span_err_with_code(
254+
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
255+
let mut err = handler.struct_span_err_with_code(
255256
self.span,
256257
rustc_errors::fluent::typeck::missing_type_params,
257258
error_code!(E0393),
@@ -269,12 +270,9 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
269270
err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
270271

271272
let mut suggested = false;
272-
if let (Ok(snippet), true) = (
273-
sess.source_map().span_to_snippet(self.span),
274-
// Don't suggest setting the type params if there are some already: the order is
275-
// tricky to get right and the user will already know what the syntax is.
276-
self.empty_generic_args,
277-
) {
273+
// Don't suggest setting the type params if there are some already: the order is
274+
// tricky to get right and the user will already know what the syntax is.
275+
if let Some(snippet) = self.span_snippet && self.empty_generic_args {
278276
if snippet.ends_with('>') {
279277
// The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
280278
// we would have to preserve the right order. For now, as clearly the user is

0 commit comments

Comments
 (0)