Skip to content

Commit feb91f3

Browse files
committed
rustc: improve error messages from wrong --pretty flowgraph use.
This defers to .fatal and .span_fatal for errors (rather than `fail!` which prints the ICE message). It also adds the span lookup when an id doesn't correspond to a block, to show what it is pointing at. It also makes the argument parser slightly looser, so that passing `--pretty flowgraph` recognises the `flowgraph` part and suggests to use an integer.
1 parent dd5365a commit feb91f3

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/librustc/driver/driver.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,22 @@ pub fn pretty_print_input(sess: Session,
653653
PpmFlowGraph(nodeid) => {
654654
let ast_map = ast_map.expect("--pretty flowgraph missing ast_map");
655655
let node = ast_map.find(nodeid).unwrap_or_else(|| {
656-
fail!("--pretty flowgraph=id couldn't find id: {}", id)
656+
sess.fatal(format_strbuf!("--pretty flowgraph couldn't find id: {}",
657+
nodeid).as_slice())
657658
});
658659
let block = match node {
659660
syntax::ast_map::NodeBlock(block) => block,
660-
_ => fail!("--pretty=flowgraph needs block, got {:?}", node)
661+
_ => {
662+
let message = format_strbuf!("--pretty=flowgraph needs block, got {:?}",
663+
node);
664+
665+
// point to what was found, if there's an
666+
// accessible span.
667+
match ast_map.opt_span(nodeid) {
668+
Some(sp) => sess.span_fatal(sp, message.as_slice()),
669+
None => sess.fatal(message.as_slice())
670+
}
671+
}
661672
};
662673
let analysis = phase_3_run_analysis_passes(sess, &krate, ast_map);
663674
print_flowgraph(analysis, block, out)

src/librustc/driver/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,12 @@ pub fn parse_pretty(sess: &Session, name: &str) -> PpMode {
302302
(None, "typed") => PpmTyped,
303303
(None, "expanded,identified") => PpmExpandedIdentified,
304304
(None, "identified") => PpmIdentified,
305-
(Some(s), "flowgraph") => {
306-
match from_str(s) {
305+
(arg, "flowgraph") => {
306+
match arg.and_then(from_str) {
307307
Some(id) => PpmFlowGraph(id),
308-
None => sess.fatal(format!("`pretty flowgraph=<nodeid>` needs \
309-
an integer <nodeid>; got {}", s))
308+
None => sess.fatal(format_strbuf!("`pretty flowgraph=<nodeid>` needs \
309+
an integer <nodeid>; got {}",
310+
arg.unwrap_or("nothing")).as_slice())
310311
}
311312
}
312313
_ => {

src/libsyntax/ast_map.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ impl Map {
388388
f(attrs)
389389
}
390390

391-
pub fn span(&self, id: NodeId) -> Span {
392-
match self.find(id) {
391+
pub fn opt_span(&self, id: NodeId) -> Option<Span> {
392+
let sp = match self.find(id) {
393393
Some(NodeItem(item)) => item.span,
394394
Some(NodeForeignItem(foreign_item)) => foreign_item.span,
395395
Some(NodeTraitMethod(trait_method)) => {
@@ -406,8 +406,14 @@ impl Map {
406406
Some(NodePat(pat)) => pat.span,
407407
Some(NodeBlock(block)) => block.span,
408408
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
409-
_ => fail!("node_span: could not find span for id {}", id),
410-
}
409+
_ => return None,
410+
};
411+
Some(sp)
412+
}
413+
414+
pub fn span(&self, id: NodeId) -> Span {
415+
self.opt_span(id)
416+
.unwrap_or_else(|| fail!("AstMap.span: could not find span for id {}", id))
411417
}
412418

413419
pub fn node_to_str(&self, id: NodeId) -> StrBuf {

0 commit comments

Comments
 (0)