Skip to content

Commit 9c84ac8

Browse files
committed
introduce an enum for tracking the 2229 migration causes
1 parent c3190c1 commit 9c84ac8

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

compiler/rustc_typeck/src/check/upvar.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8686
/// Intermediate format to store the hir_id pointing to the use that resulted in the
8787
/// corresponding place being captured and a String which contains the captured value's
8888
/// name (i.e: a.b.c)
89-
type CapturesInfo = (Option<hir::HirId>, String);
89+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
90+
enum CapturesInfo {
91+
CapturingLess { source_expr: Option<hir::HirId>, var_name: String },
92+
}
9093

9194
/// Intermediate format to store information needed to generate migration lint. The tuple
9295
/// contains the hir_id pointing to the use that resulted in the
@@ -963,7 +966,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
963966

964967
if !capture_problems.is_empty() {
965968
problematic_captures.insert(
966-
(capture.info.path_expr_id, capture.to_string(self.tcx)),
969+
CapturesInfo::CapturingLess {
970+
source_expr: capture.info.path_expr_id,
971+
var_name: capture.to_string(self.tcx),
972+
},
967973
capture_problems,
968974
);
969975
}
@@ -986,6 +992,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
986992
///
987993
/// This function only returns a HashSet of CapturesInfo for significant drops. If there
988994
/// are no significant drops than None is returned
995+
#[instrument(level = "debug", skip(self))]
989996
fn compute_2229_migrations_for_drop(
990997
&self,
991998
closure_def_id: DefId,
@@ -997,12 +1004,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9971004
let ty = self.infcx.resolve_vars_if_possible(self.node_ty(var_hir_id));
9981005

9991006
if !ty.has_significant_drop(self.tcx, self.tcx.param_env(closure_def_id.expect_local())) {
1007+
debug!("does not have significant drop");
10001008
return None;
10011009
}
10021010

10031011
let Some(root_var_min_capture_list) = min_captures.and_then(|m| m.get(&var_hir_id)) else {
10041012
// The upvar is mentioned within the closure but no path starting from it is
1005-
// used.
1013+
// used. This occurs when you have (e.g.)
1014+
//
1015+
// ```
1016+
// let x = move || {
1017+
// let _ = y;
1018+
// });
1019+
// ```
1020+
debug!("no path starting from it is used");
1021+
10061022

10071023
match closure_clause {
10081024
// Only migrate if closure is a move closure
@@ -1012,6 +1028,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10121028

10131029
return None;
10141030
};
1031+
debug!(?root_var_min_capture_list);
10151032

10161033
let mut projections_list = Vec::new();
10171034
let mut diagnostics_info = FxHashSet::default();
@@ -1021,19 +1038,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10211038
// Only care about captures that are moved into the closure
10221039
ty::UpvarCapture::ByValue(..) => {
10231040
projections_list.push(captured_place.place.projections.as_slice());
1024-
diagnostics_info.insert((
1025-
captured_place.info.path_expr_id,
1026-
captured_place.to_string(self.tcx),
1027-
));
1041+
diagnostics_info.insert(CapturesInfo::CapturingLess {
1042+
source_expr: captured_place.info.path_expr_id,
1043+
var_name: captured_place.to_string(self.tcx),
1044+
});
10281045
}
10291046
ty::UpvarCapture::ByRef(..) => {}
10301047
}
10311048
}
10321049

1050+
debug!(?projections_list);
1051+
debug!(?diagnostics_info);
1052+
10331053
let is_moved = !projections_list.is_empty();
1054+
debug!(?is_moved);
10341055

10351056
let is_not_completely_captured =
10361057
root_var_min_capture_list.iter().any(|capture| !capture.place.projections.is_empty());
1058+
debug!(?is_not_completely_captured);
10371059

10381060
if is_moved
10391061
&& is_not_completely_captured
@@ -1066,6 +1088,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10661088
/// Returns a tuple containing a vector of MigrationDiagnosticInfo, as well as a String
10671089
/// containing the reason why root variables whose HirId is contained in the vector should
10681090
/// be captured
1091+
#[instrument(level = "debug", skip(self))]
10691092
fn compute_2229_migrations(
10701093
&self,
10711094
closure_def_id: DefId,
@@ -1131,14 +1154,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11311154
// auto trait implementation issues
11321155
auto_trait_migration_reasons.extend(capture_trait_reasons.clone());
11331156

1134-
responsible_captured_hir_ids.push((
1135-
captured_info.0,
1136-
captured_info.1.clone(),
1137-
self.compute_2229_migrations_reasons(
1138-
capture_trait_reasons,
1139-
capture_drop_reorder_reason,
1140-
),
1141-
));
1157+
match captured_info {
1158+
CapturesInfo::CapturingLess { source_expr, var_name } => {
1159+
responsible_captured_hir_ids.push((
1160+
*source_expr,
1161+
var_name.clone(),
1162+
self.compute_2229_migrations_reasons(
1163+
capture_trait_reasons,
1164+
capture_drop_reorder_reason,
1165+
),
1166+
));
1167+
}
1168+
}
11421169
}
11431170

11441171
if !capture_diagnostic.is_empty() {
@@ -2087,6 +2114,7 @@ fn var_name(tcx: TyCtxt<'_>, var_hir_id: hir::HirId) -> Symbol {
20872114
tcx.hir().name(var_hir_id)
20882115
}
20892116

2117+
#[instrument(level = "debug", skip(tcx))]
20902118
fn should_do_rust_2021_incompatible_closure_captures_analysis(
20912119
tcx: TyCtxt<'_>,
20922120
closure_id: hir::HirId,

0 commit comments

Comments
 (0)