UPDATE - into_diagnostic to take a Handler instead of a 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 this is the best way to address this gap
This commit is contained in:
Jhonny Bill Mena 2022-09-05 00:15:50 -04:00
parent a2cdcb3fea
commit 321e60bf34
14 changed files with 85 additions and 57 deletions

View File

@ -1,9 +1,11 @@
use std::num::IntErrorKind;
use rustc_ast as ast;
use rustc_errors::{error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_errors::{
error_code, fluent, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler,
};
use rustc_macros::SessionDiagnostic;
use rustc_session::{parse::ParseSess, SessionDiagnostic};
use rustc_session::SessionDiagnostic;
use rustc_span::{Span, Symbol};
use crate::UnsupportedLiteralReason;
@ -49,9 +51,9 @@ pub(crate) struct UnknownMetaItem<'a> {
// Manual implementation to be able to format `expected` items correctly.
impl<'a> SessionDiagnostic<'a> for UnknownMetaItem<'_> {
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let expected = self.expected.iter().map(|name| format!("`{}`", name)).collect::<Vec<_>>();
let mut diag = sess.span_diagnostic.struct_span_err_with_code(
let mut diag = handler.struct_span_err_with_code(
self.span,
fluent::attr::unknown_meta_item,
error_code!(E0541),
@ -207,8 +209,8 @@ pub(crate) struct UnsupportedLiteral {
}
impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut diag = sess.span_diagnostic.struct_span_err_with_code(
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut diag = handler.struct_span_err_with_code(
self.span,
match self.reason {
UnsupportedLiteralReason::Generic => fluent::attr::unsupported_literal_generic,
@ -223,8 +225,10 @@ impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
error_code!(E0565),
);
if self.is_bytestr {
let start_point = handler.span_start_point_from_emitter(self.span).unwrap_or(self.span);
diag.span_suggestion(
sess.source_map().start_point(self.span),
start_point,
fluent::attr::unsupported_literal_suggestion,
"",
Applicability::MaybeIncorrect,

View File

@ -1098,6 +1098,28 @@ impl Handler {
);
std::mem::take(&mut self.inner.borrow_mut().fulfilled_expectations)
}
pub fn span_to_snippet_from_emitter(
&self,
span: rustc_span::Span,
) -> Option<Result<String, rustc_span::SpanSnippetError>> {
self.inner
.borrow()
.emitter
.source_map()
.map_or_else(|| Option::None, |sm| Some(sm.span_to_snippet(span)))
}
pub fn span_start_point_from_emitter(
&self,
span: rustc_span::Span,
) -> Option<rustc_span::Span> {
self.inner
.borrow()
.emitter
.source_map()
.map_or_else(|| Option::None, |sm| Some(sm.start_point(span)))
}
}
impl HandlerInner {

View File

@ -341,7 +341,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
multi_suggestions,
bad_label,
}
.into_diagnostic(&self.tcx.sess.parse_sess),
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
TypeAnnotationNeeded::E0283 => AmbigousImpl {
span,
source_kind,
@ -351,7 +351,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
multi_suggestions,
bad_label,
}
.into_diagnostic(&self.tcx.sess.parse_sess),
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
TypeAnnotationNeeded::E0284 => AmbigousReturn {
span,
source_kind,
@ -361,7 +361,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
multi_suggestions,
bad_label,
}
.into_diagnostic(&self.tcx.sess.parse_sess),
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
}
}
@ -537,7 +537,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
multi_suggestions,
bad_label: None,
}
.into_diagnostic(&self.tcx.sess.parse_sess),
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
TypeAnnotationNeeded::E0283 => AmbigousImpl {
span,
source_kind,
@ -547,7 +547,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
multi_suggestions,
bad_label: None,
}
.into_diagnostic(&self.tcx.sess.parse_sess),
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
TypeAnnotationNeeded::E0284 => AmbigousReturn {
span,
source_kind,
@ -557,7 +557,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
multi_suggestions,
bad_label: None,
}
.into_diagnostic(&self.tcx.sess.parse_sess),
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic),
}
}
@ -575,7 +575,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
span,
generator_kind: GeneratorKindAsDiagArg(kind),
}
.into_diagnostic(&self.tcx.sess.parse_sess)
.into_diagnostic(&self.tcx.sess.parse_sess.span_diagnostic)
}
}

View File

