Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SessionDiagnostic::into_diagnostic to take Handler instead of ParseSess #101434

7 changes: 6 additions & 1 deletion compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
sess.emit_err(session_diagnostics::MultipleStabilityLevels { span });
}
AttrError::UnsupportedLiteral(reason, is_bytestr) => {
sess.emit_err(session_diagnostics::UnsupportedLiteral { span, reason, is_bytestr });
sess.emit_err(session_diagnostics::UnsupportedLiteral {
span,
reason,
is_bytestr,
start_point_span: sess.source_map().start_point(span),
});
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_attr/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -204,11 +206,12 @@ pub(crate) struct UnsupportedLiteral {
pub span: Span,
pub reason: UnsupportedLiteralReason,
pub is_bytestr: bool,
pub start_point_span: Span,
}

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,
Expand All @@ -224,7 +227,7 @@ impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
);
if self.is_bytestr {
diag.span_suggestion(
sess.source_map().start_point(self.span),
self.start_point_span,
fluent::attr::unsupported_literal_suggestion,
"",
Applicability::MaybeIncorrect,
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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),
}
}

Expand All @@ -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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_lint/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_macros/src/diagnostics/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_monomorphize/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/query/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_session/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, ()>) {
Expand All @@ -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, !>) -> ! {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_trait_selection/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/astconv/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.tcx().sess.emit_err(MissingTypeParams {
span,
def_span: self.tcx().def_span(def_id),
span_snippet: self.tcx().sess.source_map().span_to_snippet(span).ok(),
missing_type_params,
empty_generic_args,
});
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_typeck/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -244,14 +244,15 @@ pub struct UnconstrainedOpaqueType {
pub struct MissingTypeParams {
pub span: Span,
pub def_span: Span,
pub span_snippet: Option<String>,
pub missing_type_params: Vec<Symbol>,
pub empty_generic_args: bool,
}

// 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),
Expand All @@ -269,12 +270,9 @@ 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),
// 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,
) {
// 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.
if let Some(snippet) = self.span_snippet && self.empty_generic_args {
if snippet.ends_with('>') {
// The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
// we would have to preserve the right order. For now, as clearly the user is
Expand Down
Loading