Skip to content

Commit 976336d

Browse files
committed
Add proper tracing spans to rustc_trait_selection::traits::error_reporting
1 parent 9d20fd1 commit 976336d

File tree

2 files changed

+36
-58
lines changed

2 files changed

+36
-58
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
259259
let cycle = self.resolve_vars_if_possible(cycle.to_owned());
260260
assert!(!cycle.is_empty());
261261

262-
debug!("report_overflow_error_cycle: cycle={:?}", cycle);
262+
debug!(?cycle, "report_overflow_error_cycle");
263263

264264
// The 'deepest' obligation is most likely to have a useful
265265
// cause 'backtrace'
@@ -1513,6 +1513,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
15131513
}
15141514
}
15151515

1516+
#[instrument(level = "debug", skip_all)]
15161517
fn report_projection_error(
15171518
&self,
15181519
obligation: &PredicateObligation<'tcx>,
@@ -1551,15 +1552,9 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
15511552
&mut obligations,
15521553
);
15531554

1554-
debug!(
1555-
"report_projection_error obligation.cause={:?} obligation.param_env={:?}",
1556-
obligation.cause, obligation.param_env
1557-
);
1555+
debug!(?obligation.cause, ?obligation.param_env);
15581556

1559-
debug!(
1560-
"report_projection_error normalized_ty={:?} data.ty={:?}",
1561-
normalized_ty, data.term,
1562-
);
1557+
debug!(?normalized_ty, data.ty = ?data.term);
15631558

15641559
let is_normalized_ty_expected = !matches!(
15651560
obligation.cause.code().peel_derives(),
@@ -2346,6 +2341,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
23462341
}
23472342
}
23482343

2344+
#[instrument(level = "debug", skip_all)]
23492345
fn suggest_unsized_bound_if_applicable(
23502346
&self,
23512347
err: &mut Diagnostic,
@@ -2360,10 +2356,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
23602356
) else {
23612357
return;
23622358
};
2363-
debug!(
2364-
"suggest_unsized_bound_if_applicable: pred={:?} item_def_id={:?} span={:?}",
2365-
pred, item_def_id, span
2366-
);
2359+
debug!(?pred, ?item_def_id, ?span);
23672360

23682361
let (Some(node), true) = (
23692362
self.tcx.hir().get_if_local(item_def_id),
@@ -2374,6 +2367,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
23742367
self.maybe_suggest_unsized_generics(err, span, node);
23752368
}
23762369

