Skip to content

Print query stack on ICEs #49222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use dep_graph::{DepNodeIndex, DepNode, DepKind, DepNodeColor};
use errors::DiagnosticBuilder;
use errors::Level;
use ty::tls;
use ty::{TyCtxt};
use ty::maps::config::QueryDescription;
use ty::maps::job::{QueryResult, QueryInfo};
Expand Down Expand Up @@ -108,6 +110,33 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
})
}

pub fn try_print_query_stack() {
eprintln!("query stack during panic:");

tls::with_context_opt(|icx| {
if let Some(icx) = icx {
let mut current_query = icx.query.clone();
let mut i = 0;

while let Some(query) = current_query {
let mut db = DiagnosticBuilder::new(icx.tcx.sess.diagnostic(),
Level::FailureNote,
&format!("#{} [{}] {}",
i,
query.info.query.name(),
query.info.query.describe(icx.tcx)));
db.set_span(icx.tcx.sess.codemap().def_span(query.info.span));
icx.tcx.sess.diagnostic().force_print_db(db);

current_query = query.parent.clone();
i += 1;
}
}
});

eprintln!("end of query stack");
}

/// Try to read a node index for the node dep_node.
/// A node will have an index, when it's already been marked green, or when we can mark it
/// green. This function will mark the current task as a reader of the specified node, when
Expand Down Expand Up @@ -219,6 +248,12 @@ macro_rules! define_maps {
}

impl<$tcx> Query<$tcx> {
pub fn name(&self) -> &'static str {
match *self {
$(Query::$name(_) => stringify!($name),)*
}
}

pub fn describe(&self, tcx: TyCtxt) -> String {
let (r, name) = match *self {
$(Query::$name(key) => {
Expand Down
10 changes: 9 additions & 1 deletion src/librustc/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ use std::fmt::Debug;
use std::hash::{Hash, BuildHasher};
use std::iter::repeat;
use std::panic;
use std::env;
use std::path::Path;
use std::time::{Duration, Instant};

use std::sync::mpsc::{Sender};
use syntax_pos::{SpanData};
use ty::maps::{QueryMsg};
use ty::TyCtxt;
use dep_graph::{DepNode};
use proc_macro;
use lazy_static;
Expand All @@ -48,7 +50,13 @@ lazy_static! {

fn panic_hook(info: &panic::PanicInfo) {
if !proc_macro::__internal::in_sess() {
(*DEFAULT_HOOK)(info)
(*DEFAULT_HOOK)(info);

let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false);

if backtrace {
TyCtxt::try_print_query_stack();
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,11 @@ impl Handler {
self.tracked_diagnostic_codes.borrow().contains(code)
}

pub fn force_print_db(&self, mut db: DiagnosticBuilder) {
self.emitter.borrow_mut().emit(&db);
db.cancel();
}

fn emit_db(&self, db: &DiagnosticBuilder) {
let diagnostic = &**db;

Expand Down