diff options
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/mir/consts.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/coverage.rs | 39 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/interpret/error.rs | 33 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/interpret/queries.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/query.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/query/mod.rs | 2 |
6 files changed, 70 insertions, 49 deletions
diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 7983329b0f7..52009422d98 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -8,6 +8,7 @@ use rustc_session::config::RemapPathScopeComponents; use rustc_span::{DUMMY_SP, Span}; use rustc_type_ir::visit::TypeVisitableExt; +use super::interpret::ReportedErrorInfo; use crate::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, Scalar, alloc_range}; use crate::mir::{Promoted, pretty_print_const_value}; use crate::ty::print::{pretty_print_const, with_no_trimmed_paths}; @@ -331,7 +332,10 @@ impl<'tcx> Const<'tcx> { ConstKind::Expr(_) => { bug!("Normalization of `ty::ConstKind::Expr` is unimplemented") } - _ => Err(tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body").into()), + _ => Err(ReportedErrorInfo::non_const_eval_error( + tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body"), + ) + .into()), } } Const::Unevaluated(uneval, _) => { diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index b7410ca5f18..962176290df 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -3,6 +3,7 @@ use std::fmt::{self, Debug, Formatter}; use rustc_index::IndexVec; +use rustc_index::bit_set::BitSet; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; use rustc_span::Span; @@ -310,3 +311,41 @@ pub struct MCDCDecisionSpan { pub decision_depth: u16, pub num_conditions: usize, } + +/// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass +/// (for compiler option `-Cinstrument-coverage`), after MIR optimizations +/// have had a chance to potentially remove some of them. +/// +/// Used by the `coverage_ids_info` query. +#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable)] +pub struct CoverageIdsInfo { + pub counters_seen: BitSet<CounterId>, + pub zero_expressions: BitSet<ExpressionId>, +} + +impl CoverageIdsInfo { + /// Coverage codegen needs to know how many coverage counters are ever + /// incremented within a function, so that it can set the `num-counters` + /// argument of the `llvm.instrprof.increment` intrinsic. + /// + /// This may be less than the highest counter ID emitted by the + /// InstrumentCoverage MIR pass, if the highest-numbered counter increments + /// were removed by MIR optimizations. + pub fn num_counters_after_mir_opts(&self) -> u32 { + // FIXME(Zalathar): Currently this treats an unused counter as "used" + // if its ID is less than that of the highest counter that really is + // used. Fixing this would require adding a renumbering step somewhere. + self.counters_seen.last_set_in(..).map_or(0, |max| max.as_u32() + 1) + } + + /// Returns `true` if the given term is known to have a value of zero, taking + /// into account knowledge of which counters are unused and which expressions + /// are always zero. + pub fn is_zero_term(&self, term: CovTerm) -> bool { + match term { + CovTerm::Zero => true, + CovTerm::Counter(id) => !self.counters_seen.contains(id), + CovTerm::Expression(id) => self.zero_expressions.contains(id), + } + } +} diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index ad5d678178d..fbada6ec405 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -28,10 +28,10 @@ pub enum ErrorHandled { TooGeneric(Span), } -impl From<ErrorGuaranteed> for ErrorHandled { +impl From<ReportedErrorInfo> for ErrorHandled { #[inline] - fn from(error: ErrorGuaranteed) -> ErrorHandled { - ErrorHandled::Reported(error.into(), DUMMY_SP) + fn from(error: ReportedErrorInfo) -> ErrorHandled { + ErrorHandled::Reported(error, DUMMY_SP) } } @@ -65,6 +65,20 @@ pub struct ReportedErrorInfo { impl ReportedErrorInfo { #[inline] + pub fn const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo { + ReportedErrorInfo { allowed_in_infallible: false, error } + } + + /// Use this when the error that led to this is *not* a const-eval error + /// (e.g., a layout or type checking error). + #[inline] + pub fn non_const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo { + ReportedErrorInfo { allowed_in_infallible: true, error } + } + + /// Use this when the error that led to this *is* a const-eval error, but + /// we do allow it to occur in infallible constants (e.g., resource exhaustion). + #[inline] pub fn allowed_in_infallible(error: ErrorGuaranteed) -> ReportedErrorInfo { ReportedErrorInfo { allowed_in_infallible: true, error } } @@ -74,13 +88,6 @@ impl ReportedErrorInfo { } } -impl From<ErrorGuaranteed> for ReportedErrorInfo { - #[inline] - fn from(error: ErrorGuaranteed) -> ReportedErrorInfo { - ReportedErrorInfo { allowed_in_infallible: false, error } - } -} - impl Into<ErrorGuaranteed> for ReportedErrorInfo { #[inline] fn into(self) -> ErrorGuaranteed { @@ -180,12 +187,6 @@ fn print_backtrace(backtrace: &Backtrace) { eprintln!("\n\nAn error occurred in the MIR interpreter:\n{backtrace}"); } -impl From<ErrorGuaranteed> for InterpErrorInfo<'_> { - fn from(err: ErrorGuaranteed) -> Self { - InterpErrorKind::InvalidProgram(InvalidProgramInfo::AlreadyReported(err.into())).into() - } -} - impl From<ErrorHandled> for InterpErrorInfo<'_> { fn from(err: ErrorHandled) -> Self { InterpErrorKind::InvalidProgram(match err { diff --git a/compiler/rustc_middle/src/mir/interpret/queries.rs b/compiler/rustc_middle/src/mir/interpret/queries.rs index e540f0194ec..f7f38575bd0 100644 --- a/compiler/rustc_middle/src/mir/interpret/queries.rs +++ b/compiler/rustc_middle/src/mir/interpret/queries.rs @@ -6,6 +6,7 @@ use tracing::{debug, instrument}; use super::{ ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, EvalToValTreeResult, GlobalId, + ReportedErrorInfo, }; use crate::mir; use crate::query::TyCtxtEnsure; @@ -81,7 +82,9 @@ impl<'tcx> TyCtxt<'tcx> { // For errors during resolution, we deliberately do not point at the usage site of the constant, // since for these errors the place the constant is used shouldn't matter. Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)), - Err(err) => Err(ErrorHandled::Reported(err.into(), DUMMY_SP)), + Err(err) => { + Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP)) + } } } @@ -138,7 +141,9 @@ impl<'tcx> TyCtxt<'tcx> { // For errors during resolution, we deliberately do not point at the usage site of the constant, // since for these errors the place the constant is used shouldn't matter. Ok(None) => Err(ErrorHandled::TooGeneric(DUMMY_SP)), - Err(err) => Err(ErrorHandled::Reported(err.into(), DUMMY_SP)), + Err(err) => { + Err(ErrorHandled::Reported(ReportedErrorInfo::non_const_eval_error(err), DUMMY_SP)) + } } } diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 80dfcbf2e69..f690359e012 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -8,7 +8,7 @@ use rustc_abi::{FieldIdx, VariantIdx}; use rustc_data_structures::fx::FxIndexMap; use rustc_errors::ErrorGuaranteed; use rustc_hir::def_id::LocalDefId; -use rustc_index::bit_set::{BitMatrix, BitSet}; +use rustc_index::bit_set::BitMatrix; use rustc_index::{Idx, IndexVec}; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; use rustc_span::Span; @@ -16,7 +16,6 @@ use rustc_span::symbol::Symbol; use smallvec::SmallVec; use super::{ConstValue, SourceInfo}; -use crate::mir; use crate::ty::fold::fold_regions; use crate::ty::{self, CoroutineArgsExt, OpaqueHiddenType, Ty, TyCtxt}; @@ -351,30 +350,3 @@ pub struct DestructuredConstant<'tcx> { pub variant: Option<VariantIdx>, pub fields: &'tcx [(ConstValue<'tcx>, Ty<'tcx>)], } - -/// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass -/// (for compiler option `-Cinstrument-coverage`), after MIR optimizations -/// have had a chance to potentially remove some of them. -/// -/// Used by the `coverage_ids_info` query. -#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable)] -pub struct CoverageIdsInfo { - pub counters_seen: BitSet<mir::coverage::CounterId>, - pub expressions_seen: BitSet<mir::coverage::ExpressionId>, -} - -impl CoverageIdsInfo { - /// Coverage codegen needs to know how many coverage counters are ever - /// incremented within a function, so that it can set the `num-counters` - /// argument of the `llvm.instrprof.increment` intrinsic. - /// - /// This may be less than the highest counter ID emitted by the - /// InstrumentCoverage MIR pass, if the highest-numbered counter increments - /// were removed by MIR optimizations. - pub fn num_counters_after_mir_opts(&self) -> u32 { - // FIXME(Zalathar): Currently this treats an unused counter as "used" - // if its ID is less than that of the highest counter that really is - // used. Fixing this would require adding a renumbering step somewhere. - self.counters_seen.last_set_in(..).map_or(0, |max| max.as_u32() + 1) - } -} diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index a3976c3dda1..fc3d690a8a9 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -581,7 +581,7 @@ rustc_queries! { /// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass /// (for compiler option `-Cinstrument-coverage`), after MIR optimizations /// have had a chance to potentially remove some of them. - query coverage_ids_info(key: ty::InstanceKind<'tcx>) -> &'tcx mir::CoverageIdsInfo { + query coverage_ids_info(key: ty::InstanceKind<'tcx>) -> &'tcx mir::coverage::CoverageIdsInfo { desc { |tcx| "retrieving coverage IDs info from MIR for `{}`", tcx.def_path_str(key.def_id()) } arena_cache } |
