Skip to content

Commit 6e22c0a

Browse files
committed
impl SessionDiagnostic for LayoutError and Spanned<T>
1 parent 249e46b commit 6e22c0a

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::mono::CodegenUnit;
1313
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
1414
use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, TyAndLayout, LayoutOfHelpers};
1515
use rustc_session::Session;
16-
use rustc_span::Span;
16+
use rustc_span::{Span, source_map::respan};
1717
use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
1818
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
1919

@@ -478,6 +478,23 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
478478
#[inline]
479479
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
480480
if let LayoutError::SizeOverflow(_) = err {
481+
let _ = respan(span, err);
482+
// error: lifetime may not live long enough
483+
// --> src/context.rs:483:13
484+
// |
485+
// 475 | impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
486+
// | ---- ---- lifetime `'tcx` defined here
487+
// | |
488+
// | lifetime `'gcc` defined here
489+
// ...
490+
// 483 | self.sess().emit_fatal(respan(span, err))
491+
// | ^^^^^^^^^^^ argument requires that `'gcc` must outlive `'tcx`
492+
// |
493+
// = help: consider adding the following bound: `'gcc: 'tcx`
494+
// = note: requirement occurs because of the type `CodegenCx<'_, '_>`, which makes the generic argument `'_` invariant
495+
// = note: the struct `CodegenCx<'gcc, 'tcx>` is invariant over the parameter `'gcc`
496+
// = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
497+
// self.sess().emit_fatal(respan(span, err))
481498
self.sess().emit_fatal(LayoutSizeOverflow { span, error: err.to_string() })
482499
} else {
483500
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ use crate::ty::{
77
};
88
use rustc_ast as ast;
99
use rustc_attr as attr;
10+
use rustc_errors::Handler;
1011
use rustc_hir as hir;
1112
use rustc_hir::def_id::DefId;
1213
use rustc_hir::lang_items::LangItem;
1314
use rustc_index::bit_set::BitSet;
1415
use rustc_index::vec::{Idx, IndexVec};
15-
use rustc_session::{config::OptLevel, DataTypeKind, FieldInfo, SizeKind, VariantInfo};
16+
use rustc_session::{
17+
config::OptLevel, DataTypeKind, FieldInfo, SessionDiagnostic, SizeKind, VariantInfo,
18+
};
1619
use rustc_span::symbol::Symbol;
1720
use rustc_span::{Span, DUMMY_SP};
1821
use rustc_target::abi::call::{
@@ -206,6 +209,12 @@ pub enum LayoutError<'tcx> {
206209
NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>),
207210
}
208211

212+
impl<'a> SessionDiagnostic<'a, !> for LayoutError<'a> {
213+
fn into_diagnostic(self, handler: &'a Handler) -> rustc_errors::DiagnosticBuilder<'a, !> {
214+
handler.struct_fatal(self.to_string())
215+
}
216+
}
217+
209218
impl<'tcx> fmt::Display for LayoutError<'tcx> {
210219
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211220
match *self {

compiler/rustc_session/src/session.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_errors::{
3333
use rustc_macros::HashStable_Generic;
3434
pub use rustc_span::def_id::StableCrateId;
3535
use rustc_span::edition::Edition;
36-
use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMap, Span};
36+
use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMap, Span, Spanned};
3737
use rustc_span::{sym, SourceFileHashAlgorithm, Symbol};
3838
use rustc_target::asm::InlineAsmArch;
3939
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@@ -223,6 +223,27 @@ pub struct PerfStats {
223223
pub normalize_projection_ty: AtomicUsize,
224224
}
225225

226+
/// Trait implemented by error types. This should not be implemented manually. Instead, use
227+
/// `#[derive(SessionDiagnostic)]` -- see [rustc_macros::SessionDiagnostic].
228+
#[rustc_diagnostic_item = "SessionDiagnostic"]
229+
pub trait SessionDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> {
230+
/// Write out as a diagnostic out of `Handler`.
231+
#[must_use]
232+
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, T>;
233+
}
234+
235+
impl<'a, T, E> SessionDiagnostic<'a, E> for Spanned<T>
236+
where
237+
T: SessionDiagnostic<'a, E>,
238+
E: EmissionGuarantee,
239+
{
240+
fn into_diagnostic(self, handler: &'a Handler) -> rustc_errors::DiagnosticBuilder<'a, E> {
241+
let mut diag = self.node.into_diagnostic(handler);
242+
diag.set_span(self.span);
243+
diag
244+
}
245+
}
246+
226247
impl Session {
227248
pub fn miri_unleashed_feature(&self, span: Span, feature_gate: Option<Symbol>) {
228249
self.miri_unleashed_features.lock().push((span, feature_gate));

0 commit comments

Comments
 (0)