diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index a8ed510866d..faba9856f02 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -63,7 +63,7 @@ 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, source_map: sess.source_map() }); } } } diff --git a/compiler/rustc_attr/src/session_diagnostics.rs b/compiler/rustc_attr/src/session_diagnostics.rs index f74540e9655..3f5a51fbd83 100644 --- a/compiler/rustc_attr/src/session_diagnostics.rs +++ b/compiler/rustc_attr/src/session_diagnostics.rs @@ -6,7 +6,7 @@ use rustc_errors::{ }; use rustc_macros::SessionDiagnostic; use rustc_session::SessionDiagnostic; -use rustc_span::{Span, Symbol}; +use rustc_span::{Span, Symbol, source_map::SourceMap}; use crate::UnsupportedLiteralReason; @@ -202,13 +202,14 @@ pub(crate) struct InvalidReprHintNoValue { } // Error code: E0565 -pub(crate) struct UnsupportedLiteral { +pub(crate) struct UnsupportedLiteral<'a> { pub span: Span, pub reason: UnsupportedLiteralReason, 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> { let mut diag = handler.struct_span_err_with_code( self.span, @@ -225,10 +226,8 @@ 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( - start_point, + self.source_map.start_point(self.span), fluent::attr::unsupported_literal_suggestion, "", Applicability::MaybeIncorrect, diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index af554db3013..68abdd0bad1 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1098,28 +1098,6 @@ 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> { - 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 { - self.inner - .borrow() - .emitter - .source_map() - .map_or_else(|| Option::None, |sm| Some(sm.start_point(span))) - } } impl HandlerInner { diff --git a/compiler/rustc_typeck/src/astconv/errors.rs b/compiler/rustc_typeck/src/astconv/errors.rs index ff39bf36129..1262dd7dcc7 100644 --- a/compiler/rustc_typeck/src/astconv/errors.rs +++ b/compiler/rustc_typeck/src/astconv/errors.rs @@ -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), + source_map: self.tcx().sess.source_map(), missing_type_params, empty_generic_args, }); diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index bfe03d62575..7b553a46e3b 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -3,7 +3,7 @@ use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic}; use rustc_middle::ty::Ty; use rustc_session::SessionDiagnostic; -use rustc_span::{symbol::Ident, Span, Symbol}; +use rustc_span::{symbol::Ident, Span, Symbol, source_map::SourceMap}; #[derive(SessionDiagnostic)] #[diag(typeck::field_multiply_specified_in_initializer, code = "E0062")] @@ -241,15 +241,16 @@ pub struct UnconstrainedOpaqueType { pub name: Symbol, } -pub struct MissingTypeParams { +pub struct MissingTypeParams<'a> { pub span: Span, pub def_span: Span, pub missing_type_params: Vec, pub empty_generic_args: bool, + pub source_map: &'a SourceMap, } // 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> { let mut err = handler.struct_span_err_with_code( self.span, @@ -269,8 +270,8 @@ impl<'a> SessionDiagnostic<'a> for MissingTypeParams { err.span_label(self.def_span, rustc_errors::fluent::typeck::label); let mut suggested = false; - if let (Some(Ok(snippet)), true) = ( - handler.span_to_snippet_from_emitter(self.span), + if let (Ok(snippet), true) = ( + self.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,