Skip to content

Commit 9c800ec

Browse files
authored
Rollup merge of rust-lang#97805 - coolreader18:trace-suggestions, r=oli-obk
Add proper tracing spans to rustc_trait_selection::traits::error_reporting While I was trying to figure out rust-lang#97704 I did some of this to make the logs more legible, so I figured I'd do the whole module and open a PR with it. afaict this is an ongoing process in the compiler from the log->tracing transition? but lmk if there was a reason for the more verbose forms of logging as they are. Also, for some of the functions with only one log in them, I put the function name as a message for that log instead of `#[instrument]`-ing the whole function with a span? but maybe the latter would actually be preferable, I'm not actually sure.
2 parents b20aff2 + 976336d commit 9c800ec

File tree

2 files changed

+36
-58
lines changed

2 files changed

+36
-58
lines changed

Diff for: 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
}

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+25-41
Original file line numberDiff line numberDiff line change
@@ -1628,16 +1628,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16281628
/// ```
16291629
///
16301630
/// Returns `true` if an async-await specific note was added to the diagnostic.
1631+
#[instrument(level = "debug", skip_all, fields(?obligation.predicate, ?obligation.cause.span))]
16311632
fn maybe_note_obligation_cause_for_async_await(
16321633
&self,
16331634
err: &mut Diagnostic,
16341635
obligation: &PredicateObligation<'tcx>,
16351636
) -> bool {
1636-
debug!(
1637-
"maybe_note_obligation_cause_for_async_await: obligation.predicate={:?} \
1638-
obligation.cause.span={:?}",
1639-
obligation.predicate, obligation.cause.span
1640-
);
16411637
let hir = self.tcx.hir();
16421638

16431639
// Attempt to detect an async-await error by looking at the obligation causes, looking
@@ -1677,18 +1673,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16771673
let mut seen_upvar_tys_infer_tuple = false;
16781674

16791675
while let Some(code) = next_code {
1680-
debug!("maybe_note_obligation_cause_for_async_await: code={:?}", code);
1676+
debug!(?code);
16811677
match code {
16821678
ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => {
16831679
next_code = Some(parent_code);
16841680
}
16851681
ObligationCauseCode::ImplDerivedObligation(cause) => {
16861682
let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
16871683
debug!(
1688-
"maybe_note_obligation_cause_for_async_await: ImplDerived \
1689-
parent_trait_ref={:?} self_ty.kind={:?}",
1690-
cause.derived.parent_trait_pred,
1691-
ty.kind()
1684+
parent_trait_ref = ?cause.derived.parent_trait_pred,
1685+
self_ty.kind = ?ty.kind(),
1686+
"ImplDerived",
16921687
);
16931688

16941689
match *ty.kind() {
@@ -1717,10 +1712,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17171712
| ObligationCauseCode::BuiltinDerivedObligation(derived_obligation) => {
17181713
let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty();
17191714
debug!(
1720-
"maybe_note_obligation_cause_for_async_await: \
1721-
parent_trait_ref={:?} self_ty.kind={:?}",
1722-
derived_obligation.parent_trait_pred,
1723-
ty.kind()
1715+
parent_trait_ref = ?derived_obligation.parent_trait_pred,
1716+
self_ty.kind = ?ty.kind(),
17241717
);
17251718

17261719
match *ty.kind() {
@@ -1750,7 +1743,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17501743
}
17511744

17521745
// Only continue if a generator was found.
1753-
debug!(?generator, ?trait_ref, ?target_ty, "maybe_note_obligation_cause_for_async_await");
1746+
debug!(?generator, ?trait_ref, ?target_ty);
17541747
let (Some(generator_did), Some(trait_ref), Some(target_ty)) = (generator, trait_ref, target_ty) else {
17551748
return false;
17561749
};
@@ -1760,12 +1753,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17601753
let in_progress_typeck_results = self.in_progress_typeck_results.map(|t| t.borrow());
17611754
let generator_did_root = self.tcx.typeck_root_def_id(generator_did);
17621755
debug!(
1763-
"maybe_note_obligation_cause_for_async_await: generator_did={:?} \
1764-
generator_did_root={:?} in_progress_typeck_results.hir_owner={:?} span={:?}",
1765-
generator_did,
1766-
generator_did_root,
1767-
in_progress_typeck_results.as_ref().map(|t| t.hir_owner),
1768-
span
1756+
?generator_did,
1757+
?generator_did_root,
1758+
in_progress_typeck_results.hir_owner = ?in_progress_typeck_results.as_ref().map(|t| t.hir_owner),
1759+
?span,
17691760
);
17701761

17711762
let generator_body = generator_did
@@ -1788,7 +1779,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17881779
if let Some(body) = generator_body {
17891780
visitor.visit_body(body);
17901781
}
1791-
debug!("maybe_note_obligation_cause_for_async_await: awaits = {:?}", visitor.awaits);
1782+
debug!(awaits = ?visitor.awaits);
17921783

17931784
// Look for a type inside the generator interior that matches the target type to get
17941785
// a span.
@@ -1809,11 +1800,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18091800
let ty_erased = self.tcx.erase_late_bound_regions(ty);
18101801
let ty_erased = self.tcx.erase_regions(ty_erased);
18111802
let eq = ty_erased == target_ty_erased;
1812-
debug!(
1813-
"maybe_note_obligation_cause_for_async_await: ty_erased={:?} \
1814-
target_ty_erased={:?} eq={:?}",
1815-
ty_erased, target_ty_erased, eq
1816-
);
1803+
debug!(?ty_erased, ?target_ty_erased, ?eq);
18171804
eq
18181805
};
18191806

@@ -1888,6 +1875,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18881875

18891876
/// Unconditionally adds the diagnostic note described in
18901877
/// `maybe_note_obligation_cause_for_async_await`'s documentation comment.
1878+
#[instrument(level = "debug", skip_all)]
18911879
fn note_obligation_cause_for_async_await(
18921880
&self,
18931881
err: &mut Diagnostic,
@@ -2037,8 +2025,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20372025
} else {
20382026
// Look at the last interior type to get a span for the `.await`.
20392027
debug!(
2040-
"note_obligation_cause_for_async_await generator_interior_types: {:#?}",
2041-
typeck_results.as_ref().map(|t| &t.generator_interior_types)
2028+
generator_interior_types = ?format_args!(
2029+
"{:#?}", typeck_results.as_ref().map(|t| &t.generator_interior_types)
2030+
),
20422031
);
20432032
explain_yield(interior_span, yield_span, scope_span);
20442033
}
@@ -2073,7 +2062,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20732062
// bar(Foo(std::ptr::null())).await;
20742063
// ^^^^^^^^^^^^^^^^^^^^^ raw-ptr `*T` created inside this struct ctor.
20752064
// ```
2076-
debug!("parent_def_kind: {:?}", self.tcx.def_kind(parent_did));
2065+
debug!(parent_def_kind = ?self.tcx.def_kind(parent_did));
20772066
let is_raw_borrow_inside_fn_like_call =
20782067
match self.tcx.def_kind(parent_did) {
20792068
DefKind::Fn | DefKind::Ctor(..) => target_ty.is_unsafe_ptr(),
@@ -2131,7 +2120,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
21312120

21322121
// Add a note for the item obligation that remains - normally a note pointing to the
21332122
// bound that introduced the obligation (e.g. `T: Send`).
2134-
debug!("note_obligation_cause_for_async_await: next_code={:?}", next_code);
2123+
debug!(?next_code);
21352124
self.note_obligation_cause_code(
21362125
err,
21372126
&obligation.predicate,
@@ -2688,20 +2677,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
26882677
));
26892678
}
26902679

2680+
#[instrument(
2681+
level = "debug", skip(self, err), fields(trait_pred.self_ty = ?trait_pred.self_ty())
2682+
)]
26912683
fn suggest_await_before_try(
26922684
&self,
26932685
err: &mut Diagnostic,
26942686
obligation: &PredicateObligation<'tcx>,
26952687
trait_pred: ty::PolyTraitPredicate<'tcx>,
26962688
span: Span,
26972689
) {
2698-
debug!(
2699-
"suggest_await_before_try: obligation={:?}, span={:?}, trait_pred={:?}, trait_pred_self_ty={:?}",
2700-
obligation,
2701-
span,
2702-
trait_pred,
2703-
trait_pred.self_ty()
2704-
);
27052690
let body_hir_id = obligation.cause.body_id;
27062691
let item_id = self.tcx.hir().get_parent_node(body_hir_id);
27072692

@@ -2739,14 +2724,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
27392724
);
27402725

27412726
debug!(
2742-
"suggest_await_before_try: normalized_projection_type {:?}",
2743-
self.resolve_vars_if_possible(projection_ty)
2727+
normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)
27442728
);
27452729
let try_obligation = self.mk_trait_obligation_with_new_self_ty(
27462730
obligation.param_env,
27472731
trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
27482732
);
2749-
debug!("suggest_await_before_try: try_trait_obligation {:?}", try_obligation);
2733+
debug!(try_trait_obligation = ?try_obligation);
27502734
if self.predicate_may_hold(&try_obligation)
27512735
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
27522736
&& snippet.ends_with('?')

0 commit comments

Comments
 (0)