2370+
#[instrument(level = "debug", skip_all)]
23772371
fn maybe_suggest_unsized_generics<'hir>(
23782372
&self,
23792373
err: &mut Diagnostic,
@@ -2384,8 +2378,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
23842378
return;
23852379
};
23862380
let sized_trait = self.tcx.lang_items().sized_trait();
2387-
debug!("maybe_suggest_unsized_generics: generics.params={:?}", generics.params);
2388-
debug!("maybe_suggest_unsized_generics: generics.predicates={:?}", generics.predicates);
2381+
debug!(?generics.params);
2382+
debug!(?generics.predicates);
23892383
let Some(param) = generics.params.iter().find(|param| param.span == span) else {
23902384
return;
23912385
};
@@ -2399,7 +2393,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
23992393
if explicitly_sized {
24002394
return;
24012395
}
2402-
debug!("maybe_suggest_unsized_generics: param={:?}", param);
2396+
debug!(?param);
24032397
match node {
24042398
hir::Node::Item(
24052399
item @ hir::Item {
@@ -2517,7 +2511,7 @@ impl<'v> Visitor<'v> for FindTypeParam {
25172511
if path.segments.len() == 1 && path.segments[0].ident.name == self.param =>
25182512
{
25192513
if !self.nested {
2520-
debug!("FindTypeParam::visit_ty: ty={:?}", ty);
2514+
debug!(?ty, "FindTypeParam::visit_ty");
25212515
self.invalid_spans.push(ty.span);
25222516
}
25232517
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+25-41
Original file line numberDiff line numberDiff line change
@@ -1623,16 +1623,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16231623
/// ```
16241624
///
16251625
/// Returns `true` if an async-await specific note was added to the diagnostic.
1626+
#[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
16261627
fn maybe_note_obligation_cause_for_async_await(
16271628
&self,
16281629
err: &mut Diagnostic,
16291630
obligation: &PredicateObligation<'tcx>,
16301631
) -> bool {
1631-
debug!(
1632-
"maybe_note_obligation_cause_for_async_await: obligation.predicate={:?} \
1633-
obligation.cause.span={:?}",
1634-
obligation.predicate, obligation.cause.span
1635-
);
16361632
let hir = self.tcx.hir();
16371633

16381634
// Attempt to detect an async-await error by looking at the obligation causes, looking
@@ -1672,18 +1668,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16721668
let mut seen_upvar_tys_infer_tuple = false;
16731669

16741670
while let Some(code) = next_code {
1675-
debug!("maybe_note_obligation_cause_for_async_await: code={:?}", code);
1671+
debug!(?code);
16761672
match code {
16771673
ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => {
16781674
next_code = Some(parent_code);
16791675
}
16801676
ObligationCauseCode::ImplDerivedObligation(cause) => {
16811677
let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
16821678
debug!(
1683-
"maybe_note_obligation_cause_for_async_await: ImplDerived \
1684-
parent_trait_ref={:?} self_ty.kind={:?}",
1685-
cause.derived.parent_trait_pred,
1686-
ty.kind()
1679+
parent_trait_ref = ?cause.derived.parent_trait_pred,
1680+
self_ty.kind = ?ty.kind(),
1681+
"ImplDerived",
16871682
);
16881683

16891684
match *ty.kind() {
@@ -1712,10 +1707,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17121707
| ObligationCauseCode::BuiltinDerivedObligation(derived_obligation) => {
17131708
let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
17141709
debug!(
1715-
"maybe_note_obligation_cause_for_async_await: \
1716-
parent_trait_ref={:?} self_ty.kind={:?}",
1717-
derived_obligation.parent_trait_pred,
1718-
ty.kind()
1710+
parent_trait_ref = ?derived_obligation.parent_trait_pred,
1711+
self_ty.kind = ?ty.kind(),
17191712
);
17201713

17211714
match *ty.kind() {
@@ -1745,7 +1738,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17451738
}
17461739

17471740
// Only continue if a generator was found.
1748-
debug!(?generator, ?trait_ref, ?target_ty, "maybe_note_obligation_cause_for_async_await");
1741+
debug!(?generator, ?trait_ref, ?target_ty);
17491742
let (Some(generator_did), Some(trait_ref), Some(target_ty)) = (generator, trait_ref, target_ty) else {
17501743
return false;
17511744
};
@@ -1755,12 +1748,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17551748
let in_progress_typeck_results = self.in_progress_typeck_results.map(|t| t.borrow());
17561749
let generator_did_root = self.tcx.typeck_root_def_id(generator_did);
17571750
debug!(
1758-
"maybe_note_obligation_cause_for_async_await: generator_did={:?} \
1759-
generator_did_root={:?} in_progress_typeck_results.hir_owner={:?} span={:?}",
1760-
generator_did,
1761-
generator_did_root,
1762-
in_progress_typeck_results.as_ref().map(|t| t.hir_owner),
1763-
span
1751+
?generator_did,
1752+
?generator_did_root,
1753+
in_progress_typeck_results.hir_owner = ?in_progress_typeck_results.as_ref().map(|t| t.hir_owner),
1754+
?span,
17641755
);
17651756

17661757
let generator_body = generator_did
@@ -1783,7 +1774,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17831774
if let Some(body) = generator_body {
17841775
visitor.visit_body(body);
17851776
}
1786-
debug!("maybe_note_obligation_cause_for_async_await: awaits = {:?}", visitor.awaits);
1777+
debug!(awaits = ?visitor.awaits);
17871778

17881779
// Look for a type inside the generator interior that matches the target type to get
17891780
// a span.
@@ -1804,11 +1795,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18041795
let ty_erased = self.tcx.erase_late_bound_regions(ty);
18051796
let ty_erased = self.tcx.erase_regions(ty_erased);
18061797
let eq = ty_erased == target_ty_erased;
1807-
debug!(
1808-
"maybe_note_obligation_cause_for_async_await: ty_erased={:?} \
1809-
target_ty_erased={:?} eq={:?}",
1810-
ty_erased, target_ty_erased, eq
1811-
);
1798+
debug!(?ty_erased, ?target_ty_erased, ?eq);
18121799
eq
18131800
};
18141801

@@ -1883,6 +1870,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18831870

18841871
/// Unconditionally adds the diagnostic note described in
18851872
/// `maybe_note_obligation_cause_for_async_await`'s documentation comment.
1873+
#[instrument(level = "debug", skip_all)]
18861874
fn note_obligation_cause_for_async_await(
18871875
&self,
18881876
err: &mut Diagnostic,
@@ -2032,8 +2020,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20322020
} else {
20332021
// Look at the last interior type to get a span for the `.await`.
20342022
debug!(
2035-
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
2036-
typeck_results.as_ref().map(|t| &t.generator_interior_types)
2023+
generator_interior_types = ?format_args!(
2024+
"{:#?}", typeck_results.as_ref().map(|t| &t.generator_interior_types)
2025+
),
20372026
);
20382027
explain_yield(interior_span, yield_span, scope_span);
20392028
}
@@ -2068,7 +2057,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20682057
// bar(Foo(std::ptr::null())).await;
20692058
// ^^^^^^^^^^^^^^^^^^^^^ raw-ptr `*T` created inside this struct ctor.
20702059
// ```
2071-
debug!("parent_def_kind: {:?}", self.tcx.def_kind(parent_did));
2060+
debug!(parent_def_kind = ?self.tcx.def_kind(parent_did));
20722061
let is_raw_borrow_inside_fn_like_call =
20732062
match self.tcx.def_kind(parent_did) {
20742063
DefKind::Fn | DefKind::Ctor(..) => target_ty.is_unsafe_ptr(),
@@ -2126,7 +2115,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
21262115

21272116
// Add a note for the item obligation that remains - normally a note pointing to the
21282117
// bound that introduced the obligation (e.g. `T: Send`).
2129-
debug!("note_obligation_cause_for_async_await: next_code={:?}", next_code);
2118+
debug!(?next_code);
21302119
self.note_obligation_cause_code(
21312120
err,
21322121
&obligation.predicate,
@@ -2683,20 +2672,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
26832672
));
26842673
}
26852674

2675+
#[instrument(
2676+
level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
2677+
)]
26862678
fn suggest_await_before_try(
26872679
&self,
26882680
err: &mut Diagnostic,
26892681
obligation: &PredicateObligation<'tcx>,
26902682
trait_pred: ty::PolyTraitPredicate<'tcx>,
26912683
span: Span,
26922684
) {
2693-
debug!(
2694-
"suggest_await_before_try: obligation={:?}, span={:?}, trait_pred={:?}, trait_pred_self_ty={:?}",
2695-
obligation,
2696-
span,
2697-
trait_pred,
2698-
trait_pred.self_ty()
2699-
);
27002685
let body_hir_id = obligation.cause.body_id;
27012686
let item_id = self.tcx.hir().get_parent_node(body_hir_id);
27022687

@@ -2734,14 +2719,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
27342719
);
27352720

27362721
debug!(
2737-
"suggest_await_before_try: normalized_projection_type {:?}",
2738-
self.resolve_vars_if_possible(projection_ty)
2722+
normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
27392723
);
27402724
let try_obligation = self.mk_trait_obligation_with_new_self_ty(
27412725
obligation.param_env,
27422726
trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
27432727
);
2744-
debug!("suggest_await_before_try: try_trait_obligation {:?}", try_obligation);
2728+
debug!(try_trait_obligation = ?try_obligation);
27452729
if self.predicate_may_hold(&try_obligation)
27462730
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
27472731
&& snippet.ends_with('?')

0 commit comments

Comments
 (0)