Skip to content

Commit

Permalink
librustc: add {span_,}bug! macros
Browse files Browse the repository at this point in the history
... as single "internal compiler error" entry point.

The macros pass `file!()`, `line!()` and `format_args!(...)` on to a
cold, never-inlined function, ultimately calling `bug()` or `span_bug()`
on the `Handler` from `session::diagnostic()` via the tcx in tls or,
failing that, panicking directly.
  • Loading branch information
ben0x539 committed Mar 31, 2016
1 parent 3399d19 commit fc7ec6b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/librustc/macros.rs
Expand Up @@ -44,3 +44,18 @@ macro_rules! enum_from_u32 {
}
}
}

#[macro_export]
macro_rules! bug {
() => ( bug!("impossible case reached") );
($($message:tt)*) => ({
$crate::session::bug_fmt(file!(), line!(), format_args!($($message)*))
})
}

#[macro_export]
macro_rules! span_bug {
($span:expr, $($message:tt)*) => ({
$crate::session::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
})
}
34 changes: 34 additions & 0 deletions src/librustc/session/mod.rs
Expand Up @@ -12,6 +12,7 @@ use lint;
use middle::cstore::CrateStore;
use middle::dependency_format;
use session::search_paths::PathKind;
use ty::tls;
use util::nodemap::{NodeMap, FnvHashMap};
use mir::transform as mir_pass;

Expand All @@ -35,6 +36,7 @@ use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::env;
use std::rc::Rc;
use std::fmt;

pub mod config;
pub mod filesearch;
Expand Down Expand Up @@ -541,3 +543,35 @@ pub fn compile_result_from_err_count(err_count: usize) -> CompileResult {
Err(err_count)
}
}

#[cold]
#[inline(never)]
pub fn bug_fmt(file: &'static str, line: u32, args: fmt::Arguments) -> ! {
// this wrapper mostly exists so I don't have to write a fully
// qualified path of None::<Span> inside the bug!() macro defintion
opt_span_bug_fmt(file, line, None::<Span>, args);
}

#[cold]
#[inline(never)]
pub fn span_bug_fmt<S: Into<MultiSpan>>(file: &'static str,
line: u32,
span: S,
args: fmt::Arguments) -> ! {
opt_span_bug_fmt(file, line, Some(span), args);
}

fn opt_span_bug_fmt<S: Into<MultiSpan>>(file: &'static str,
line: u32,
span: Option<S>,
args: fmt::Arguments) -> ! {
tls::with_opt(move |tcx| {
let msg = format!("{}:{}: {}", file, line, args);
match (tcx, span) {
(Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
(Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
(None, _) => panic!(msg)
}
});
unreachable!();
}

0 comments on commit fc7ec6b

Please sign in to comment.