Skip to content

Commit 4ebf1e6

Browse files
Add InterpErrorExt::to_string
1 parent 15ce96c commit 4ebf1e6

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ where
165165
let span = span.unwrap_or(our_span);
166166

167167
let handler = &tcx.sess.parse_sess.span_diagnostic;
168-
let mut err = handler.create_err(Spanned { span, node: InterpErrorExt(error)});
168+
let mut err = handler.create_err(Spanned { span, node: InterpErrorExt(error) });
169169

170170
for frame in frames {
171171
err.eager_subdiagnostic(handler, frame);

compiler/rustc_const_eval/src/errors.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::mir::interpret::{
99
MachineStopType, PointerKind, ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo,
1010
ValidationErrorInfo,
1111
};
12-
use rustc_middle::ty::Ty;
12+
use rustc_middle::ty::{self, Ty};
1313
use rustc_span::{ErrorGuaranteed, Span};
1414
use rustc_target::abi::call::AdjustForForeignAbiError;
1515
use rustc_target::abi::{Size, WrappingRange};
@@ -957,6 +957,25 @@ impl IntoDiagnostic<'_> for InterpErrorExt<'_> {
957957
}
958958
}
959959

960+
impl InterpErrorExt<'_> {
961+
/// Translate InterpError to String.
962+
///
963+
/// This should not be used for any user-facing diagnostics,
964+
/// only for debug messages in the docs.
965+
pub fn to_string(self) -> String {
966+
ty::tls::with(move |tcx| {
967+
let handler = &tcx.sess.parse_sess.span_diagnostic;
968+
let builder = self.into_diagnostic(handler);
969+
let s = handler.eagerly_translate_to_string(
970+
builder.message.first().unwrap().0.to_owned(),
971+
builder.args(),
972+
);
973+
builder.cancel();
974+
s
975+
})
976+
}
977+
}
978+
960979
pub struct MachineStopExt(Box<dyn MachineStopType>);
961980

962981
impl IntoDiagnostic<'_> for MachineStopExt {

compiler/rustc_mir_transform/src/const_prop.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use either::Right;
66
use rustc_const_eval::const_eval::CheckAlignment;
77
use rustc_const_eval::InterpErrorExt;
88
use rustc_data_structures::fx::FxHashSet;
9-
use rustc_errors::{DiagnosticMessage, IntoDiagnostic};
109
use rustc_hir::def::DefKind;
1110
use rustc_index::bit_set::BitSet;
1211
use rustc_index::{IndexSlice, IndexVec};
@@ -17,7 +16,6 @@ use rustc_middle::mir::*;
1716
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
1817
use rustc_middle::ty::InternalSubsts;
1918
use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeVisitableExt};
20-
use rustc_span::sym::DiagnosticMessage;
2119
use rustc_span::{def_id::DefId, Span, DUMMY_SP};
2220
use rustc_target::abi::{self, Align, HasDataLayout, Size, TargetDataLayout};
2321
use rustc_target::spec::abi::Abi as CallAbi;
@@ -388,17 +386,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
388386
op
389387
}
390388
Err(e) => {
391-
let msg = ty::tls::with(move |tcx| {
392-
let handler = &tcx.sess.parse_sess.span_diagnostic;
393-
let err = InterpErrorExt(e.into_kind());
394-
let builder = err.into_diagnostic(handler);
395-
builder.cancel();
396-
"Should be diagnostic message"
397-
});
398-
trace!(
399-
"get_const failed: {:?}",
400-
todo!("find a way to convert diagnostic builder to string")
401-
);
389+
trace!("get_const failed: {}", InterpErrorExt(e.into_kind()).to_string());
402390
return None;
403391
}
404392
};

compiler/rustc_mir_transform/src/const_prop_lint.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_const_eval::interpret::Immediate;
99
use rustc_const_eval::interpret::{
1010
self, InterpCx, InterpResult, LocalValue, MemoryKind, OpTy, Scalar, StackPopCleanup,
1111
};
12+
use rustc_const_eval::InterpErrorExt;
1213
use rustc_hir::def::DefKind;
1314
use rustc_hir::HirId;
1415
use rustc_index::bit_set::BitSet;
@@ -232,10 +233,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
232233
op
233234
}
234235
Err(e) => {
235-
trace!(
236-
"get_const failed: {:?}",
237-
todo!("find a way to convert diagnostic to string")
238-
);
236+
trace!("get_const failed: {}", InterpErrorExt(e.into_kind()).to_string());
239237
return None;
240238
}
241239
};

src/tools/miri/src/diagnostics.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::num::NonZeroU64;
33

44
use log::trace;
55

6+
use rustc_const_eval::InterpErrorExt;
67
use rustc_errors::DiagnosticMessage;
78
use rustc_span::{source_map::DUMMY_SP, SpanData, Symbol};
89
use rustc_target::abi::{Align, Size};
@@ -334,15 +335,7 @@ pub fn report_error<'tcx, 'mir>(
334335

335336
// FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the
336337
// label and arguments from the InterpError.
337-
let e = {
338-
let handler = &ecx.tcx.sess.parse_sess.span_diagnostic;
339-
let mut diag = ecx.tcx.sess.struct_allow("");
340-
let msg = e.diagnostic_message();
341-
e.add_args(handler, &mut diag);
342-
let s = handler.eagerly_translate_to_string(msg, diag.args());
343-
diag.cancel();
344-
s
345-
};
338+
let e = InterpErrorExt(e).to_string();
346339

347340
msg.insert(0, e);
348341

0 commit comments

Comments
 (0)