Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions compiler/rustc_codegen_cranelift/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use rustc_index::IndexVec;
use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::layout::{
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
codegen_handle_fn_abi_err,
};
use rustc_span::{Spanned, Symbol};
use rustc_span::Symbol;
use rustc_target::callconv::FnAbi;
use rustc_target::spec::{Arch, HasTargetSpec, Target};

Expand Down Expand Up @@ -453,23 +454,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
span: Span,
fn_abi_request: FnAbiRequest<'tcx>,
) -> ! {
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) =
err
{
self.0.sess.dcx().emit_fatal(Spanned { span, node: err })
} else {
match fn_abi_request {
FnAbiRequest::OfFnPtr { sig, extra_args } => {
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}");
}
FnAbiRequest::OfInstance { instance, extra_args } => {
span_bug!(
span,
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}"
);
}
}
}
codegen_handle_fn_abi_err(self.0, err, span, fn_abi_request).raise_fatal()
}
}

Expand Down
23 changes: 3 additions & 20 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::mir::interpret::Allocation;
use rustc_middle::mono::CodegenUnit;
use rustc_middle::span_bug;
use rustc_middle::ty::layout::{
FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, HasTyCtxt, HasTypingEnv, LayoutError,
LayoutOfHelpers,
LayoutOfHelpers, codegen_handle_fn_abi_err,
};
use rustc_middle::ty::{self, ExistentialTraitRef, Instance, Ty, TyCtxt};
#[cfg(feature = "master")]
use rustc_session::config::DebugInfo;
use rustc_session::{PointerAuthSchema, Session};
use rustc_span::{DUMMY_SP, Span, Symbol, respan};
use rustc_span::{DUMMY_SP, Span, Symbol};
use rustc_target::spec::{HasTargetSpec, HasX86AbiOpt, Target, TlsModel, X86Abi};

#[cfg(feature = "master")]
Expand Down Expand Up @@ -562,23 +561,7 @@ impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
span: Span,
fn_abi_request: FnAbiRequest<'tcx>,
) -> ! {
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) =
err
{
self.tcx.dcx().emit_fatal(respan(span, err))
} else {
match fn_abi_request {
FnAbiRequest::OfFnPtr { sig, extra_args } => {
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}");
}
FnAbiRequest::OfInstance { instance, extra_args } => {
span_bug!(
span,
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}"
);
}
}
}
codegen_handle_fn_abi_err(self.tcx, err, span, fn_abi_request).raise_fatal()
}
}

Expand Down
22 changes: 4 additions & 18 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId;
use rustc_middle::bug;
use rustc_middle::mono::CodegenUnit;
use rustc_middle::ty::layout::{
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
codegen_handle_fn_abi_err,
};
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_sanitizers::ignorelist::{SanitizerIgnoreList, typename_for_ignore_list};
use rustc_session::config::{
BranchProtection, CFGuard, CFProtection, DebugInfo, FunctionReturn, PAuthKey, PacRet,
};
use rustc_session::{PointerAuthSchema, Session};
use rustc_span::{DUMMY_SP, Span, Spanned, Symbol, sym};
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
use rustc_structures::CrateType;
use rustc_target::spec::{
Arch, CfgAbi, Env, FramePointer, HasTargetSpec, Os, RelocModel, SmallDataThresholdSupport,
Expand Down Expand Up @@ -1316,21 +1317,6 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
span: Span,
fn_abi_request: FnAbiRequest<'tcx>,
) -> ! {
match err {
FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) => {
self.tcx.dcx().emit_fatal(Spanned { span, node: err });
}
_ => match fn_abi_request {
FnAbiRequest::OfFnPtr { sig, extra_args } => {
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
}
FnAbiRequest::OfInstance { instance, extra_args } => {
span_bug!(
span,
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
);
}
},
}
codegen_handle_fn_abi_err(self.tcx, err, span, fn_abi_request).raise_fatal()
}
}
10 changes: 5 additions & 5 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
}
sym::offload => {
if tcx.sess.opts.unstable_opts.offload.is_empty() {
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutEnable);
let _ = tcx.dcx().emit_err(OffloadWithoutEnable);
}

if tcx.sess.lto() != rustc_session::config::Lto::Fat {
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutFatLTO);
let _ = tcx.dcx().emit_err(OffloadWithoutFatLTO);
}

codegen_offload(self, tcx, instance, args);
Expand Down Expand Up @@ -1752,18 +1752,18 @@ fn codegen_autodiff<'ll, 'tcx>(
) -> IntrinsicResult<'tcx, &'ll Value> {
let tcx = bx.tcx;
if !tcx.sess.opts.unstable_opts.autodiff.contains(&rustc_session::config::AutoDiff::Enable) {
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutEnable);
let _ = tcx.dcx().emit_err(AutoDiffWithoutEnable);
}

