Skip to content

Commit 754d51e

Browse files
committed
useful debug printouts
The changes to dumping expressions seem particularly useful
1 parent af15e52 commit 754d51e

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

Diff for: compiler/rustc_typeck/src/check/_match.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_trait_selection::traits::{
1414
};
1515

1616
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17+
#[instrument(skip(self), level="debug")]
1718
pub fn check_match(
1819
&self,
1920
expr: &'tcx hir::Expr<'tcx>,
@@ -26,6 +27,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2627

2728
let acrb = arms_contain_ref_bindings(arms);
2829
let scrutinee_ty = self.demand_scrutinee_type(scrut, acrb, arms.is_empty());
30+
debug!(?scrutinee_ty);
2931

3032
// If there are no arms, that is a diverging match; a special case.
3133
if arms.is_empty() {

Diff for: compiler/rustc_typeck/src/check/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use rustc_trait_selection::traits::error_reporting::report_object_safety_error;
5151

5252
/// Reifies a cast check to be checked once we have full type information for
5353
/// a function context.
54+
#[derive(Debug)]
5455
pub struct CastCheck<'tcx> {
5556
expr: &'tcx hir::Expr<'tcx>,
5657
expr_ty: Ty<'tcx>,
@@ -603,12 +604,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
603604
});
604605
}
605606

607+
#[instrument(skip(fcx), level = "debug")]
606608
pub fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
607609
self.expr_ty = fcx.structurally_resolved_type(self.expr.span, self.expr_ty);
608610
self.cast_ty = fcx.structurally_resolved_type(self.cast_span, self.cast_ty);
609611

610-
debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
611-
612612
if !fcx.type_is_known_to_be_sized_modulo_regions(self.cast_ty, self.span) {
613613
self.report_cast_to_unsized_type(fcx);
614614
} else if self.expr_ty.references_error() || self.cast_ty.references_error() {

Diff for: compiler/rustc_typeck/src/check/coercion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
13281328
/// The inner coercion "engine". If `expression` is `None`, this
13291329
/// is a forced-unit case, and hence `expression_ty` must be
13301330
/// `Nil`.
1331+
#[instrument(skip(self,fcx,augment_error,label_expression_as_expected), level="debug")]
13311332
crate fn coerce_inner<'a>(
13321333
&mut self,
13331334
fcx: &FnCtxt<'a, 'tcx>,

Diff for: compiler/rustc_typeck/src/check/expr.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
156156
/// Note that inspecting a type's structure *directly* may expose the fact
157157
/// that there are actually multiple representations for `Error`, so avoid
158158
/// that when err needs to be handled differently.
159+
#[instrument(skip(self), level="debug")]
159160
pub(super) fn check_expr_with_expectation(
160161
&self,
161162
expr: &'tcx hir::Expr<'tcx>,
162163
expected: Expectation<'tcx>,
163164
) -> Ty<'tcx> {
164-
debug!(">> type-checking: expected={:?}, expr={:?} ", expected, expr);
165+
if self.tcx().sess.verbose() {
166+
// make this code only run with -Zverbose because it is probably slow
167+
if let Ok(lint_str) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
168+
if !lint_str.contains("\n") {
169+
debug!("expr text: {}", lint_str);
170+
} else {
171+
let mut lines = lint_str.lines();
172+
if let Some(line0) = lines.next() {
173+
let remaining_lines = lines.count();
174+
debug!("expr text: {}", line0);
175+
debug!("expr text: ...(and {} more lines)", remaining_lines);
176+
}
177+
}
178+
}
179+
}
165180

166181
// True if `expr` is a `Try::from_ok(())` that is a result of desugaring a try block
167182
// without the final expr (e.g. `try { return; }`). We don't want to generate an
@@ -1049,6 +1064,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10491064
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
10501065
match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
10511066
Ok(cast_check) => {
1067+
debug!(
1068+
"check_expr_cast: deferring cast from {:?} to {:?}: {:?}",
1069+
t_cast,
1070+
t_expr,
1071+
cast_check,
1072+
);
10521073
deferred_cast_checks.push(cast_check);
10531074
t_cast
10541075
}

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::slice;
2929
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3030
pub(in super::super) fn check_casts(&self) {
3131
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
32+
debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len());
3233
for cast in deferred_cast_checks.drain(..) {
3334
cast.check(self);
3435
}

Diff for: compiler/rustc_typeck/src/expr_use_visitor.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
120120
}
121121
}
122122

123+
#[instrument(skip(self), level = "debug")]
123124
pub fn consume_body(&mut self, body: &hir::Body<'_>) {
124-
debug!("consume_body(body={:?})", body);
125-
126125
for param in body.params {
127126
let param_ty = return_if_err!(self.mc.pat_ty_adjusted(&param.pat));
128127
debug!("consume_body: param_ty = {:?}", param_ty);
@@ -243,7 +242,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
243242
let ExprUseVisitor { ref mc, body_owner: _, delegate: _ } = *self;
244243
let mut needs_to_be_read = false;
245244
for arm in arms.iter() {
246-
match mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
245+
return_if_err!(mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
247246
match &pat.kind {
248247
PatKind::Binding(.., opt_sub_pat) => {
249248
// If the opt_sub_pat is None, than the binding does not count as
@@ -290,13 +289,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
290289
// examined
291290
}
292291
}
293-
}) {
294-
Ok(_) => (),
295-
Err(_) => {
296-
// If typeck failed, assume borrow is needed.
297-
needs_to_be_read = true;
298-
}
299-
}
292+
}));
300293
}
301294

302295
if needs_to_be_read {

0 commit comments

Comments
 (0)