Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc7ec6b

Browse files
committedMar 31, 2016
librustc: add {span_,}bug! macros
... 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.
1 parent 3399d19 commit fc7ec6b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
 

‎src/librustc/macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ macro_rules! enum_from_u32 {
4444
}
4545
}
4646
}
47+
48+
#[macro_export]
49+
macro_rules! bug {
50+
() => ( bug!("impossible case reached") );
51+
($($message:tt)*) => ({
52+
$crate::session::bug_fmt(file!(), line!(), format_args!($($message)*))
53+
})
54+
}
55+
56+
#[macro_export]
57+
macro_rules! span_bug {
58+
($span:expr, $($message:tt)*) => ({
59+
$crate::session::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
60+
})
61+
}

‎src/librustc/session/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use lint;
1212
use middle::cstore::CrateStore;
1313
use middle::dependency_format;
1414
use session::search_paths::PathKind;
15+
use ty::tls;
1516
use util::nodemap::{NodeMap, FnvHashMap};
1617
use mir::transform as mir_pass;
1718

@@ -35,6 +36,7 @@ use std::cell::{Cell, RefCell};
3536
use std::collections::{HashMap, HashSet};
3637
use std::env;
3738
use std::rc::Rc;
39+
use std::fmt;
3840

3941
pub mod config;
4042
pub mod filesearch;
@@ -541,3 +543,35 @@ pub fn compile_result_from_err_count(err_count: usize) -> CompileResult {
541543
Err(err_count)
542544
}
543545
}
546+
547+
#[cold]
548+
#[inline(never)]
549+
pub fn bug_fmt(file: &'static str, line: u32, args: fmt::Arguments) -> ! {
550+
// this wrapper mostly exists so I don't have to write a fully
551+
// qualified path of None::<Span> inside the bug!() macro defintion
552+
opt_span_bug_fmt(file, line, None::<Span>, args);
553+
}
554+
555+
#[cold]
556+
#[inline(never)]
557+
pub fn span_bug_fmt<S: Into<MultiSpan>>(file: &'static str,
558+
line: u32,
559+
span: S,
560+
args: fmt::Arguments) -> ! {
561+
opt_span_bug_fmt(file, line, Some(span), args);
562+
}
563+
564+
fn opt_span_bug_fmt<S: Into<MultiSpan>>(file: &'static str,
565+
line: u32,
566+
span: Option<S>,
567+
args: fmt::Arguments) -> ! {
568+
tls::with_opt(move |tcx| {
569+
let msg = format!("{}:{}: {}", file, line, args);
570+
match (tcx, span) {
571+
(Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
572+
(Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
573+
(None, _) => panic!(msg)
574+
}
575+
});
576+
unreachable!();
577+
}

0 commit comments

Comments
 (0)
Please sign in to comment.