let ct = tcx.crate_types();
let lto = tcx.sess.lto();
if ct.len() == 1 && ct.contains(&CrateType::Executable) {
if lto != rustc_session::config::Lto::Fat {
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
let _ = tcx.dcx().emit_err(AutoDiffWithoutLto);
}
} else {
if lto != rustc_session::config::Lto::Fat && !tcx.sess.opts.cg.linker_plugin_lto.enabled() {
let _ = tcx.dcx().emit_almost_fatal(AutoDiffWithoutLto);
let _ = tcx.dcx().emit_err(AutoDiffWithoutLto);
}
}

Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ impl EmissionGuarantee for FatalAbort {
}
}

impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
fn emit_producing_guarantee(diag: Diag<'_, Self>) -> Self::EmitResult {
diag.emit_producing_nothing();
rustc_span::fatal_error::FatalError
}
}

/// Trait implemented by error types. This is rarely implemented manually. Instead, use
/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
///
Expand Down
46 changes: 15 additions & 31 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,19 +1026,6 @@ impl<'a> DiagCtxtHandle<'a> {
self.create_fatal(fatal).emit()
}

#[track_caller]
pub fn create_almost_fatal(
self,
fatal: impl Diagnostic<'a, FatalError>,
) -> Diag<'a, FatalError> {
fatal.into_diag(self, Fatal)
}

#[track_caller]
pub fn emit_almost_fatal(self, fatal: impl Diagnostic<'a, FatalError>) -> FatalError {
self.create_almost_fatal(fatal).emit()
}

// FIXME: This method should be removed (every error should have an associated error code).
#[track_caller]
pub fn struct_err(self, msg: impl Into<DiagMessage>) -> Diag<'a> {
Expand Down Expand Up @@ -1582,24 +1569,21 @@ impl DelayedDiagInner {
}
}

/// | Level | is_error | EmissionGuarantee | Top-level | Sub | Used in lints?
/// | ----- | -------- | ----------------- | --------- | --- | --------------
/// | Bug | yes | BugAbort | yes | - | -
/// | Fatal | yes | FatalAbort/FatalError[^star] | yes | - | -
/// | Error | yes | ErrorGuaranteed | yes | - | yes
/// | DelayedBug | yes | ErrorGuaranteed | yes | - | -
/// | ForceWarning | - | () | yes | - | lint-only
/// | Warning | - | () | yes | yes | yes
/// | Note | - | () | rare | yes | -
/// | OnceNote | - | () | - | yes | lint-only
/// | Help | - | () | rare | yes | -
/// | OnceHelp | - | () | - | yes | lint-only
/// | FailureNote | - | () | rare | - | -
/// | Allow | - | () | yes | - | lint-only
/// | Expect | - | () | yes | - | lint-only
///
/// [^star]: `FatalAbort` normally, `FatalError` in the non-aborting "almost fatal" case that is
/// occasionally used.
/// | Level | is_error | EmissionGuarantee | Top-level | Sub | Used in lints?
/// | ----- | -------- | ----------------- | --------- | --- | --------------
/// | Bug | yes | BugAbort | yes | - | -
/// | Fatal | yes | FatalAbort | yes | - | -
/// | Error | yes | ErrorGuaranteed | yes | - | yes
/// | DelayedBug | yes | ErrorGuaranteed | yes | - | -
/// | ForceWarning | - | () | yes | - | lint-only
/// | Warning | - | () | yes | yes | yes
/// | Note | - | () | rare | yes | -
/// | OnceNote | - | () | - | yes | lint-only
/// | Help | - | () | rare | yes | -
/// | OnceHelp | - | () | - | yes | lint-only
/// | FailureNote | - | () | rare | - | -
/// | Allow | - | () | yes | - | lint-only
/// | Expect | - | () | yes | - | lint-only
///
#[derive(Copy, PartialEq, Eq, Clone, Hash, Debug, Encodable, Decodable)]
pub enum Level {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,10 @@ pub(crate) fn start_codegen<'tcx>(

info!("Pre-codegen\n{:?}", tcx.debug_stats());

let metadata = rustc_metadata::fs::encode_and_write_metadata(tcx);
let metadata = match rustc_metadata::fs::encode_and_write_metadata(tcx) {
Ok(metadata) => metadata,
Err(guar) => guar.raise_fatal(),
};

let is_host_metadata = tcx
.sess
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_metadata/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::config::{OutFileName, OutputType};
use rustc_session::output::filename_for_metadata;
use rustc_span::ErrorGuaranteed;
use rustc_structures::CrateType;

use crate::diagnostics::{
Expand Down Expand Up @@ -34,7 +35,7 @@ pub fn emit_wrapper_file(sess: &Session, data: &[u8], tmpdir: &Path, name: &str)
out_filename
}

pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> Result<EncodedMetadata, ErrorGuaranteed> {
let out_filename = filename_for_metadata(tcx.sess, tcx.output_filenames(()));
// To avoid races with another rustc process scanning the output directory,
// we need to write the file somewhere else and atomically move it to its
Expand Down Expand Up @@ -70,6 +71,10 @@ pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
}
}

