Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5de0c84

Browse files
nikomatsakiscuviper
authored andcommitted
introduce an enum for tracking the 2229 migration causes
(cherry picked from commit 9c84ac8)
1 parent fed62b2 commit 5de0c84

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,6 +1004,7 @@ 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

@@ -1006,7 +1014,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10061014
root_var_min_capture_list
10071015
} else {
10081016
// The upvar is mentioned within the closure but no path starting from it is
1009-
// used.
1017+
// used. This occurs when you have (e.g.)
1018+
//
1019+
// ```
1020+
// let x = move || {
1021+
// let _ = y;
1022+
// });
1023+
// ```
1024+
debug!("no path starting from it is used");
1025+
10101026

10111027
match closure_clause {
10121028
// Only migrate if closure is a move closure
@@ -1016,6 +1032,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10161032

10171033
return None;
10181034
};
1035+
debug!(?root_var_min_capture_list);
10191036

10201037
let mut projections_list = Vec::new();
10211038
let mut diagnostics_info = FxHashSet::default();
@@ -1025,19 +1042,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10251042
// Only care about captures that are moved into the closure
10261043
ty::UpvarCapture::ByValue(..) => {
10271044
projections_list.push(captured_place.place.projections.as_slice());
1028-
diagnostics_info.insert((
1029-
captured_place.info.path_expr_id,
1030-
captured_place.to_string(self.tcx),
1031-
));
1045+
diagnostics_info.insert(CapturesInfo::CapturingLess {
1046+
source_expr: captured_place.info.path_expr_id,
1047+
var_name: captured_place.to_string(self.tcx),
1048+
});
10321049
}
10331050
ty::UpvarCapture::ByRef(..) => {}
10341051
}
10351052
}
10361053

1054+
debug!(?projections_list);
1055+
debug!(?diagnostics_info);
1056+
10371057
let is_moved = !projections_list.is_empty();
1058+
debug!(?is_moved);
10381059

10391060
let is_not_completely_captured =
10401061
root_var_min_capture_list.iter().any(|capture| !capture.place.projections.is_empty());
1062+
debug!(?is_not_completely_captured);
10411063

10421064
if is_moved
10431065
&& is_not_completely_captured
@@ -1070,6 +1092,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10701092
/// Returns a tuple containing a vector of MigrationDiagnosticInfo, as well as a String
10711093
/// containing the reason why root variables whose HirId is contained in the vector should
10721094
/// be captured
1095+
#[instrument(level = "debug", skip(self))]
10731096
fn compute_2229_migrations(
10741097
&self,
10751098
closure_def_id: DefId,
@@ -1137,14 +1160,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11371160
// auto trait implementation issues
11381161
auto_trait_migration_reasons.extend(capture_trait_reasons.clone());
11391162

1140-
responsible_captured_hir_ids.push((
1141-
captured_info.0,
1142-
captured_info.1.clone(),
1143-
self.compute_2229_migrations_reasons(
1144-
capture_trait_reasons,
1145-
capture_drop_reorder_reason,
1146-
),
1147-
));
1163+
match captured_info {
1164+
CapturesInfo::CapturingLess { source_expr, var_name } => {
1165+
responsible_captured_hir_ids.push((
1166+
*source_expr,
1167+
var_name.clone(),
1168+
self.compute_2229_migrations_reasons(
1169+
capture_trait_reasons,
1170+
capture_drop_reorder_reason,
1171+
),
1172+
));
1173+
}
1174+
}
11481175
}
11491176

11501177
if !capture_diagnostic.is_empty() {
@@ -2095,6 +2122,7 @@ fn var_name(tcx: TyCtxt<'_>, var_hir_id: hir::HirId) -> Symbol {
20952122
tcx.hir().name(var_hir_id)
20962123
}
20972124

2125+
#[instrument(level = "debug", skip(tcx))]
20982126
fn should_do_rust_2021_incompatible_closure_captures_analysis(
20992127
tcx: TyCtxt<'_>,
21002128
closure_id: hir::HirId,

0 commit comments

Comments
 (0)