Skip to content

Commit 1af79cf

Browse files
committed
Auto merge of #32562 - ben0x539:bug-macro, r=nikomatsakis
librustc: Add bug!(), bug_span!() macros as unified entry points for internal compiler errors The macros pass `file!()`, `line!()` and `format_args!(...)` on to a cold, never-inlined function, ultimately calling `session::{span_,}bug_fmt` via the tcx in tls or, failing that, panicking directly. cc @eddyb r? @nikomatsakis
2 parents f2285bd + 33cc0ed commit 1af79cf

File tree

161 files changed

+1326
-1451
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+1326
-1451
lines changed

src/librustc/cfg/construct.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,11 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
583583
return *l;
584584
}
585585
}
586-
self.tcx.sess.span_bug(expr.span,
587-
&format!("no loop scope for id {}", loop_id));
586+
span_bug!(expr.span, "no loop scope for id {}", loop_id);
588587
}
589588

590589
r => {
591-
self.tcx.sess.span_bug(expr.span,
592-
&format!("bad entry `{:?}` in def_map for label", r));
590+
span_bug!(expr.span, "bad entry `{:?}` in def_map for label", r);
593591
}
594592
}
595593
}

src/librustc/dep_graph/edges.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl DepGraphEdges {
122122
{
123123
match self.current_node() {
124124
Some(open_node) => self.add_edge_from_open_node(open_node, op),
125-
None => panic!("no current node, cannot add edge into dependency graph")
125+
None => bug!("no current node, cannot add edge into dependency graph")
126126
}
127127
}
128128

src/librustc/dep_graph/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl DepGraphThreadData {
148148

149149
// Outline this too.
150150
fn invalid_message(&self, string: &str) {
151-
panic!("{}; see src/librustc/dep_graph/README.md for more information", string)
151+
bug!("{}; see src/librustc/dep_graph/README.md for more information", string)
152152
}
153153
}
154154

src/librustc/front/map/blocks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,21 @@ impl<'a> FnLikeNode<'a> {
231231
span: i.span,
232232
attrs: &i.attrs,
233233
}),
234-
_ => panic!("item FnLikeNode that is not fn-like"),
234+
_ => bug!("item FnLikeNode that is not fn-like"),
235235
},
236236
map::NodeTraitItem(ti) => match ti.node {
237237
ast::MethodTraitItem(ref sig, Some(ref body)) => {
238238
method(ti.id, ti.name, sig, None, body, ti.span, &ti.attrs)
239239
}
240-
_ => panic!("trait method FnLikeNode that is not fn-like"),
240+
_ => bug!("trait method FnLikeNode that is not fn-like"),
241241
},
242242
map::NodeImplItem(ii) => {
243243
match ii.node {
244244
ast::ImplItemKind::Method(ref sig, ref body) => {
245245
method(ii.id, ii.name, sig, Some(ii.vis), body, ii.span, &ii.attrs)
246246
}
247247
_ => {
248-
panic!("impl method FnLikeNode that is not fn-like")
248+
bug!("impl method FnLikeNode that is not fn-like")
249249
}
250250
}
251251
}
@@ -256,9 +256,9 @@ impl<'a> FnLikeNode<'a> {
256256
e.id,
257257
e.span,
258258
e.attrs.as_attr_slice())),
259-
_ => panic!("expr FnLikeNode that is not fn-like"),
259+
_ => bug!("expr FnLikeNode that is not fn-like"),
260260
},
261-
_ => panic!("other FnLikeNode that is not fn-like"),
261+
_ => bug!("other FnLikeNode that is not fn-like"),
262262
}
263263
}
264264
}