if let Some(guar) = tcx.sess.dcx().has_errors_or_delayed_bugs() {
return Err(guar);
}

let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");

// If the user requests metadata as output, rename `metadata_filename`
Expand Down Expand Up @@ -109,7 +114,7 @@ pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
tcx.dcx().emit_fatal(FailedCreateEncodedMetadata { err });
});

metadata
Ok(metadata)
}

#[cfg(not(target_os = "linux"))]
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ impl<'tcx> Collector<'tcx> {
DllCallingConvention::Vectorcall(self.i686_arg_list_size(item))
}
_ => {
self.tcx.dcx().emit_fatal(diagnostics::RawDylibUnsupportedAbi { span });
self.tcx.dcx().emit_err(diagnostics::RawDylibUnsupportedAbi { span });
return None;
}
}
} else {
Expand All @@ -283,7 +284,8 @@ impl<'tcx> Collector<'tcx> {
DllCallingConvention::C
}
_ => {
self.tcx.dcx().emit_fatal(diagnostics::RawDylibUnsupportedAbi { span });
self.tcx.dcx().emit_err(diagnostics::RawDylibUnsupportedAbi { span });
return None;
}
}
};
Expand Down
15 changes: 13 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,8 +1571,19 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
if let DefKind::Static { .. } = def_kind {
if !self.tcx.is_foreign_item(def_id) {
let data = self.tcx.eval_static_initializer(def_id).unwrap();
record!(self.tables.eval_static_initializer[def_id] <- data);
match self.tcx.eval_static_initializer(def_id) {
Ok(data) => record!(self.tables.eval_static_initializer[def_id] <- data),
Err(err) => match err {
interpret::ErrorHandled::Reported(_, _) => {
self.tcx.dcx().delayed_bug(format!(
"eval_static_initializer returned an error in metadata emission"
));
}
interpret::ErrorHandled::TooGeneric(span) => {
span_bug!(span, "generic static???");
}
},
};
}
}
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
Expand Down
26 changes: 25 additions & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_hir::attrs::lang_items::LangItem;
use rustc_hir::def_id::DefId;
use rustc_macros::{StableHash, TyDecodable, TyEncodable, extension};
use rustc_session::config::OptLevel;
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Spanned, Symbol, sym};
use rustc_structures::Limit;
use rustc_target::callconv::FnAbi;
use rustc_target::spec::{HasTargetSpec, HasX86AbiOpt, Target, X86Abi};
Expand Down Expand Up @@ -1374,6 +1374,8 @@ pub trait FnAbiOfHelpers<'tcx>: LayoutOfHelpers<'tcx> {
/// but this hook allows e.g. codegen to return only `&FnAbi` from its
/// `cx.fn_abi_of_*(...)`, without any `Result<...>` around it to deal with
/// (and any `FnAbiError`s are turned into fatal errors or ICEs).
///
/// Codegen backends should use [`codegen_handle_fn_abi_err`] as implementation.
fn handle_fn_abi_err(
&self,
err: FnAbiError<'tcx>,
Expand All @@ -1382,6 +1384,28 @@ pub trait FnAbiOfHelpers<'tcx>: LayoutOfHelpers<'tcx> {
) -> <Self::FnAbiOfResult as MaybeResult<&'tcx FnAbi<'tcx, Ty<'tcx>>>>::Error;
}

/// Implementation of [`FnAbiOfHelpers::handle_fn_abi_err`] for codegen backends.
pub fn codegen_handle_fn_abi_err<'tcx>(
tcx: TyCtxt<'tcx>,
err: FnAbiError<'tcx>,
span: Span,
fn_abi_request: FnAbiRequest<'tcx>,
) -> ErrorGuaranteed {
match err {
FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) => {
tcx.dcx().emit_err(Spanned { span, node: err })
}
_ => match fn_abi_request {
FnAbiRequest::OfFnPtr { sig, extra_args } => {
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
}
FnAbiRequest::OfInstance { instance, extra_args } => {
span_bug!(span, "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",);
}
},
}
}

/// Blanket extension trait for contexts that can compute `FnAbi`s.
pub trait FnAbiOf<'tcx>: FnAbiOfHelpers<'tcx> {
/// Compute a `FnAbi` suitable for indirect calls, i.e. to `fn` pointers.
Expand Down
Loading
Loading