@ -1,6 +1,6 @@
use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed};
use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed, Handler};
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic};
use rustc_session::{lint::Level, SessionDiagnostic};
use rustc_span::{Span, Symbol};
#[derive(SessionDiagnostic)]
@ -122,9 +122,9 @@ pub struct CheckNameUnknown {
impl SessionDiagnostic<'_> for CheckNameUnknown {
fn into_diagnostic(
self,
sess: &ParseSess,
handler: &Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(fluent::lint::check_name_unknown);
let mut diag = handler.struct_err(fluent::lint::check_name_unknown);
diag.code(rustc_errors::error_code!(E0602));
if let Some(suggestion) = self.suggestion {
diag.help(fluent::lint::help);

View File

@ -88,7 +88,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
{
fn into_diagnostic(
self,
#sess: &'__session_diagnostic_sess rustc_session::parse::ParseSess
#sess: &'__session_diagnostic_sess rustc_errors::Handler
) -> rustc_errors::DiagnosticBuilder<'__session_diagnostic_sess, G> {
use rustc_errors::IntoDiagnosticArg;
#implementation

View File

@ -424,9 +424,9 @@ pub(crate) struct MultipleCandidates {
impl SessionDiagnostic<'_> for MultipleCandidates {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
handler: &'_ rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::multiple_candidates);
let mut diag = handler.struct_err(rustc_errors::fluent::metadata::multiple_candidates);
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("flavor", self.flavor);
diag.code(error_code!(E0465));
@ -540,9 +540,9 @@ pub struct InvalidMetadataFiles {
impl SessionDiagnostic<'_> for InvalidMetadataFiles {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
handler: &'_ rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::invalid_meta_files);
let mut diag = handler.struct_err(rustc_errors::fluent::metadata::invalid_meta_files);
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("add_info", self.add_info);
diag.code(error_code!(E0786));
@ -568,9 +568,9 @@ pub struct CannotFindCrate {
impl SessionDiagnostic<'_> for CannotFindCrate {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
handler: &'_ rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::metadata::cannot_find_crate);
let mut diag = handler.struct_err(rustc_errors::fluent::metadata::cannot_find_crate);
diag.set_arg("crate_name", self.crate_name);
diag.set_arg("add_info", self.add_info);
diag.set_arg("locator_triple", self.locator_triple.triple());

View File

@ -47,9 +47,10 @@ pub struct UnusedGenericParams {
impl SessionDiagnostic<'_> for UnusedGenericParams {
fn into_diagnostic(
self,
sess: &'_ rustc_session::parse::ParseSess,
handler: &'_ rustc_errors::Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(rustc_errors::fluent::monomorphize::unused_generic_params);
let mut diag =
handler.struct_err(rustc_errors::fluent::monomorphize::unused_generic_params);
diag.set_span(self.span);
for (span, name) in self.param_spans.into_iter().zip(self.param_names) {
// FIXME: I can figure out how to do a label with a fluent string with a fixed message,

View File

@ -1997,7 +1997,7 @@ impl<'a> Parser<'a> {
return Err(MissingSemicolonBeforeArray {
open_delim: open_delim_span,
semicolon: prev_span.shrink_to_hi(),
}.into_diagnostic(self.sess));
}.into_diagnostic(&self.sess.span_diagnostic));
}
Ok(_) => (),
Err(err) => err.cancel(),
@ -2745,7 +2745,8 @@ impl<'a> Parser<'a> {
fn parse_try_block(&mut self, span_lo: Span) -> PResult<'a, P<Expr>> {
let (attrs, body) = self.parse_inner_attrs_and_block()?;
if self.eat_keyword(kw::Catch) {
Err(CatchAfterTry { span: self.prev_token.span }.into_diagnostic(self.sess))
Err(CatchAfterTry { span: self.prev_token.span }
.into_diagnostic(&self.sess.span_diagnostic))
} else {
let span = span_lo.to(body.span);
self.sess.gated_spans.gate(sym::try_blocks, span);

View File

@ -572,7 +572,7 @@ pub(crate) fn report_cycle<'a>(
stack_count,
};
cycle_diag.into_diagnostic(&sess.parse_sess)
cycle_diag.into_diagnostic(&sess.parse_sess.span_diagnostic)
}
pub fn print_query_stack<CTX: QueryContext>(

View File

@ -343,7 +343,7 @@ impl ParseSess {
&'a self,
err: impl SessionDiagnostic<'a>,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
err.into_diagnostic(self)
err.into_diagnostic(&self.span_diagnostic)
}
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
@ -354,7 +354,7 @@ impl ParseSess {
&'a self,
warning: impl SessionDiagnostic<'a, ()>,
) -> DiagnosticBuilder<'a, ()> {
warning.into_diagnostic(self)
warning.into_diagnostic(&self.span_diagnostic)
}
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
@ -365,7 +365,7 @@ impl ParseSess {
&'a self,
fatal: impl SessionDiagnostic<'a, !>,
) -> DiagnosticBuilder<'a, !> {
fatal.into_diagnostic(self)
fatal.into_diagnostic(&self.span_diagnostic)
}
pub fn emit_fatal<'a>(&'a self, fatal: impl SessionDiagnostic<'a, !>) -> ! {

View File

@ -21,7 +21,7 @@ use rustc_errors::json::JsonEmitter;
use rustc_errors::registry::Registry;
use rustc_errors::{
error_code, fallback_fluent_bundle, DiagnosticBuilder, DiagnosticId, DiagnosticMessage,
EmissionGuarantee, ErrorGuaranteed, FluentBundle, LazyFallbackBundle, MultiSpan,
EmissionGuarantee, ErrorGuaranteed, FluentBundle, Handler, LazyFallbackBundle, MultiSpan,
};
use rustc_macros::HashStable_Generic;
pub use rustc_span::def_id::StableCrateId;
@ -220,9 +220,9 @@ pub struct PerfStats {
/// `#[derive(SessionDiagnostic)]` -- see [rustc_macros::SessionDiagnostic].
#[rustc_diagnostic_item = "SessionDiagnostic"]
pub trait SessionDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> {
/// Write out as a diagnostic out of `sess`.
/// Write out as a diagnostic out of `Handler`.
#[must_use]
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, T>;
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, T>;
}
impl Session {

View File

@ -1,7 +1,7 @@
use rustc_errors::{fluent, ErrorGuaranteed};
use rustc_errors::{fluent, ErrorGuaranteed, Handler};
use rustc_macros::SessionDiagnostic;
use rustc_middle::ty::{PolyTraitRef, Ty, Unevaluated};
use rustc_session::{parse::ParseSess, Limit, SessionDiagnostic};
use rustc_session::{Limit, SessionDiagnostic};
use rustc_span::{Span, Symbol};
#[derive(SessionDiagnostic)]
@ -69,9 +69,9 @@ pub struct NegativePositiveConflict<'a> {
impl SessionDiagnostic<'_> for NegativePositiveConflict<'_> {
fn into_diagnostic(
self,
sess: &ParseSess,
handler: &Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(fluent::trait_selection::negative_positive_conflict);
let mut diag = handler.struct_err(fluent::trait_selection::negative_positive_conflict);
diag.set_arg("trait_desc", self.trait_desc);
diag.set_arg(
"self_desc",

View File

@ -1,8 +1,8 @@
//! Errors emitted by typeck.
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_middle::ty::Ty;
use rustc_session::{parse::ParseSess, SessionDiagnostic};
use rustc_session::SessionDiagnostic;
use rustc_span::{symbol::Ident, Span, Symbol};
#[derive(SessionDiagnostic)]
@ -250,8 +250,8 @@ pub struct MissingTypeParams {
// Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut err = sess.span_diagnostic.struct_span_err_with_code(
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut err = handler.struct_span_err_with_code(
self.span,
rustc_errors::fluent::typeck::missing_type_params,
error_code!(E0393),
@ -269,8 +269,8 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
let mut suggested = false;
if let (Ok(snippet), true) = (
sess.source_map().span_to_snippet(self.span),
if let (Some(Ok(snippet)), true) = (
handler.span_to_snippet_from_emitter(self.span),
// Don't suggest setting the type params if there are some already: the order is
// tricky to get right and the user will already know what the syntax is.
self.empty_generic_args,

View File

@ -11,9 +11,9 @@ extern crate rustc_macros;
extern crate rustc_session;
extern crate rustc_span;
use rustc_errors::{AddSubdiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, fluent};
use rustc_errors::{AddSubdiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, Handler, fluent};
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_session::{parse::ParseSess, SessionDiagnostic};
use rustc_session::SessionDiagnostic;
use rustc_span::Span;
#[derive(SessionDiagnostic)]
@ -33,8 +33,8 @@ struct Note {
pub struct UntranslatableInSessionDiagnostic;
impl<'a> SessionDiagnostic<'a, ErrorGuaranteed> for UntranslatableInSessionDiagnostic {
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
sess.struct_err("untranslatable diagnostic")
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
handler.struct_err("untranslatable diagnostic")
//~^ ERROR diagnostics should be created using translatable messages
}
}
@ -42,8 +42,8 @@ impl<'a> SessionDiagnostic<'a, ErrorGuaranteed> for UntranslatableInSessionDiagn
pub struct TranslatableInSessionDiagnostic;
impl<'a> SessionDiagnostic<'a, ErrorGuaranteed> for TranslatableInSessionDiagnostic {
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
sess.struct_err(fluent::parser::expect_path)
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
handler.struct_err(fluent::parser::expect_path)
}
}
@ -64,11 +64,11 @@ impl AddSubdiagnostic for TranslatableInAddSubdiagnostic {
}
}
pub fn make_diagnostics<'a>(sess: &'a ParseSess) {
let _diag = sess.struct_err(fluent::parser::expect_path);
pub fn make_diagnostics<'a>(handler: &'a Handler) {
let _diag = handler.struct_err(fluent::parser::expect_path);
//~^ ERROR diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
let _diag = sess.struct_err("untranslatable diagnostic");
let _diag = handler.struct_err("untranslatable diagnostic");
//~^ ERROR diagnostics should only be created in `SessionDiagnostic`/`AddSubdiagnostic` impls
//~^^ ERROR diagnostics should be created using translatable messages
}
@ -76,6 +76,6 @@ pub fn make_diagnostics<'a>(sess: &'a ParseSess) {
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted.
#[rustc_lint_diagnostics]
pub fn skipped_because_of_annotation<'a>(sess: &'a ParseSess) {
let _diag = sess.struct_err("untranslatable diagnostic"); // okay!
pub fn skipped_because_of_annotation<'a>(handler: &'a Handler) {
let _diag = handler.struct_err("untranslatable diagnostic"); // okay!
}