UPDATE - avoid exposing source_map methods from Handler

This commit is contained in:
Jhonny Bill Mena 2022-09-05 11:42:48 -04:00
parent 321e60bf34
commit 1524b59444
5 changed files with 13 additions and 34 deletions

View File

@ -63,7 +63,7 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
sess.emit_err(session_diagnostics::MultipleStabilityLevels { span }); sess.emit_err(session_diagnostics::MultipleStabilityLevels { span });
} }
AttrError::UnsupportedLiteral(reason, is_bytestr) => { 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, source_map: sess.source_map() });
} }
} }
} }

View File

@ -6,7 +6,7 @@ use rustc_errors::{
}; };
use rustc_macros::SessionDiagnostic; use rustc_macros::SessionDiagnostic;
use rustc_session::SessionDiagnostic; use rustc_session::SessionDiagnostic;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol, source_map::SourceMap};
use crate::UnsupportedLiteralReason; use crate::UnsupportedLiteralReason;
@ -202,13 +202,14 @@ pub(crate) struct InvalidReprHintNoValue {
} }
// Error code: E0565 // Error code: E0565
pub(crate) struct UnsupportedLiteral { pub(crate) struct UnsupportedLiteral<'a> {
pub span: Span, pub span: Span,
pub reason: UnsupportedLiteralReason, pub reason: UnsupportedLiteralReason,
pub is_bytestr: bool, pub is_bytestr: bool,
pub source_map: &'a SourceMap,
} }
impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral { impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral<'a> {
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> { fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut diag = handler.struct_span_err_with_code( let mut diag = handler.struct_span_err_with_code(
self.span, self.span,
@ -225,10 +226,8 @@ impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
error_code!(E0565), error_code!(E0565),
); );
if self.is_bytestr { if self.is_bytestr {
let start_point = handler.span_start_point_from_emitter(self.span).unwrap_or(self.span);
diag.span_suggestion( diag.span_suggestion(
start_point, self.source_map.start_point(self.span),
fluent::attr::unsupported_literal_suggestion, fluent::attr::unsupported_literal_suggestion,
"", "",
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,

View File

@ -1098,28 +1098,6 @@ impl Handler {
); );
std::mem::take(&mut self.inner.borrow_mut().fulfilled_expectations) 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 { impl HandlerInner {

View File

@ -29,6 +29,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.tcx().sess.emit_err(MissingTypeParams { self.tcx().sess.emit_err(MissingTypeParams {
span, span,
def_span: self.tcx().def_span(def_id), def_span: self.tcx().def_span(def_id),
source_map: self.tcx().sess.source_map(),
missing_type_params, missing_type_params,
empty_generic_args, empty_generic_args,
}); });

View File

@ -3,7 +3,7 @@ use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_session::SessionDiagnostic; use rustc_session::SessionDiagnostic;
use rustc_span::{symbol::Ident, Span, Symbol}; use rustc_span::{symbol::Ident, Span, Symbol, source_map::SourceMap};
#[derive(SessionDiagnostic)] #[derive(SessionDiagnostic)]
#[diag(typeck::field_multiply_specified_in_initializer, code = "E0062")] #[diag(typeck::field_multiply_specified_in_initializer, code = "E0062")]
@ -241,15 +241,16 @@ pub struct UnconstrainedOpaqueType {
pub name: Symbol, pub name: Symbol,
} }
pub struct MissingTypeParams { pub struct MissingTypeParams<'a> {
pub span: Span, pub span: Span,
pub def_span: Span, pub def_span: Span,
pub missing_type_params: Vec<Symbol>, pub missing_type_params: Vec<Symbol>,
pub empty_generic_args: bool, pub empty_generic_args: bool,
pub source_map: &'a SourceMap,
} }
// Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`. // Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
impl<'a> SessionDiagnostic<'a> for MissingTypeParams { impl<'a> SessionDiagnostic<'a> for MissingTypeParams<'a> {
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> { fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let mut err = handler.struct_span_err_with_code( let mut err = handler.struct_span_err_with_code(
self.span, self.span,
@ -269,8 +270,8 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
err.span_label(self.def_span, rustc_errors::fluent::typeck::label); err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
let mut suggested = false; let mut suggested = false;
if let (Some(Ok(snippet)), true) = ( if let (Ok(snippet), true) = (
handler.span_to_snippet_from_emitter(self.span), self.source_map.span_to_snippet(self.span),
// Don't suggest setting the type params if there are some already: the order is // 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. // tricky to get right and the user will already know what the syntax is.
self.empty_generic_args, self.empty_generic_args,