Skip to content

Commit 22e7a62

Browse files
committed
Early exit if program doesn't contain a main fn
1 parent b245786 commit 22e7a62

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/bin/miri.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate rustc_driver;
55
extern crate rustc_hir;
66
extern crate rustc_interface;
77
extern crate rustc_session;
8+
extern crate rustc_errors;
89

910
use std::convert::TryFrom;
1011
use std::env;
@@ -13,7 +14,8 @@ use std::str::FromStr;
1314
use hex::FromHexError;
1415
use log::debug;
1516

16-
use rustc_session::CtfeBacktrace;
17+
use rustc_session::{CtfeBacktrace, config::ErrorOutputType};
18+
use rustc_errors::emitter::{HumanReadableErrorType, ColorConfig};
1719
use rustc_driver::Compilation;
1820
use rustc_hir::def_id::LOCAL_CRATE;
1921
use rustc_middle::ty::TyCtxt;
@@ -32,7 +34,12 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
3234

3335
queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
3436
init_late_loggers(tcx);
35-
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect("no main function found!");
37+
let (entry_def_id, _) = if let Some((entry_def, x)) = tcx.entry_fn(LOCAL_CRATE) {
38+
(entry_def, x)
39+
} else {
40+
let output_ty = ErrorOutputType::HumanReadable(HumanReadableErrorType::Default(ColorConfig::Auto));
41+
rustc_session::early_error(output_ty, "miri can only run programs that have a main function");
42+
};
3643
let mut config = self.miri_config.clone();
3744

3845
// Add filename to `miri` arguments.

0 commit comments

Comments
 (0)