Skip to content

Commit

Permalink
librustc_save_analysis: use bug!(), span_bug!()
Browse files Browse the repository at this point in the history
  • Loading branch information
ben0x539 committed Mar 31, 2016
1 parent d05f726 commit 37f8429
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/librustc_save_analysis/data.rs
Expand Up @@ -22,11 +22,11 @@ use syntax::codemap::Span;

#[macro_export]
macro_rules! down_cast_data {
($id:ident, $kind:ident, $this:ident, $sp:expr) => {
($id:ident, $kind:ident, $sp:expr) => {
let $id = if let super::Data::$kind(data) = $id {
data
} else {
$this.sess.span_bug($sp, &format!("unexpected data kind: {:?}", $id));
span_bug!($sp, "unexpected data kind: {:?}", $id);
}
};
}
Expand Down
55 changes: 26 additions & 29 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -51,11 +51,11 @@ use super::span_utils::SpanUtils;
use super::recorder;

macro_rules! down_cast_data {
($id:ident, $kind:ident, $this:ident, $sp:expr) => {
($id:ident, $kind:ident, $sp:expr) => {
let $id = if let super::Data::$kind(data) = $id {
data
} else {
$this.sess.span_bug($sp, &format!("unexpected data kind: {:?}", $id));
span_bug!($sp, "unexpected data kind: {:?}", $id);
}
};
}
Expand Down Expand Up @@ -271,8 +271,7 @@ where D: Dump
// looks up anything, not just a type
fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
if !self.tcx.def_map.borrow().contains_key(&ref_id) {
self.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
ref_id));
bug!("def_map has no key for {} in lookup_type_ref", ref_id);
}
let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
match def {
Expand All @@ -294,9 +293,9 @@ where D: Dump

let def_map = self.tcx.def_map.borrow();
if !def_map.contains_key(&ref_id) {
self.sess.span_bug(span,
&format!("def_map has no key for {} in lookup_def_kind",
ref_id));
span_bug!(span,
"def_map has no key for {} in lookup_def_kind",
ref_id);
}
let def = def_map.get(&ref_id).unwrap().full_def();
match def {
Expand Down Expand Up @@ -347,8 +346,9 @@ where D: Dump
Def::Method(..) |
Def::PrimTy(_) |
Def::Err => {
self.sess.span_bug(span,
&format!("process_def_kind for unexpected item: {:?}", def));
span_bug!(span,
"process_def_kind for unexpected item: {:?}",
def);
}
}
}
Expand Down Expand Up @@ -480,7 +480,7 @@ where D: Dump
ty_params: &ast::Generics,
body: &ast::Block) {
if let Some(fn_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(fn_data, FunctionData, self, item.span);
down_cast_data!(fn_data, FunctionData, item.span);
if !self.span.filter_generated(Some(fn_data.span), item.span) {
self.dumper.function(item.span, fn_data.clone().normalize(&self.tcx));
}
Expand All @@ -502,7 +502,7 @@ where D: Dump

fn process_static_or_const_item(&mut self, item: &ast::Item, typ: &ast::Ty, expr: &ast::Expr) {
if let Some(var_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(var_data, VariableData, self, item.span);
down_cast_data!(var_data, VariableData, item.span);
if !self.span.filter_generated(Some(var_data.span), item.span) {
let mut var_data = var_data;
var_data.scope = normalize_node_id(&self.tcx, var_data.scope) as u32;
Expand Down Expand Up @@ -578,7 +578,7 @@ where D: Dump
None => return,
Some(data) => data,
};
down_cast_data!(enum_data, EnumData, self, item.span);
down_cast_data!(enum_data, EnumData, item.span);
let normalized = enum_data.clone().normalize(&self.tcx);
if !self.span.filter_generated(Some(normalized.span), item.span) {
self.dumper.enum_data(item.span, normalized);
Expand Down Expand Up @@ -638,7 +638,7 @@ where D: Dump
impl_items: &[ast::ImplItem]) {
let mut has_self_ref = false;
if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(impl_data, ImplData, self, item.span);
down_cast_data!(impl_data, ImplData, item.span);
if let Some(ref self_ref) = impl_data.self_ref {
has_self_ref = true;
if !self.span.filter_generated(Some(self_ref.span), item.span) {
Expand Down Expand Up @@ -734,7 +734,7 @@ where D: Dump
// `item` is the module in question, represented as an item.
fn process_mod(&mut self, item: &ast::Item) {
if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(mod_data, ModData, self, item.span);
down_cast_data!(mod_data, ModData, item.span);
if !self.span.filter_generated(Some(mod_data.span), item.span) {
self.dumper.mod_data(mod_data.normalize(&self.tcx));
}
Expand All @@ -750,10 +750,9 @@ where D: Dump
let path_data = match path_data {
Some(pd) => pd,
None => {
self.tcx.sess.span_bug(path.span,
&format!("Unexpected def kind while looking up path in \
`{}`",
self.span.snippet(path.span)))
span_bug!(path.span,
"Unexpected def kind while looking up path in `{}`",
self.span.snippet(path.span))
}
};

Expand Down Expand Up @@ -807,8 +806,7 @@ where D: Dump
}
}
_ => {
self.sess.span_bug(path.span,
&format!("Unexpected data: {:?}", path_data));
span_bug!(path.span, "Unexpected data: {:?}", path_data);
}
}

Expand Down Expand Up @@ -844,7 +842,7 @@ where D: Dump
self.write_sub_paths_truncated(path, false);

if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
down_cast_data!(struct_lit_data, TypeRefData, self, ex.span);
down_cast_data!(struct_lit_data, TypeRefData, ex.span);
if !self.span.filter_generated(Some(struct_lit_data.span), ex.span) {
self.dumper.type_ref(ex.span, struct_lit_data.normalize(&self.tcx));
}
Expand All @@ -869,7 +867,7 @@ where D: Dump

fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) {
if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
down_cast_data!(mcd, MethodCallData, self, ex.span);
down_cast_data!(mcd, MethodCallData, ex.span);
if !self.span.filter_generated(Some(mcd.span), ex.span) {
self.dumper.method_call(ex.span, mcd.normalize(&self.tcx));
}
Expand Down Expand Up @@ -1234,7 +1232,7 @@ impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> {
self.visit_expr(&sub_ex);

if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
down_cast_data!(field_data, VariableRefData, self, ex.span);
down_cast_data!(field_data, VariableRefData, ex.span);
if !self.span.filter_generated(Some(field_data.span), ex.span) {
self.dumper.variable_ref(ex.span, field_data.normalize(&self.tcx));
}
Expand All @@ -1258,9 +1256,9 @@ impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> {
}
}
ty::TyTuple(_) => {}
_ => self.sess.span_bug(ex.span,
&format!("Expected struct or tuple type, found {:?}",
ty)),
_ => span_bug!(ex.span,
"Expected struct or tuple type, found {:?}",
ty),
}
}
ast::ExprKind::Closure(_, ref decl, ref body) => {
Expand Down Expand Up @@ -1302,7 +1300,7 @@ impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> {

fn visit_mac(&mut self, mac: &ast::Mac) {
// These shouldn't exist in the AST at this point, log a span bug.
self.sess.span_bug(mac.span, "macro invocation should have been expanded out of AST");
span_bug!(mac.span, "macro invocation should have been expanded out of AST");
}

fn visit_pat(&mut self, p: &ast::Pat) {
Expand All @@ -1325,8 +1323,7 @@ impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> {
for &(id, ref p, immut, ref_kind) in &collector.collected_paths {
let def_map = self.tcx.def_map.borrow();
if !def_map.contains_key(&id) {
self.sess.span_bug(p.span,
&format!("def_map has no key for {} in visit_arm", id));
span_bug!(p.span, "def_map has no key for {} in visit_arm", id);
}
let def = def_map.get(&id).unwrap().full_def();
match def {
Expand Down
49 changes: 22 additions & 27 deletions src/librustc_save_analysis/lib.rs
Expand Up @@ -22,7 +22,7 @@
#![feature(rustc_private)]
#![feature(staged_api)]

extern crate rustc;
#[macro_use] extern crate rustc;
extern crate rustc_front;

#[macro_use] extern crate log;
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
_ => {
// FIXME
unimplemented!();
bug!();
}
}
}
Expand Down Expand Up @@ -292,21 +292,19 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
result
}
_ => {
self.tcx.sess.span_bug(span,
&format!("Container {:?} for method {} not \
an impl?",
impl_id,
id));
span_bug!(span,
"Container {:?} for method {} not an impl?",
impl_id,
id);
}
}
}
r => {
self.tcx.sess.span_bug(span,
&format!("Container {:?} for method {} is not a node \
item {:?}",
impl_id,
id,
r));
span_bug!(span,
"Container {:?} for method {} is not a node item {:?}",
impl_id,
id,
r);
}
},
None => match self.tcx.trait_of_item(self.tcx.map.local_def_id(id)) {
Expand All @@ -316,18 +314,17 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
format!("::{}", self.tcx.item_path_str(def_id))
}
r => {
self.tcx.sess.span_bug(span,
&format!("Could not find container {:?} for \
method {}, got {:?}",
def_id,
id,
r));
span_bug!(span,
"Could not find container {:?} for \
method {}, got {:?}",
def_id,
id,
r);
}
}
}
None => {
self.tcx.sess.span_bug(span,
&format!("Could not find container for method {}", id));
span_bug!(span, "Could not find container for method {}", id);
}
},
};
Expand Down Expand Up @@ -443,16 +440,15 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
}
_ => {
// FIXME
unimplemented!();
bug!();
}
}
}

pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Data> {
let def_map = self.tcx.def_map.borrow();
if !def_map.contains_key(&id) {
self.tcx.sess.span_bug(path.span,
&format!("def_map has no key for {} in visit_expr", id));
span_bug!(path.span, "def_map has no key for {} in visit_expr", id);
}
let def = def_map.get(&id).unwrap().full_def();
let sub_span = self.span_utils.span_for_last_ident(path.span);
Expand Down Expand Up @@ -618,13 +614,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {

pub fn get_data_for_id(&self, _id: &NodeId) -> Data {
// FIXME
unimplemented!();
bug!();
}

fn lookup_ref_id(&self, ref_id: NodeId) -> Option<DefId> {
if !self.tcx.def_map.borrow().contains_key(&ref_id) {
self.tcx.sess.bug(&format!("def_map has no key for {} in lookup_type_ref",
ref_id));
bug!("def_map has no key for {} in lookup_type_ref", ref_id);
}
let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
match def {
Expand Down
26 changes: 13 additions & 13 deletions src/librustc_save_analysis/span_utils.rs
Expand Up @@ -223,12 +223,12 @@ impl<'a> SpanUtils<'a> {
}
if bracket_count != 0 {
let loc = self.sess.codemap().lookup_char_pos(span.lo);
self.sess.span_bug(span,
&format!("Mis-counted brackets when breaking path? Parsing '{}' \
in {}, line {}",
self.snippet(span),
loc.file.name,
loc.line));
span_bug!(span,
"Mis-counted brackets when breaking path? Parsing '{}' \
in {}, line {}",
self.snippet(span),
loc.file.name,
loc.line);
}
if result.is_none() && prev.tok.is_ident() && bracket_count == 0 {
return self.make_sub_span(span, Some(prev.sp));
Expand Down Expand Up @@ -256,12 +256,12 @@ impl<'a> SpanUtils<'a> {
return vec!();
}
let loc = self.sess.codemap().lookup_char_pos(span.lo);
self.sess.span_bug(span,
&format!("Mis-counted brackets when breaking path? \
Parsing '{}' in {}, line {}",
self.snippet(span),
loc.file.name,
loc.line));
span_bug!(span,
"Mis-counted brackets when breaking path? \
Parsing '{}' in {}, line {}",
self.snippet(span),
loc.file.name,
loc.line);
}
return result
}
Expand Down Expand Up @@ -374,7 +374,7 @@ impl<'a> SpanUtils<'a> {
loc.line);
self.err_count.set(self.err_count.get() + 1);
if self.err_count.get() > 1000 {
self.sess.bug("span errors reached 1000, giving up");
bug!("span errors reached 1000, giving up");
}
}

Expand Down

0 comments on commit 37f8429

Please sign in to comment.