Skip to content

Commit 353a75d

Browse files
authored
Rollup merge of rust-lang#104317 - RalfJung:ctfe-error-reporting, r=oli-obk
cleanup and dedupe CTFE and Miri error reporting It looks like most of the time, this error raised from const_prop_lint is just redundant -- it duplicates the error reported when evaluating the const-eval query. This lets us make `ConstEvalErr` private to the const_eval module which I think is a good step. The Miri change mostly replaces a `match` by `if let`, and dedupes the "this error is impossible in Miri" checks. r? ``@oli-obk`` Fixes rust-lang#75461
2 parents a53ef74 + 84ff2eb commit 353a75d

File tree

6 files changed

+106
-114
lines changed

6 files changed

+106
-114
lines changed

src/concurrency/thread.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
712712
if tcx.is_foreign_item(def_id) {
713713
throw_unsup_format!("foreign thread-local statics are not supported");
714714
}
715-
let allocation = tcx.eval_static_initializer(def_id)?;
715+
// We don't give a span -- statics don't need that, they cannot be generic or associated.
716+
let allocation = this.ctfe_query(None, |tcx| tcx.eval_static_initializer(def_id))?;
716717
let mut allocation = allocation.inner().clone();
717718
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
718719
allocation.mutability = Mutability::Mut;

src/diagnostics.rs

+97-99
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ fn prune_stacktrace<'tcx>(
146146
}
147147
}
148148

