Skip to content

Commit 63af264

Browse files
committed
Spread tracing instrumentation into the polymorphization logic
1 parent 0559e50 commit 63af264

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

compiler/rustc_mir/src/monomorphize/polymorphize.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pub fn provide(providers: &mut Providers) {
3030
/// Determine which generic parameters are used by the function/method/closure represented by
3131
/// `def_id`. Returns a bitset where bits representing unused parameters are set (`is_empty`
3232
/// indicates all parameters are used).
33+
#[instrument(skip(tcx))]
3334
fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
34-
debug!("unused_generic_params({:?})", def_id);
35-
3635
if !tcx.sess.opts.debugging_opts.polymorphize {
3736
// If polymorphization disabled, then all parameters are used.
3837
return FiniteBitSet::new_empty();
@@ -46,7 +45,7 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
4645
}
4746

4847
let generics = tcx.generics_of(def_id);
49-
debug!("unused_generic_params: generics={:?}", generics);
48+
debug!(?generics);
5049

5150
// Exit early when there are no parameters to be unused.
5251
if generics.count() == 0 {
@@ -57,11 +56,11 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
5756
let context = tcx.hir().body_const_context(def_id.expect_local());
5857
match context {
5958
Some(ConstContext::ConstFn) | None if !tcx.is_mir_available(def_id) => {
60-
debug!("unused_generic_params: (no mir available) def_id={:?}", def_id);
59+
debug!("no mir available");
6160
return FiniteBitSet::new_empty();
6261
}
6362
Some(_) if !tcx.is_ctfe_mir_available(def_id) => {
64-
debug!("unused_generic_params: (no ctfe mir available) def_id={:?}", def_id);
63+
debug!("no ctfe mir available");
6564
return FiniteBitSet::new_empty();
6665
}
6766
_ => {}
@@ -72,9 +71,9 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
7271
generics.count().try_into().expect("more generic parameters than can fit into a `u32`");
7372
let mut unused_parameters = FiniteBitSet::<u32>::new_empty();
7473
unused_parameters.set_range(0..generics_count);
75-
debug!("unused_generic_params: (start) unused_parameters={:?}", unused_parameters);
74+
debug!(?unused_parameters, "(start)");
7675
mark_used_by_default_parameters(tcx, def_id, generics, &mut unused_parameters);
77-
debug!("unused_generic_params: (after default) unused_parameters={:?}", unused_parameters);
76+
debug!(?unused_parameters, "(after default)");
7877

7978
// Visit MIR and accumululate used generic parameters.
8079
let body = match context {
@@ -85,10 +84,10 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
8584
};
8685
let mut vis = MarkUsedGenericParams { tcx, def_id, unused_parameters: &mut unused_parameters };
8786
vis.visit_body(body);
88-
debug!("unused_generic_params: (after visitor) unused_parameters={:?}", unused_parameters);
87+
debug!(?unused_parameters, "(after visitor)");
8988

9089
mark_used_by_predicates(tcx, def_id, &mut unused_parameters);
91-
debug!("unused_generic_params: (end) unused_parameters={:?}", unused_parameters);
90+
debug!(?unused_parameters, "(end)");
9291

9392
// Emit errors for debugging and testing if enabled.
9493
if !unused_parameters.is_empty() {
@@ -101,6 +100,7 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {
101100
/// Some parameters are considered used-by-default, such as non-generic parameters and the dummy
102101
/// generic parameters from closures, this function marks them as used. `leaf_is_closure` should
103102
/// be `true` if the item that `unused_generic_params` was invoked on is a closure.
103+
#[instrument(skip(tcx, def_id, generics, unused_parameters))]
104104
fn mark_used_by_default_parameters<'tcx>(
105105
tcx: TyCtxt<'tcx>,
106106
def_id: DefId,
@@ -109,12 +109,12 @@ fn mark_used_by_default_parameters<'tcx>(
109109
) {
110110
if !tcx.is_trait(def_id) && (tcx.is_closure(def_id) || tcx.type_of(def_id).is_generator()) {
111111
for param in &generics.params {
112-
debug!("mark_used_by_default_parameters: (closure/gen) param={:?}", param);
112+
debug!(?param, "(closure/gen)");
113113
unused_parameters.clear(param.index);
114114
}
115115
} else {
116116
for param in &generics.params {
117-
debug!("mark_used_by_default_parameters: (other) param={:?}", param);
117+
debug!(?param, "(other)");
118118
if let ty::GenericParamDefKind::Lifetime = param.kind {
119119
unused_parameters.clear(param.index);
120120
}
@@ -128,23 +128,20 @@ fn mark_used_by_default_parameters<'tcx>(
128128

129129
/// Search the predicates on used generic parameters for any unused generic parameters, and mark
130130
/// those as used.
131+
#[instrument(skip(tcx, def_id))]
131132
fn mark_used_by_predicates<'tcx>(
132133
tcx: TyCtxt<'tcx>,
133134
def_id: DefId,
134135
unused_parameters: &mut FiniteBitSet<u32>,
135136
) {
136137
let def_id = tcx.closure_base_def_id(def_id);
137138
let predicates = tcx.explicit_predicates_of(def_id);
138-
debug!("mark_used_by_predicates: predicates_of={:?}", predicates);
139139

140140
let mut current_unused_parameters = FiniteBitSet::new_empty();
141141
// Run to a fixed point to support `where T: Trait<U>, U: Trait<V>`, starting with an empty
142142
// bit set so that this is skipped if all parameters are already used.
143143
while current_unused_parameters != *unused_parameters {
144-
debug!(
145-
"mark_used_by_predicates: current_unused_parameters={:?} = unused_parameters={:?}",
146-
current_unused_parameters, unused_parameters
147-
);
144+
debug!(?current_unused_parameters, ?unused_parameters);
148145
current_unused_parameters = *unused_parameters;
149146

150147
for (predicate, _) in predicates.predicates {
@@ -169,13 +166,13 @@ fn mark_used_by_predicates<'tcx>(
169166

170167
/// Emit errors for the function annotated by `#[rustc_polymorphize_error]`, labelling each generic
171168
/// parameter which was unused.
169+
#[instrument(skip(tcx, generics))]
172170
fn emit_unused_generic_params_error<'tcx>(
173171
tcx: TyCtxt<'tcx>,
174172
def_id: DefId,
175173
generics: &'tcx ty::Generics,
176174
unused_parameters: &FiniteBitSet<u32>,
177175
) {
178-
debug!("emit_unused_generic_params_error: def_id={:?}", def_id);
179176
let base_def_id = tcx.closure_base_def_id(def_id);
180177
if !tcx
181178
.get_attrs(base_def_id)
@@ -185,7 +182,6 @@ fn emit_unused_generic_params_error<'tcx>(
185182
return;
186183
}
187184

188-
debug!("emit_unused_generic_params_error: unused_parameters={:?}", unused_parameters);
189185
let fn_span = match tcx.opt_item_name(def_id) {
190186
Some(ident) => ident.span,
191187
_ => tcx.def_span(def_id),
@@ -197,7 +193,7 @@ fn emit_unused_generic_params_error<'tcx>(
197193
while let Some(generics) = next_generics {
198194
for param in &generics.params {
199195
if unused_parameters.contains(param.index).unwrap_or(false) {
200-
debug!("emit_unused_generic_params_error: param={:?}", param);
196+
debug!(?param);
201197
let def_span = tcx.def_span(param.def_id);
202198
err.span_label(def_span, &format!("generic parameter `{}` is unused", param.name));
203199
}
@@ -219,33 +215,31 @@ struct MarkUsedGenericParams<'a, 'tcx> {
219215
impl<'a, 'tcx> MarkUsedGenericParams<'a, 'tcx> {
220216
/// Invoke `unused_generic_params` on a body contained within the current item (e.g.
221217
/// a closure, generator or constant).
218+
#[instrument(skip(self, def_id, substs))]
222219
fn visit_child_body(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) {
223220
let unused = self.tcx.unused_generic_params(def_id);
224-
debug!(
225-
"visit_child_body: unused_parameters={:?} unused={:?}",
226-
self.unused_parameters, unused
227-
);
221+
debug!(?self.unused_parameters, ?unused);
228222
for (i, arg) in substs.iter().enumerate() {
229223
let i = i.try_into().unwrap();
230224
if !unused.contains(i).unwrap_or(false) {
231225
arg.visit_with(self);
232226
}
233227
}
234-
debug!("visit_child_body: unused_parameters={:?}", self.unused_parameters);
228+
debug!(?self.unused_parameters);
235229
}
236230
}
237231

238232
impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
233+
#[instrument(skip(self, local))]
239234
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
240-
debug!("visit_local_decl: local_decl={:?}", local_decl);
241235
if local == Local::from_usize(1) {
242236
let def_kind = self.tcx.def_kind(self.def_id);
243237
if matches!(def_kind, DefKind::Closure | DefKind::Generator) {
244238
// Skip visiting the closure/generator that is currently being processed. This only
245239
// happens because the first argument to the closure is a reference to itself and
246240
// that will call `visit_substs`, resulting in each generic parameter captured being
247241
// considered used by default.
248-
debug!("visit_local_decl: skipping closure substs");
242+
debug!("skipping closure substs");
249243
return;
250244
}
251245
}
@@ -263,15 +257,15 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
263257
}
264258

265259
impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
260+
#[instrument(skip(self))]
266261
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
267-
debug!("visit_const: c={:?}", c);
268262
if !c.has_param_types_or_consts() {
269263
return ControlFlow::CONTINUE;
270264
}
271265

272266
match c.val {
273267
ty::ConstKind::Param(param) => {
274-
debug!("visit_const: param={:?}", param);
268+
debug!(?param);
275269
self.unused_parameters.clear(param.index);
276270
ControlFlow::CONTINUE
277271
}
@@ -296,15 +290,15 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
296290
}
297291
}
298292

293+
#[instrument(skip(self))]
299294
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
300-
debug!("visit_ty: ty={:?}", ty);
301295
if !ty.has_param_types_or_consts() {
302296
return ControlFlow::CONTINUE;
303297
}
304298

305299
match *ty.kind() {
306300
ty::Closure(def_id, substs) | ty::Generator(def_id, substs, ..) => {
307-
debug!("visit_ty: def_id={:?}", def_id);
301+
debug!(?def_id);
308302
// Avoid cycle errors with generators.
309303
if def_id == self.def_id {
310304
return ControlFlow::CONTINUE;
@@ -316,7 +310,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
316310
ControlFlow::CONTINUE
317311
}
318312
ty::Param(param) => {
319-
debug!("visit_ty: param={:?}", param);
313+
debug!(?param);
320314
self.unused_parameters.clear(param.index);
321315
ControlFlow::CONTINUE
322316
}
@@ -333,8 +327,8 @@ struct HasUsedGenericParams<'a> {
333327
impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a> {
334328
type BreakTy = ();
335329

330+
#[instrument(skip(self))]
336331
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
337-
debug!("visit_const: c={:?}", c);
338332
if !c.has_param_types_or_consts() {
339333
return ControlFlow::CONTINUE;
340334
}
@@ -351,8 +345,8 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for HasUsedGenericParams<'a> {
351345
}
352346
}
353347

348+
#[instrument(skip(self))]
354349
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
355-
debug!("visit_ty: ty={:?}", ty);
356350
if !ty.has_param_types_or_consts() {
357351
return ControlFlow::CONTINUE;
358352
}

0 commit comments

Comments
 (0)