Skip to content

Commit ade99c3

Browse files
committed
Auto merge of #1473 - Stupremee:no-ice-if-no-main-fn, r=oli-obk
Early exit if program doesn't contain a main function Resolves #1452
2 parents eb5ff17 + d23e245 commit ade99c3

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-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.

tests/compile-fail/no_main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// error-pattern: miri can only run programs that have a main function
2+
#![no_main]

0 commit comments

Comments
 (0)