impl SessionDiagnostic for LayoutError and Spanned<T>

This commit is contained in:
Ellis Hoag 2022-09-11 17:34:51 -07:00
parent 249e46bfba
commit 6e22c0a8e1
3 changed files with 50 additions and 3 deletions

View File

@ -13,7 +13,7 @@ use rustc_middle::mir::mono::CodegenUnit;
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, TyAndLayout, LayoutOfHelpers};
use rustc_session::Session;
use rustc_span::Span;
use rustc_span::{Span, source_map::respan};
use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};
@ -478,6 +478,23 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
#[inline]
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
if let LayoutError::SizeOverflow(_) = err {
let _ = respan(span, err);
// error: lifetime may not live long enough
// --> src/context.rs:483:13
// |
// 475 | impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
// | ---- ---- lifetime `'tcx` defined here
// | |
// | lifetime `'gcc` defined here
// ...
// 483 | self.sess().emit_fatal(respan(span, err))
// | ^^^^^^^^^^^ argument requires that `'gcc` must outlive `'tcx`
// |
// = help: consider adding the following bound: `'gcc: 'tcx`
// = note: requirement occurs because of the type `CodegenCx<'_, '_>`, which makes the generic argument `'_` invariant
// = note: the struct `CodegenCx<'gcc, 'tcx>` is invariant over the parameter `'gcc`
// = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
// self.sess().emit_fatal(respan(span, err))
self.sess().emit_fatal(LayoutSizeOverflow { span, error: err.to_string() })
} else {
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)

View File

@ -7,12 +7,15 @@ use crate::ty::{
};
use rustc_ast as ast;
use rustc_attr as attr;
use rustc_errors::Handler;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::BitSet;
use rustc_index::vec::{Idx, IndexVec};
use rustc_session::{config::OptLevel, DataTypeKind, FieldInfo, SizeKind, VariantInfo};
use rustc_session::{
config::OptLevel, DataTypeKind, FieldInfo, SessionDiagnostic, SizeKind, VariantInfo,
};
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::call::{
@ -206,6 +209,12 @@ pub enum LayoutError<'tcx> {
NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>),
}
impl<'a> SessionDiagnostic<'a, !> for LayoutError<'a> {
fn into_diagnostic(self, handler: &'a Handler) -> rustc_errors::DiagnosticBuilder<'a, !> {
handler.struct_fatal(self.to_string())
}
}
impl<'tcx> fmt::Display for LayoutError<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {

View File

@ -33,7 +33,7 @@ use rustc_errors::{
use rustc_macros::HashStable_Generic;
pub use rustc_span::def_id::StableCrateId;
use rustc_span::edition::Edition;
use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMap, Span};
use rustc_span::source_map::{FileLoader, RealFileLoader, SourceMap, Span, Spanned};
use rustc_span::{sym, SourceFileHashAlgorithm, Symbol};
use rustc_target::asm::InlineAsmArch;
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@ -223,6 +223,27 @@ pub struct PerfStats {
pub normalize_projection_ty: AtomicUsize,
}
/// Trait implemented by error types. This should not be implemented manually. Instead, use
/// `#[derive(SessionDiagnostic)]` -- see [rustc_macros::SessionDiagnostic].
#[rustc_diagnostic_item = "SessionDiagnostic"]
pub trait SessionDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> {
/// Write out as a diagnostic out of `Handler`.
#[must_use]
fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, T>;
}
impl<'a, T, E> SessionDiagnostic<'a, E> for Spanned<T>
where
T: SessionDiagnostic<'a, E>,
E: EmissionGuarantee,
{
fn into_diagnostic(self, handler: &'a Handler) -> rustc_errors::DiagnosticBuilder<'a, E> {
let mut diag = self.node.into_diagnostic(handler);
diag.set_span(self.span);
diag
}
}
impl Session {
pub fn miri_unleashed_feature(&self, span: Span, feature_gate: Option<Symbol>) {
self.miri_unleashed_features.lock().push((span, feature_gate));