149-
/// Emit a custom diagnostic without going through the miri-engine machinery
149+
/// Emit a custom diagnostic without going through the miri-engine machinery.
150+
///
151+
/// Returns `Some` if this was regular program termination with a given exit code, `None` otherwise.
150152
pub fn report_error<'tcx, 'mir>(
151153
ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>,
152154
e: InterpErrorInfo<'tcx>,
@@ -155,106 +157,102 @@ pub fn report_error<'tcx, 'mir>(
155157

156158
let mut msg = vec![];
157159

158-
let (title, helps) = match &e.kind() {
159-
MachineStop(info) => {
160-
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
161-
use TerminationInfo::*;
162-
let title = match info {
163-
Exit(code) => return Some(*code),
164-
Abort(_) => Some("abnormal termination"),
165-
UnsupportedInIsolation(_) | Int2PtrWithStrictProvenance =>
166-
Some("unsupported operation"),
167-
StackedBorrowsUb { .. } => Some("Undefined Behavior"),
168-
Deadlock => Some("deadlock"),
169-
MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None,
170-
};
171-
#[rustfmt::skip]
172-
let helps = match info {
173-
UnsupportedInIsolation(_) =>
174-
vec![
175-
(None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")),
176-
(None, format!("or pass `-Zmiri-isolation-error=warn` to configure Miri to return an error code from isolated operations (if supported for that operation) and continue with a warning")),
177-
],
178-
StackedBorrowsUb { help, history, .. } => {
179-
let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md";
180-
msg.extend(help.clone());
181-
let mut helps = vec![
182-
(None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental")),
183-
(None, format!("see {url} for further information")),
184-
];
185-
if let Some(TagHistory {created, invalidated, protected}) = history.clone() {
186-
helps.push((Some(created.1), created.0));
187-
if let Some((msg, span)) = invalidated {
188-
helps.push((Some(span), msg));
189-
}
190-
if let Some((protector_msg, protector_span)) = protected {
191-
helps.push((Some(protector_span), protector_msg));
192-
}
160+
let (title, helps) = if let MachineStop(info) = e.kind() {
161+
let info = info.downcast_ref::<TerminationInfo>().expect("invalid MachineStop payload");
162+
use TerminationInfo::*;
163+
let title = match info {
164+
Exit(code) => return Some(*code),
165+
Abort(_) => Some("abnormal termination"),
166+
UnsupportedInIsolation(_) | Int2PtrWithStrictProvenance =>
167+
Some("unsupported operation"),
168+
StackedBorrowsUb { .. } => Some("Undefined Behavior"),
169+
Deadlock => Some("deadlock"),
170+
MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None,
171+
};
172+
#[rustfmt::skip]
173+
let helps = match info {
174+
UnsupportedInIsolation(_) =>
175+
vec![
176+
(None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")),
177+
(None, format!("or pass `-Zmiri-isolation-error=warn` to configure Miri to return an error code from isolated operations (if supported for that operation) and continue with a warning")),
178+
],
179+
StackedBorrowsUb { help, history, .. } => {
180+
let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md";
181+
msg.extend(help.clone());
182+
let mut helps = vec![
183+
(None, format!("this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental")),
184+
(None, format!("see {url} for further information")),
185+
];
186+
if let Some(TagHistory {created, invalidated, protected}) = history.clone() {
187+
helps.push((Some(created.1), created.0));
188+
if let Some((msg, span)) = invalidated {
189+
helps.push((Some(span), msg));
190+
}
191+
if let Some((protector_msg, protector_span)) = protected {
192+
helps.push((Some(protector_span), protector_msg));
193193
}
194-
helps
195194
}
196-
MultipleSymbolDefinitions { first, first_crate, second, second_crate, .. } =>
197-
vec![
198-
(Some(*first), format!("it's first defined here, in crate `{first_crate}`")),
199-
(Some(*second), format!("then it's defined here again, in crate `{second_crate}`")),
200-
],
201-
SymbolShimClashing { link_name, span } =>
202-
vec![(Some(*span), format!("the `{link_name}` symbol is defined here"))],
203-
Int2PtrWithStrictProvenance =>
204-
vec![(None, format!("use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead"))],
205-
_ => vec![],
206-
};
207-
(title, helps)
208-
}
209-
_ => {
210-
#[rustfmt::skip]
211-
let title = match e.kind() {
212-
Unsupported(_) =>
213-
"unsupported operation",
214-
UndefinedBehavior(_) =>
215-
"Undefined Behavior",
216-
ResourceExhaustion(_) =>
217-
"resource exhaustion",
218-
InvalidProgram(
219-
InvalidProgramInfo::AlreadyReported(_) |
220-
InvalidProgramInfo::Layout(..) |
221-
InvalidProgramInfo::ReferencedConstant
222-
) =>
223-
"post-monomorphization error",
224-
kind =>
225-
bug!("This error should be impossible in Miri: {kind:?}"),
226-
};
227-
#[rustfmt::skip]
228-
let helps = match e.kind() {
229-
Unsupported(
230-
UnsupportedOpInfo::ThreadLocalStatic(_) |
231-
UnsupportedOpInfo::ReadExternStatic(_) |
232-
UnsupportedOpInfo::PartialPointerOverwrite(_) |
233-
UnsupportedOpInfo::PartialPointerCopy(_) |
234-
UnsupportedOpInfo::ReadPointerAsBytes
235-
) =>
236-
panic!("Error should never be raised by Miri: {kind:?}", kind = e.kind()),
237-
Unsupported(
238-
UnsupportedOpInfo::Unsupported(_)
239-
) =>
240-
vec![(None, format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support"))],
241-
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })
242-
if ecx.machine.check_alignment == AlignmentCheck::Symbolic
243-
=>
244-
vec![
245-
(None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")),
246-
(None, format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives")),
247-
],
248-
UndefinedBehavior(_) =>
249-
vec![
250-
(None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")),
251-
(None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
252-
],
253-
InvalidProgram(_) | ResourceExhaustion(_) | MachineStop(_) =>
254-
vec![],
255-
};
256-
(Some(title), helps)
257-
}
195+
helps
196+
}
197+
MultipleSymbolDefinitions { first, first_crate, second, second_crate, .. } =>
198+
vec![
199+
(Some(*first), format!("it's first defined here, in crate `{first_crate}`")),
200+
(Some(*second), format!("then it's defined here again, in crate `{second_crate}`")),
201+
],
202+
SymbolShimClashing { link_name, span } =>
203+
vec![(Some(*span), format!("the `{link_name}` symbol is defined here"))],
204+
Int2PtrWithStrictProvenance =>
205+
vec![(None, format!("use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead"))],
206+
_ => vec![],
207+
};
208+
(title, helps)
209+
} else {
210+
#[rustfmt::skip]
211+
let title = match e.kind() {
212+
UndefinedBehavior(_) =>
213+
"Undefined Behavior",
214+
ResourceExhaustion(_) =>
215+
"resource exhaustion",
216+
Unsupported(
217+
// We list only the ones that can actually happen.
218+
UnsupportedOpInfo::Unsupported(_)
219+
) =>
220+
"unsupported operation",
221+
InvalidProgram(
222+
// We list only the ones that can actually happen.
223+
InvalidProgramInfo::AlreadyReported(_) |
224+
InvalidProgramInfo::Layout(..)
225+
) =>
226+
"post-monomorphization error",
227+
kind =>
228+
bug!("This error should be impossible in Miri: {kind:?}"),
229+
};
230+
#[rustfmt::skip]
231+
let helps = match e.kind() {
232+
Unsupported(_) =>
233+
vec![(None, format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support"))],
234+
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. })
235+
if ecx.machine.check_alignment == AlignmentCheck::Symbolic
236+
=>
237+
vec![
238+
(None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")),
239+
(None, format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives")),
240+
],
241+
UndefinedBehavior(_) =>
242+
vec![
243+
(None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")),
244+
(None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
245+
],
246+
InvalidProgram(
247+
InvalidProgramInfo::AlreadyReported(rustc_errors::ErrorGuaranteed { .. })
248+
) => {
249+
// This got already reported. No point in reporting it again.
250+
return None;
251+
}
252+
_ =>
253+
vec![],
254+
};
255+
(Some(title), helps)
258256
};
259257

260258
let stacktrace = ecx.generate_stacktrace();

src/helpers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
117117
let this = self.eval_context_ref();
118118
let instance = this.resolve_path(path);
119119
let cid = GlobalId { instance, promoted: None };
120-
let const_val = this.eval_to_allocation(cid)?;
120+
// We don't give a span -- this isn't actually used directly by the program anyway.
121+
let const_val = this.eval_global(cid, None)?;
121122
this.read_scalar(&const_val.into())
122123
}
123124

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern crate rustc_ast;
4646
extern crate rustc_middle;
4747
extern crate rustc_const_eval;
4848
extern crate rustc_data_structures;
49+
extern crate rustc_errors;
4950
extern crate rustc_hir;
5051
extern crate rustc_index;
5152
extern crate rustc_session;

tests/fail/erroneous_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl<T> PrintName<T> {
1111

1212
fn no_codegen<T>() {
1313
if false {
14-
let _ = PrintName::<T>::VOID; //~ERROR: post-monomorphization error
14+
let _ = PrintName::<T>::VOID; //~NOTE: constant
1515
}
1616
}
1717
fn main() {

tests/fail/erroneous_const.stderr

+3-12
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@ LL | const VOID: ! = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9-
error: post-monomorphization error: referenced constant has errors
9+
note: erroneous constant used
1010
--> $DIR/erroneous_const.rs:LL:CC
1111
|
1212
LL | let _ = PrintName::<T>::VOID;
13-
| ^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
14-
|
15-
= note: inside `no_codegen::<i32>` at $DIR/erroneous_const.rs:LL:CC
16-
note: inside `main` at $DIR/erroneous_const.rs:LL:CC
17-
--> $DIR/erroneous_const.rs:LL:CC
18-
|
19-
LL | no_codegen::<i32>();
20-
| ^^^^^^^^^^^^^^^^^^^
21-
22-
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
| ^^^^^^^^^^^^^^^^^^^^
2314

24-
error: aborting due to 2 previous errors
15+
error: aborting due to previous error
2516

2617
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)