Skip to content

Commit 87ecf84

Browse files
committed
Improve CTFE validation error message
1 parent fb3ea63 commit 87ecf84

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ pub enum UndefinedBehaviorInfo<'tcx> {
256256
/// The value validity check found a problem.
257257
/// Should only be thrown by `validity.rs` and always point out which part of the value
258258
/// is the problem.
259-
ValidationFailure(String),
259+
ValidationFailure {
260+
path: Option<String>,
261+
msg: String,
262+
},
260263
/// Using a non-boolean `u8` as bool.
261264
InvalidBool(u8),
262265
/// Using a non-character `u32` as character.
@@ -331,7 +334,10 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
331334
),
332335
WriteToReadOnly(a) => write!(f, "writing to {} which is read-only", a),
333336
DerefFunctionPointer(a) => write!(f, "accessing {} which contains a function", a),
334-
ValidationFailure(ref err) => write!(f, "type validation failed: {}", err),
337+
ValidationFailure { path: None, msg } => write!(f, "type validation failed: {}", msg),
338+
ValidationFailure { path: Some(path), msg } => {
339+
write!(f, "type validation failed at {}: {}", path, msg)
340+
}
335341
InvalidBool(b) => {
336342
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{:02x}", b)
337343
}
@@ -499,13 +505,13 @@ impl fmt::Debug for InterpError<'_> {
499505
}
500506

501507
impl InterpError<'_> {
502-
/// Some errors to string formatting even if the error is never printed.
508+
/// Some errors do string formatting even if the error is never printed.
503509
/// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
504510
/// so this method lets us detect them and `bug!` on unexpected errors.
505511
pub fn formatted_string(&self) -> bool {
506512
match self {
507513
InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_))
508-
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure(_))
514+
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationFailure { .. })
509515
| InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) => true,
510516
_ => false,
511517
}

compiler/rustc_mir/src/interpret/validity.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ use super::{
2626

2727
macro_rules! throw_validation_failure {
2828
($where:expr, { $( $what_fmt:expr ),+ } $( expected { $( $expected_fmt:expr ),+ } )?) => {{
29-
let msg = rustc_middle::ty::print::with_no_trimmed_paths(|| {
29+
let (path, msg) = rustc_middle::ty::print::with_no_trimmed_paths(|| {
3030
let mut msg = String::new();
3131
msg.push_str("encountered ");
3232
write!(&mut msg, $($what_fmt),+).unwrap();
33-
let where_ = &$where;
34-
if !where_.is_empty() {
35-
msg.push_str(" at ");
36-
write_path(&mut msg, where_);
37-
}
3833
$(
3934
msg.push_str(", but expected ");
4035
write!(&mut msg, $($expected_fmt),+).unwrap();
4136
)?
4237

43-
msg
38+
let where_ = &$where;
39+
let path = if !where_.is_empty() {
40+
let mut path = String::new();
41+
write_path(&mut path, where_);
42+
Some(path)
43+
} else {
44+
None
45+
};
46+
47+
(path, msg)
4448
});
45-
throw_ub!(ValidationFailure(msg))
49+
throw_ub!(ValidationFailure { path, msg })
4650
}};
4751
}
4852

0 commit comments

Comments
 (0)