src/librustc/front/map/mod.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ impl<'ast> Map<'ast> {
335335
return self.opt_local_def_id(id)
336336
.map(|def_id| DepNode::Hir(def_id))
337337
.unwrap_or_else(|| {
338-
panic!("Walking parents from `{}` \
339-
led to `NotPresent` at `{}`",
340-
id0, id)
338+
bug!("Walking parents from `{}` \
339+
led to `NotPresent` at `{}`",
340+
id0, id)
341341
}),
342342
}
343343
}
@@ -363,8 +363,8 @@ impl<'ast> Map<'ast> {
363363

364364
pub fn local_def_id(&self, node: NodeId) -> DefId {
365365
self.opt_local_def_id(node).unwrap_or_else(|| {
366-
panic!("local_def_id: no entry for `{}`, which has a map of `{:?}`",
367-
node, self.find_entry(node))
366+
bug!("local_def_id: no entry for `{}`, which has a map of `{:?}`",
367+
node, self.find_entry(node))
368368
})
369369
}
370370

@@ -402,7 +402,7 @@ impl<'ast> Map<'ast> {
402402
pub fn get(&self, id: NodeId) -> Node<'ast> {
403403
match self.find(id) {
404404
Some(node) => node, // read recorded by `find`
405-
None => panic!("couldn't find node id {} in the AST map", id)
405+
None => bug!("couldn't find node id {} in the AST map", id)
406406
}
407407
}
408408

@@ -576,22 +576,22 @@ impl<'ast> Map<'ast> {
576576
self.read(id); // reveals some of the content of a node
577577
abi
578578
}
579-
None => panic!("expected foreign mod or inlined parent, found {}",
579+
None => bug!("expected foreign mod or inlined parent, found {}",
580580
self.node_to_string(parent))
581581
}
582582
}
583583

584584
pub fn expect_item(&self, id: NodeId) -> &'ast Item {
585585
match self.find(id) { // read recorded by `find`
586586
Some(NodeItem(item)) => item,
587-
_ => panic!("expected item, found {}", self.node_to_string(id))
587+
_ => bug!("expected item, found {}", self.node_to_string(id))
588588
}
589589
}
590590

591591
pub fn expect_trait_item(&self, id: NodeId) -> &'ast TraitItem {
592592
match self.find(id) {
593593
Some(NodeTraitItem(item)) => item,
594-
_ => panic!("expected trait item, found {}", self.node_to_string(id))
594+
_ => bug!("expected trait item, found {}", self.node_to_string(id))
595595
}
596596
}
597597

@@ -600,38 +600,38 @@ impl<'ast> Map<'ast> {
600600
Some(NodeItem(i)) => {
601601
match i.node {
602602
ItemStruct(ref struct_def, _) => struct_def,
603-
_ => panic!("struct ID bound to non-struct")
603+
_ => bug!("struct ID bound to non-struct")
604604
}
605605
}
606606
Some(NodeVariant(variant)) => {
607607
if variant.node.data.is_struct() {
608608
&variant.node.data
609609
} else {
610-
panic!("struct ID bound to enum variant that isn't struct-like")
610+
bug!("struct ID bound to enum variant that isn't struct-like")
611611
}
612612
}
613-
_ => panic!(format!("expected struct, found {}", self.node_to_string(id))),
613+
_ => bug!("expected struct, found {}", self.node_to_string(id)),
614614
}
615615
}
616616

617617
pub fn expect_variant(&self, id: NodeId) -> &'ast Variant {
618618
match self.find(id) {
619619
Some(NodeVariant(variant)) => variant,
620-
_ => panic!(format!("expected variant, found {}", self.node_to_string(id))),
620+
_ => bug!("expected variant, found {}", self.node_to_string(id)),
621621
}
622622
}
623623

624624
pub fn expect_foreign_item(&self, id: NodeId) -> &'ast ForeignItem {
625625
match self.find(id) {
626626
Some(NodeForeignItem(item)) => item,
627-
_ => panic!("expected foreign item, found {}", self.node_to_string(id))
627+
_ => bug!("expected foreign item, found {}", self.node_to_string(id))
628628
}
629629
}
630630

631631
pub fn expect_expr(&self, id: NodeId) -> &'ast Expr {
632632
match self.find(id) { // read recorded by find
633633
Some(NodeExpr(expr)) => expr,
634-
_ => panic!("expected expr, found {}", self.node_to_string(id))
634+
_ => bug!("expected expr, found {}", self.node_to_string(id))
635635
}
636636
}
637637

@@ -656,7 +656,7 @@ impl<'ast> Map<'ast> {
656656
NodeLocal(&Pat { node: PatKind::Ident(_,l,_), .. }) => {
657657
PathName(l.node.name)
658658
},
659-
_ => panic!("no path elem for {:?}", node)
659+
_ => bug!("no path elem for {:?}", node)
660660
}
661661
}
662662

