From 84ff2eb20f2456cb5f0bcfb9a5d1406378386327 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 15 Nov 2022 12:06:20 +0100 Subject: [PATCH] cleanup and dedupe CTFE and Miri error reporting --- src/concurrency/thread.rs | 3 +- src/diagnostics.rs | 196 +++++++++++++++--------------- src/helpers.rs | 3 +- src/lib.rs | 1 + tests/fail/erroneous_const.rs | 2 +- tests/fail/erroneous_const.stderr | 15 +-- 6 files changed, 106 insertions(+), 114 deletions(-) diff --git a/src/concurrency/thread.rs b/src/concurrency/thread.rs index ac5dcbf0f4f2f..8fbee9a352294 100644 --- a/src/concurrency/thread.rs +++ b/src/concurrency/thread.rs @@ -712,7 +712,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { if tcx.is_foreign_item(def_id) { throw_unsup_format!("foreign thread-local statics are not supported"); } - let allocation = tcx.eval_static_initializer(def_id)?; + // We don't give a span -- statics don't need that, they cannot be generic or associated. + let allocation = this.ctfe_query(None, |tcx| tcx.eval_static_initializer(def_id))?; let mut allocation = allocation.inner().clone(); // This allocation will be deallocated when the thread dies, so it is not in read-only memory. allocation.mutability = Mutability::Mut; diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 0cfa3812e400d..7658cea10f9f8 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -146,7 +146,9 @@ fn prune_stacktrace<'tcx>( } } -/// Emit a custom diagnostic without going through the miri-engine machinery +/// Emit a custom diagnostic without going through the miri-engine machinery. +/// +/// Returns `Some` if this was regular program termination with a given exit code, `None` otherwise. pub fn report_error<'tcx, 'mir>( ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>, e: InterpErrorInfo<'tcx>, @@ -155,106 +157,102 @@ pub fn report_error<'tcx, 'mir>( let mut msg = vec![]; - let (title, helps) = match &e.kind() { - MachineStop(info) => { - let info = info.downcast_ref::().expect("invalid MachineStop payload"); - use TerminationInfo::*; - let title = match info { - Exit(code) => return Some(*code), - Abort(_) => Some("abnormal termination"), - UnsupportedInIsolation(_) | Int2PtrWithStrictProvenance => - Some("unsupported operation"), - StackedBorrowsUb { .. } => Some("Undefined Behavior"), - Deadlock => Some("deadlock"), - MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None, - }; - #[rustfmt::skip] - let helps = match info { - UnsupportedInIsolation(_) => - vec![ - (None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")), - (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")), - ], - StackedBorrowsUb { help, history, .. } => { - let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md"; - msg.extend(help.clone()); - let mut helps = vec![ - (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")), - (None, format!("see {url} for further information")), - ]; - if let Some(TagHistory {created, invalidated, protected}) = history.clone() { - helps.push((Some(created.1), created.0)); - if let Some((msg, span)) = invalidated { - helps.push((Some(span), msg)); - } - if let Some((protector_msg, protector_span)) = protected { - helps.push((Some(protector_span), protector_msg)); - } + let (title, helps) = if let MachineStop(info) = e.kind() { + let info = info.downcast_ref::().expect("invalid MachineStop payload"); + use TerminationInfo::*; + let title = match info { + Exit(code) => return Some(*code), + Abort(_) => Some("abnormal termination"), + UnsupportedInIsolation(_) | Int2PtrWithStrictProvenance => + Some("unsupported operation"), + StackedBorrowsUb { .. } => Some("Undefined Behavior"), + Deadlock => Some("deadlock"), + MultipleSymbolDefinitions { .. } | SymbolShimClashing { .. } => None, + }; + #[rustfmt::skip] + let helps = match info { + UnsupportedInIsolation(_) => + vec![ + (None, format!("pass the flag `-Zmiri-disable-isolation` to disable isolation;")), + (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")), + ], + StackedBorrowsUb { help, history, .. } => { + let url = "https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md"; + msg.extend(help.clone()); + let mut helps = vec![ + (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")), + (None, format!("see {url} for further information")), + ]; + if let Some(TagHistory {created, invalidated, protected}) = history.clone() { + helps.push((Some(created.1), created.0)); + if let Some((msg, span)) = invalidated { + helps.push((Some(span), msg)); + } + if let Some((protector_msg, protector_span)) = protected { + helps.push((Some(protector_span), protector_msg)); } - helps } - MultipleSymbolDefinitions { first, first_crate, second, second_crate, .. } => - vec![ - (Some(*first), format!("it's first defined here, in crate `{first_crate}`")), - (Some(*second), format!("then it's defined here again, in crate `{second_crate}`")), - ], - SymbolShimClashing { link_name, span } => - vec![(Some(*span), format!("the `{link_name}` symbol is defined here"))], - Int2PtrWithStrictProvenance => - 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"))], - _ => vec![], - }; - (title, helps) - } - _ => { - #[rustfmt::skip] - let title = match e.kind() { - Unsupported(_) => - "unsupported operation", - UndefinedBehavior(_) => - "Undefined Behavior", - ResourceExhaustion(_) => - "resource exhaustion", - InvalidProgram( - InvalidProgramInfo::AlreadyReported(_) | - InvalidProgramInfo::Layout(..) | - InvalidProgramInfo::ReferencedConstant - ) => - "post-monomorphization error", - kind => - bug!("This error should be impossible in Miri: {kind:?}"), - }; - #[rustfmt::skip] - let helps = match e.kind() { - Unsupported( - UnsupportedOpInfo::ThreadLocalStatic(_) | - UnsupportedOpInfo::ReadExternStatic(_) | - UnsupportedOpInfo::PartialPointerOverwrite(_) | - UnsupportedOpInfo::PartialPointerCopy(_) | - UnsupportedOpInfo::ReadPointerAsBytes - ) => - panic!("Error should never be raised by Miri: {kind:?}", kind = e.kind()), - Unsupported( - UnsupportedOpInfo::Unsupported(_) - ) => - 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"))], - UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. }) - if ecx.machine.check_alignment == AlignmentCheck::Symbolic - => - vec![ - (None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")), - (None, format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives")), - ], - UndefinedBehavior(_) => - vec![ - (None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")), - (None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")), - ], - InvalidProgram(_) | ResourceExhaustion(_) | MachineStop(_) => - vec![], - }; - (Some(title), helps) - } + helps + } + MultipleSymbolDefinitions { first, first_crate, second, second_crate, .. } => + vec![ + (Some(*first), format!("it's first defined here, in crate `{first_crate}`")), + (Some(*second), format!("then it's defined here again, in crate `{second_crate}`")), + ], + SymbolShimClashing { link_name, span } => + vec![(Some(*span), format!("the `{link_name}` symbol is defined here"))], + Int2PtrWithStrictProvenance => + 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"))], + _ => vec![], + }; + (title, helps) + } else { + #[rustfmt::skip] + let title = match e.kind() { + UndefinedBehavior(_) => + "Undefined Behavior", + ResourceExhaustion(_) => + "resource exhaustion", + Unsupported( + // We list only the ones that can actually happen. + UnsupportedOpInfo::Unsupported(_) + ) => + "unsupported operation", + InvalidProgram( + // We list only the ones that can actually happen. + InvalidProgramInfo::AlreadyReported(_) | + InvalidProgramInfo::Layout(..) + ) => + "post-monomorphization error", + kind => + bug!("This error should be impossible in Miri: {kind:?}"), + }; + #[rustfmt::skip] + let helps = match e.kind() { + Unsupported(_) => + 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"))], + UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. }) + if ecx.machine.check_alignment == AlignmentCheck::Symbolic + => + vec![ + (None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")), + (None, format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives")), + ], + UndefinedBehavior(_) => + vec![ + (None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")), + (None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")), + ], + InvalidProgram( + InvalidProgramInfo::AlreadyReported(rustc_errors::ErrorGuaranteed { .. }) + ) => { + // This got already reported. No point in reporting it again. + return None; + } + _ => + vec![], + }; + (Some(title), helps) }; let stacktrace = ecx.generate_stacktrace(); diff --git a/src/helpers.rs b/src/helpers.rs index f98727186c48d..1fdf3e52a8b65 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -117,7 +117,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let this = self.eval_context_ref(); let instance = this.resolve_path(path); let cid = GlobalId { instance, promoted: None }; - let const_val = this.eval_to_allocation(cid)?; + // We don't give a span -- this isn't actually used directly by the program anyway. + let const_val = this.eval_global(cid, None)?; this.read_scalar(&const_val.into()) } diff --git a/src/lib.rs b/src/lib.rs index 8028ce7535488..66df0d737c087 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,6 +46,7 @@ extern crate rustc_ast; extern crate rustc_middle; extern crate rustc_const_eval; extern crate rustc_data_structures; +extern crate rustc_errors; extern crate rustc_hir; extern crate rustc_index; extern crate rustc_session; diff --git a/tests/fail/erroneous_const.rs b/tests/fail/erroneous_const.rs index d14998ccba269..d37837c71931e 100644 --- a/tests/fail/erroneous_const.rs +++ b/tests/fail/erroneous_const.rs @@ -11,7 +11,7 @@ impl PrintName { fn no_codegen() { if false { - let _ = PrintName::::VOID; //~ERROR: post-monomorphization error + let _ = PrintName::::VOID; //~NOTE: constant } } fn main() { diff --git a/tests/fail/erroneous_const.stderr b/tests/fail/erroneous_const.stderr index 8138d69f4031e..c32ebf67a1167 100644 --- a/tests/fail/erroneous_const.stderr +++ b/tests/fail/erroneous_const.stderr @@ -6,21 +6,12 @@ LL | const VOID: ! = panic!(); | = 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) -error: post-monomorphization error: referenced constant has errors +note: erroneous constant used --> $DIR/erroneous_const.rs:LL:CC | LL | let _ = PrintName::::VOID; - | ^^^^^^^^^^^^^^^^^^^^ referenced constant has errors - | - = note: inside `no_codegen::` at $DIR/erroneous_const.rs:LL:CC -note: inside `main` at $DIR/erroneous_const.rs:LL:CC - --> $DIR/erroneous_const.rs:LL:CC - | -LL | no_codegen::(); - | ^^^^^^^^^^^^^^^^^^^ - -note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + | ^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0080`.