Skip to content

Commit 403c1b3

Browse files
authored
Rollup merge of #99186 - camsteffen:closure-localdefid, r=cjgillot
Use LocalDefId for closures more
2 parents 482153b + cf2433a commit 403c1b3

File tree

29 files changed

+165
-172
lines changed

29 files changed

+165
-172
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_errors::{
66
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
77
};
88
use rustc_hir as hir;
9-
use rustc_hir::def_id::DefId;
109
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
1110
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
1211
use rustc_infer::infer::TyCtxtInferExt;
@@ -21,6 +20,7 @@ use rustc_middle::ty::{
2120
self, subst::Subst, suggest_constraining_type_params, EarlyBinder, PredicateKind, Ty,
2221
};
2322
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
23+
use rustc_span::def_id::LocalDefId;
2424
use rustc_span::hygiene::DesugaringKind;
2525
use rustc_span::symbol::sym;
2626
use rustc_span::{BytePos, Span, Symbol};
@@ -2223,7 +2223,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22232223
let ty = self.infcx.tcx.type_of(self.mir_def_id());
22242224
match ty.kind() {
22252225
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
2226-
self.mir_def_id().to_def_id(),
2226+
self.mir_def_id(),
22272227
self.infcx.tcx.fn_sig(self.mir_def_id()),
22282228
),
22292229
_ => None,
@@ -2267,8 +2267,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22672267
// Check if our `target` was captured by a closure.
22682268
if let Rvalue::Aggregate(
22692269
box AggregateKind::Closure(def_id, substs),
2270-
operands,
2271-
) = rvalue
2270+
ref operands,
2271+
) = *rvalue
22722272
{
22732273
for operand in operands {
22742274
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else {
@@ -2292,7 +2292,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22922292
// into a place then we should annotate the closure in
22932293
// case it ends up being assigned into the return place.
22942294
annotated_closure =
2295-
self.annotate_fn_sig(*def_id, substs.as_closure().sig());
2295+
self.annotate_fn_sig(def_id, substs.as_closure().sig());
22962296
debug!(
22972297
"annotate_argument_and_return_for_borrow: \
22982298
annotated_closure={:?} assigned_from_local={:?} \
@@ -2414,12 +2414,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24142414
/// references.
24152415
fn annotate_fn_sig(
24162416
&self,
2417-
did: DefId,
2417+
did: LocalDefId,
24182418
sig: ty::PolyFnSig<'tcx>,
24192419
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
24202420
debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
2421-
let is_closure = self.infcx.tcx.is_closure(did);
2422-
let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did.as_local()?);
2421+
let is_closure = self.infcx.tcx.is_closure(did.to_def_id());
2422+
let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did);
24232423
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
24242424

24252425
// We need to work out which arguments to highlight. We do this by looking

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use rustc_const_eval::util::{call_kind, CallDesugaringKind};
55
use rustc_errors::{Applicability, Diagnostic};
66
use rustc_hir as hir;
77
use rustc_hir::def::Namespace;
8-
use rustc_hir::def_id::DefId;
98
use rustc_hir::GeneratorKind;
109
use rustc_infer::infer::TyCtxtInferExt;
10+
use rustc_middle::mir::tcx::PlaceTy;
1111
use rustc_middle::mir::{
1212
AggregateKind, Constant, FakeReadCause, Field, Local, LocalInfo, LocalKind, Location, Operand,
1313
Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
1414
};
1515
use rustc_middle::ty::print::Print;
1616
use rustc_middle::ty::{self, DefIdTree, Instance, Ty, TyCtxt};
1717
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult};
18+
use rustc_span::def_id::LocalDefId;
1819
use rustc_span::{symbol::sym, Span, DUMMY_SP};
1920
use rustc_target::abi::VariantIdx;
2021
use rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions;
@@ -41,7 +42,6 @@ pub(crate) use outlives_suggestion::OutlivesSuggestionBuilder;
4142
pub(crate) use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
4243
pub(crate) use region_name::{RegionName, RegionNameSource};
4344
pub(crate) use rustc_const_eval::util::CallKind;
44-
use rustc_middle::mir::tcx::PlaceTy;
4545

4646
pub(super) struct IncludingDowncast(pub(super) bool);
4747

@@ -325,10 +325,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
325325
// so it's safe to call `expect_local`.
326326
//
327327
// We know the field exists so it's safe to call operator[] and `unwrap` here.
328+
let def_id = def_id.expect_local();
328329
let var_id = self
329330
.infcx
330331
.tcx
331-
.typeck(def_id.expect_local())
332+
.typeck(def_id)
332333
.closure_min_captures_flattened(def_id)
333334
.nth(field.index())
334335
.unwrap()
@@ -715,12 +716,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
715716

716717
debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
717718
if let StatementKind::Assign(box (_, Rvalue::Aggregate(ref kind, ref places))) = stmt.kind {
718-
match kind {
719-
box AggregateKind::Closure(def_id, _)
720-
| box AggregateKind::Generator(def_id, _, _) => {
719+
match **kind {
720+
AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
721721
debug!("move_spans: def_id={:?} places={:?}", def_id, places);
722722
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
723-
self.closure_span(*def_id, moved_place, places)
723+
self.closure_span(def_id, moved_place, places)
724724
{
725725
return ClosureUse {
726726
generator_kind,
@@ -847,7 +847,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
847847
if let StatementKind::Assign(box (_, Rvalue::Aggregate(ref kind, ref places))) =
848848
stmt.kind
849849
{
850-
let (def_id, is_generator) = match kind {
850+
let (&def_id, is_generator) = match kind {
851851
box AggregateKind::Closure(def_id, _) => (def_id, false),
852852
box AggregateKind::Generator(def_id, _, _) => (def_id, true),
853853
_ => continue,
@@ -858,7 +858,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
858858
def_id, is_generator, places
859859
);
860860
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
861-
self.closure_span(*def_id, Place::from(target).as_ref(), places)
861+
self.closure_span(def_id, Place::from(target).as_ref(), places)
862862
{
863863
return ClosureUse { generator_kind, args_span, capture_kind_span, path_span };
864864
} else {
@@ -879,25 +879,20 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
879879
/// The second span is the location the use resulting in the captured path of the capture
880880
fn closure_span(
881881
&self,
882-
def_id: DefId,
882+
def_id: LocalDefId,
883883
target_place: PlaceRef<'tcx>,
884884
places: &[Operand<'tcx>],
885885
) -> Option<(Span, Option<GeneratorKind>, Span, Span)> {
886886
debug!(
887887
"closure_span: def_id={:?} target_place={:?} places={:?}",
888888
def_id, target_place, places
889889
);
890-
let local_did = def_id.as_local()?;
891-
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(local_did);
890+
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(def_id);
892891
let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
893892
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
894893
if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }) = expr {
895-
for (captured_place, place) in self
896-
.infcx
897-
.tcx
898-
.typeck(def_id.expect_local())
899-
.closure_min_captures_flattened(def_id)
900-
.zip(places)
894+
for (captured_place, place) in
895+
self.infcx.tcx.typeck(def_id).closure_min_captures_flattened(def_id).zip(places)
901896
{
902897
match place {
903898
Operand::Copy(place) | Operand::Move(place)

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
343343
);
344344
let tcx = self.infcx.tcx;
345345
if let ty::Closure(id, _) = *the_place_err.ty(self.body, tcx).ty.kind() {
346-
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
346+
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
347347
}
348348
}
349349

@@ -382,7 +382,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
382382
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
383383
&& let ty::Closure(id, _) = *ty.kind()
384384
{
385-
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
385+
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
386386
}
387387
}
388388

@@ -685,11 +685,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
685685
fn show_mutating_upvar(
686686
&self,
687687
tcx: TyCtxt<'_>,
688-
id: hir::def_id::DefId,
688+
closure_local_def_id: hir::def_id::LocalDefId,
689689
the_place_err: PlaceRef<'tcx>,
690690
err: &mut Diagnostic,
691691
) {
692-
let closure_local_def_id = id.expect_local();
693692
let tables = tcx.typeck(closure_local_def_id);
694693
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_local_def_id);
695694
if let Some((span, closure_kind_origin)) =
@@ -699,7 +698,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
699698
let upvar = ty::place_to_string_for_capture(tcx, closure_kind_origin);
700699
let root_hir_id = upvar_id.var_path.hir_id;
701700
// we have an origin for this closure kind starting at this root variable so it's safe to unwrap here
702-
let captured_places = tables.closure_min_captures[&id].get(&root_hir_id).unwrap();
701+
let captured_places =
702+
tables.closure_min_captures[&closure_local_def_id].get(&root_hir_id).unwrap();
703703

704704
let origin_projection = closure_kind_origin
705705
.projections

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl Display for RegionName {
189189

190190
impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
191191
pub(crate) fn mir_def_id(&self) -> hir::def_id::LocalDefId {
192-
self.body.source.def_id().as_local().unwrap()
192+
self.body.source.def_id().expect_local()
193193
}
194194

195195
pub(crate) fn mir_hir_id(&self) -> hir::HirId {

Diff for: compiler/rustc_borrowck/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn do_mir_borrowck<'a, 'tcx>(
189189
errors.set_tainted_by_errors();
190190
}
191191
let upvars: Vec<_> = tables
192-
.closure_min_captures_flattened(def.did.to_def_id())
192+
.closure_min_captures_flattened(def.did)
193193
.map(|captured_place| {
194194
let capture = captured_place.info.capture_kind;
195195
let by_ref = match capture {
@@ -1295,7 +1295,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12951295
match **aggregate_kind {
12961296
AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
12971297
let BorrowCheckResult { used_mut_upvars, .. } =
1298-
self.infcx.tcx.mir_borrowck(def_id.expect_local());
1298+
self.infcx.tcx.mir_borrowck(def_id);
12991299
debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);
13001300
for field in used_mut_upvars {
13011301
self.propagate_closure_used_mut_upvar(&operands[field.index()]);

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1847,14 +1847,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18471847
let tcx = self.tcx();
18481848
let def_id = uv.def.def_id_for_type_of();
18491849
if tcx.def_kind(def_id) == DefKind::InlineConst {
1850-
let predicates = self.prove_closure_bounds(
1851-
tcx,
1852-
def_id.expect_local(),
1853-
uv.substs,
1854-
location,
1855-
);
1850+
let def_id = def_id.expect_local();
1851+
let predicates =
1852+
self.prove_closure_bounds(tcx, def_id, uv.substs, location);
18561853
self.normalize_and_prove_instantiated_predicates(
1857-
def_id,
1854+
def_id.to_def_id(),
18581855
predicates,
18591856
location.to_locations(),
18601857
);
@@ -2514,9 +2511,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25142511
aggregate_kind, location
25152512
);
25162513

2517-
let (def_id, instantiated_predicates) = match aggregate_kind {
2514+
let (def_id, instantiated_predicates) = match *aggregate_kind {
25182515
AggregateKind::Adt(adt_did, _, substs, _, _) => {
2519-
(*adt_did, tcx.predicates_of(*adt_did).instantiate(tcx, substs))
2516+
(adt_did, tcx.predicates_of(adt_did).instantiate(tcx, substs))
25202517
}
25212518

25222519
// For closures, we have some **extra requirements** we
@@ -2541,7 +2538,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25412538
// clauses on the struct.
25422539
AggregateKind::Closure(def_id, substs)
25432540
| AggregateKind::Generator(def_id, substs, _) => {
2544-
(*def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), substs, location))
2541+
(def_id.to_def_id(), self.prove_closure_bounds(tcx, def_id, substs, location))
25452542
}
25462543

25472544
AggregateKind::Array(_) | AggregateKind::Tuple => {

Diff for: compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
238238
if let Some(local_def_id) = def_id.as_local() {
239239
let tables = self.ecx.tcx.typeck(local_def_id);
240240
if let Some(captured_place) =
241-
tables.closure_min_captures_flattened(*def_id).nth(field)
241+
tables.closure_min_captures_flattened(local_def_id).nth(field)
242242
{
243243
// Sometimes the index is beyond the number of upvars (seen
244244
// for a generator).

Diff for: compiler/rustc_middle/src/mir/mod.rs

+32-40
Original file line numberDiff line numberDiff line change
@@ -1983,53 +1983,45 @@ impl<'tcx> Debug for Rvalue<'tcx> {
19831983
}
19841984

19851985
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
1986-
if let Some(def_id) = def_id.as_local() {
1987-
let name = if tcx.sess.opts.unstable_opts.span_free_formats {
1988-
let substs = tcx.lift(substs).unwrap();
1989-
format!(
1990-
"[closure@{}]",
1991-
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
1992-
)
1993-
} else {
1994-
let span = tcx.def_span(def_id);
1995-
format!(
1996-
"[closure@{}]",
1997-
tcx.sess.source_map().span_to_diagnostic_string(span)
1998-
)
1999-
};
2000-
let mut struct_fmt = fmt.debug_struct(&name);
2001-
2002-
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
2003-
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2004-
for (&var_id, place) in iter::zip(upvars.keys(), places) {
2005-
let var_name = tcx.hir().name(var_id);
2006-
struct_fmt.field(var_name.as_str(), place);
2007-
}
2008-
}
2009-
2010-
struct_fmt.finish()
1986+
let name = if tcx.sess.opts.unstable_opts.span_free_formats {
1987+
let substs = tcx.lift(substs).unwrap();
1988+
format!(
1989+
"[closure@{}]",
1990+
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
1991+
)
20111992
} else {
2012-
write!(fmt, "[closure]")
1993+
let span = tcx.def_span(def_id);
1994+
format!(
1995+
"[closure@{}]",
1996+
tcx.sess.source_map().span_to_diagnostic_string(span)
1997+
)
1998+
};
1999+
let mut struct_fmt = fmt.debug_struct(&name);
2000+
2001+
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
2002+
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2003+
for (&var_id, place) in iter::zip(upvars.keys(), places) {
2004+
let var_name = tcx.hir().name(var_id);
2005+
struct_fmt.field(var_name.as_str(), place);
2006+
}
20132007
}
2008+
2009+
struct_fmt.finish()
20142010
}),
20152011

20162012
AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
2017-
if let Some(def_id) = def_id.as_local() {
2018-
let name = format!("[generator@{:?}]", tcx.def_span(def_id));
2019-
let mut struct_fmt = fmt.debug_struct(&name);
2020-
2021-
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
2022-
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2023-
for (&var_id, place) in iter::zip(upvars.keys(), places) {
2024-
let var_name = tcx.hir().name(var_id);
2025-
struct_fmt.field(var_name.as_str(), place);
2026-
}
2013+
let name = format!("[generator@{:?}]", tcx.def_span(def_id));
2014+
let mut struct_fmt = fmt.debug_struct(&name);
2015+
2016+
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
2017+
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2018+
for (&var_id, place) in iter::zip(upvars.keys(), places) {
2019+
let var_name = tcx.hir().name(var_id);
2020+
struct_fmt.field(var_name.as_str(), place);
20272021
}
2028-
2029-
struct_fmt.finish()
2030-
} else {
2031-
write!(fmt, "[generator]")
20322022
}
2023+
2024+
struct_fmt.finish()
20332025
}),
20342026
}
20352027
}

0 commit comments

Comments
 (0)