@@ -773,7 +773,7 @@ impl<'ast> Map<'ast> {
773773
pub fn span(&self, id: NodeId) -> Span {
774774
self.read(id); // reveals span from node
775775
self.opt_span(id)
776-
.unwrap_or_else(|| panic!("AstMap.span: could not find span for id {:?}", id))
776+
.unwrap_or_else(|| bug!("AstMap.span: could not find span for id {:?}", id))
777777
}
778778

779779
pub fn span_if_local(&self, id: DefId) -> Option<Span> {
@@ -1019,12 +1019,12 @@ impl<'a> NodePrinter for pprust::State<'a> {
10191019
NodePat(a) => self.print_pat(&a),
10201020
NodeBlock(a) => self.print_block(&a),
10211021
NodeLifetime(a) => self.print_lifetime(&a),
1022-
NodeTyParam(_) => panic!("cannot print TyParam"),
1022+
NodeTyParam(_) => bug!("cannot print TyParam"),
10231023
// these cases do not carry enough information in the
10241024
// ast_map to reconstruct their full structure for pretty
10251025
// printing.
1026-
NodeLocal(_) => panic!("cannot print isolated Local"),
1027-
NodeStructCtor(_) => panic!("cannot print isolated StructCtor"),
1026+
NodeLocal(_) => bug!("cannot print isolated Local"),
1027+
NodeStructCtor(_) => bug!("cannot print isolated StructCtor"),
10281028
}
10291029
}
10301030
}

src/librustc/infer/combine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ impl<'cx, 'tcx> ty::fold::TypeFolder<'tcx> for Generalizer<'cx, 'tcx> {
339339
// Early-bound regions should really have been substituted away before
340340
// we get to this point.
341341
ty::ReEarlyBound(..) => {
342-
self.tcx().sess.span_bug(
342+
span_bug!(
343343
self.span,
344-
&format!("Encountered early bound region when generalizing: {:?}",
345-
r));
344+
"Encountered early bound region when generalizing: {:?}",
345+
r);
346346
}
347347

348348
// Always make a fresh region variable for skolemized regions;

src/librustc/infer/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
404404
bound_failures.push((origin.clone(), kind.clone(), region));
405405
}
406406
ProcessedErrors(..) => {
407-
panic!("should not encounter a `ProcessedErrors` yet: {:?}", error)
407+
bug!("should not encounter a `ProcessedErrors` yet: {:?}", error)
408408
}
409409
}
410410
}

src/librustc/infer/freshen.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
140140
ty::TyInfer(ty::FreshIntTy(c)) |
141141
ty::TyInfer(ty::FreshFloatTy(c)) => {
142142
if c >= self.freshen_count {
143-
tcx.sess.bug(
144-
&format!("Encountered a freshend type with id {} \
145-
but our counter is only at {}",
146-
c,
147-
self.freshen_count));
143+
bug!("Encountered a freshend type with id {} \
144+
but our counter is only at {}",
145+
c,
146+
self.freshen_count);
148147
}
149148
t
150149
}

src/librustc/infer/higher_ranked/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,10 @@ impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
183183
}
184184
}
185185

186-
infcx.tcx.sess.span_bug(
186+
span_bug!(
187187
span,
188-
&format!("region {:?} is not associated with \
189-
any bound region from A!",
190-
r0))
188+
"region {:?} is not associated with any bound region from A!",
189+
r0)
191190
}
192191
}
193192

@@ -297,7 +296,7 @@ impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
297296

298297
if a_r.is_some() && b_r.is_some() && only_new_vars {
299298
// Related to exactly one bound variable from each fn:
300-
return rev_lookup(infcx, span, a_map, a_r.unwrap());
299+
return rev_lookup(span, a_map, a_r.unwrap());
301300
} else if a_r.is_none() && b_r.is_none() {
302301
// Not related to bound variables from either fn:
303302
assert!(!r0.is_bound());
@@ -308,8 +307,7 @@ impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
308307
}
309308
}
310309

