From 22e7a6263b457aa6b3402aaebbf98f47da92c03d Mon Sep 17 00:00:00 2001 From: Justus K Date: Thu, 9 Jul 2020 13:16:38 +0200 Subject: [PATCH 1/3] Early exit if program doesn't contain a main fn --- src/bin/miri.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/bin/miri.rs b/src/bin/miri.rs index f22f19845c6c..73e52af4ec51 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -5,6 +5,7 @@ extern crate rustc_driver; extern crate rustc_hir; extern crate rustc_interface; extern crate rustc_session; +extern crate rustc_errors; use std::convert::TryFrom; use std::env; @@ -13,7 +14,8 @@ use std::str::FromStr; use hex::FromHexError; use log::debug; -use rustc_session::CtfeBacktrace; +use rustc_session::{CtfeBacktrace, config::ErrorOutputType}; +use rustc_errors::emitter::{HumanReadableErrorType, ColorConfig}; use rustc_driver::Compilation; use rustc_hir::def_id::LOCAL_CRATE; use rustc_middle::ty::TyCtxt; @@ -32,7 +34,12 @@ impl rustc_driver::Callbacks for MiriCompilerCalls { queries.global_ctxt().unwrap().peek_mut().enter(|tcx| { init_late_loggers(tcx); - let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect("no main function found!"); + let (entry_def_id, _) = if let Some((entry_def, x)) = tcx.entry_fn(LOCAL_CRATE) { + (entry_def, x) + } else { + let output_ty = ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(ColorConfig::Auto)); + rustc_session::early_error(output_ty, "miri can only run programs that have a main function"); + }; let mut config = self.miri_config.clone(); // Add filename to `miri` arguments. From c93fc933bd54be1f5237c5411bc70cbc0176cf39 Mon Sep 17 00:00:00 2001 From: Justus K Date: Thu, 9 Jul 2020 14:08:45 +0200 Subject: [PATCH 2/3] Add ui test for early exiting if no main --- tests/run-pass/no_main.rs | 1 + tests/run-pass/no_main.stderr | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 tests/run-pass/no_main.rs create mode 100644 tests/run-pass/no_main.stderr diff --git a/tests/run-pass/no_main.rs b/tests/run-pass/no_main.rs new file mode 100644 index 000000000000..1ae9a6a35c25 --- /dev/null +++ b/tests/run-pass/no_main.rs @@ -0,0 +1 @@ +#![no_main] diff --git a/tests/run-pass/no_main.stderr b/tests/run-pass/no_main.stderr new file mode 100644 index 000000000000..52591a8d6da3 --- /dev/null +++ b/tests/run-pass/no_main.stderr @@ -0,0 +1,2 @@ +error: miri can only run programs that have a main function + From d23e245f38f556e833ca7fc4f616b78e791b923b Mon Sep 17 00:00:00 2001 From: Justus K Date: Thu, 9 Jul 2020 17:21:09 +0200 Subject: [PATCH 3/3] Move no_main test to compile-fail --- tests/compile-fail/no_main.rs | 2 ++ tests/run-pass/no_main.rs | 1 - tests/run-pass/no_main.stderr | 2 -- 3 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 tests/compile-fail/no_main.rs delete mode 100644 tests/run-pass/no_main.rs delete mode 100644 tests/run-pass/no_main.stderr diff --git a/tests/compile-fail/no_main.rs b/tests/compile-fail/no_main.rs new file mode 100644 index 000000000000..a9e8e816828c --- /dev/null +++ b/tests/compile-fail/no_main.rs @@ -0,0 +1,2 @@ +// error-pattern: miri can only run programs that have a main function +#![no_main] diff --git a/tests/run-pass/no_main.rs b/tests/run-pass/no_main.rs deleted file mode 100644 index 1ae9a6a35c25..000000000000 --- a/tests/run-pass/no_main.rs +++ /dev/null @@ -1 +0,0 @@ -#![no_main] diff --git a/tests/run-pass/no_main.stderr b/tests/run-pass/no_main.stderr deleted file mode 100644 index 52591a8d6da3..000000000000 --- a/tests/run-pass/no_main.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: miri can only run programs that have a main function -