311-
fn rev_lookup(infcx: &InferCtxt,
312-
span: Span,
310+
fn rev_lookup(span: Span,
313311
a_map: &FnvHashMap<ty::BoundRegion, ty::Region>,
314312
r: ty::Region) -> ty::Region
315313
{
@@ -318,9 +316,10 @@ impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
318316
return ty::ReLateBound(ty::DebruijnIndex::new(1), *a_br);
319317
}
320318
}
321-
infcx.tcx.sess.span_bug(
319+
span_bug!(
322320
span,
323-
&format!("could not find original bound region for {:?}", r));
321+
"could not find original bound region for {:?}",
322+
r);
324323
}
325324

326325
fn fresh_bound_variable(infcx: &InferCtxt, debruijn: ty::DebruijnIndex) -> ty::Region {
@@ -336,9 +335,10 @@ fn var_ids<'a, 'tcx>(fields: &CombineFields<'a, 'tcx>,
336335
.map(|(_, r)| match *r {
337336
ty::ReVar(r) => { r }
338337
r => {
339-
fields.tcx().sess.span_bug(
338+
span_bug!(
340339
fields.trace.origin.span(),
341-
&format!("found non-region-vid: {:?}", r));
340+
"found non-region-vid: {:?}",
341+
r);
342342
}
343343
})
344344
.collect()

src/librustc/infer/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ pub fn mk_eq_impl_headers<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
471471
match (a.trait_ref, b.trait_ref) {
472472
(Some(a_ref), Some(b_ref)) => mk_eq_trait_refs(cx, a_is_expected, origin, a_ref, b_ref),
473473
(None, None) => mk_eqty(cx, a_is_expected, origin, a.self_ty, b.self_ty),
474-
_ => cx.tcx.sess.bug("mk_eq_impl_headers given mismatched impl kinds"),
474+
_ => bug!("mk_eq_impl_headers given mismatched impl kinds"),
475475
}
476476
}
477477

@@ -536,10 +536,10 @@ pub fn drain_fulfillment_cx_or_panic<'a,'tcx,T>(span: Span,
536536
match drain_fulfillment_cx(infcx, fulfill_cx, result) {
537537
Ok(v) => v,
538538
Err(errors) => {
539-
infcx.tcx.sess.span_bug(
539+
span_bug!(
540540
span,
541-
&format!("Encountered errors `{:?}` fulfilling during trans",
542-
errors));
541+
"Encountered errors `{:?}` fulfilling during trans",
542+
errors);
543543
}
544544
}
545545
}
@@ -1114,9 +1114,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11141114
None if self.errors_since_creation() =>
11151115
self.tcx.types.err,
11161116
None => {
1117-
self.tcx.sess.bug(
1118-
&format!("no type for node {}: {} in fcx",
1119-
id, self.tcx.map.node_to_string(id)));
1117+
bug!("no type for node {}: {} in fcx",
1118+
id, self.tcx.map.node_to_string(id));
11201119
}
11211120
}
11221121
}
@@ -1125,7 +1124,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11251124
match self.tables.borrow().node_types.get(&ex.id) {
11261125
Some(&t) => t,
11271126
None => {
1128-
self.tcx.sess.bug("no type for expr in fcx");
1127+
bug!("no type for expr in fcx");
11291128
}
11301129
}
11311130
}

src/librustc/infer/region_inference/graphviz.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a,
9090
};
9191

9292
if output_template.is_empty() {
93-
tcx.sess.bug("empty string provided as RUST_REGION_GRAPH");
93+
bug!("empty string provided as RUST_REGION_GRAPH");
9494
}
9595

9696
if output_template.contains('%') {
@@ -182,13 +182,13 @@ impl<'a, 'tcx> dot::Labeller<'a> for ConstraintGraph<'a, 'tcx> {
182182
fn node_id(&self, n: &Node) -> dot::Id {
183183
let node_id = match self.node_ids.get(n) {
184184
Some(node_id) => node_id,
185-
None => panic!("no node_id found for node: {:?}", n),
185+
None => bug!("no node_id found for node: {:?}", n),
186186
};
187187
let name = || format!("node_{}", node_id);
188188
match dot::Id::new(name()) {
189189
Ok(id) => id,
190190
Err(_) => {
191-
panic!("failed to create graphviz node identified by {}", name());
191+
bug!("failed to create graphviz node identified by {}", name());
192192
}
193193
}
194194
}

0 commit comments

Comments
 (0)