Rollup merge of #99186 - camsteffen:closure-localdefid, r=cjgillot
Use LocalDefId for closures more
This commit is contained in:
commit
403c1b3802
29 changed files with 164 additions and 171 deletions
|
@ -6,7 +6,6 @@ use rustc_errors::{
|
|||
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
|
||||
};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
|
||||
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
|
@ -21,6 +20,7 @@ use rustc_middle::ty::{
|
|||
self, subst::Subst, suggest_constraining_type_params, EarlyBinder, PredicateKind, Ty,
|
||||
};
|
||||
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::hygiene::DesugaringKind;
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::{BytePos, Span, Symbol};
|
||||
|
@ -2223,7 +2223,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
let ty = self.infcx.tcx.type_of(self.mir_def_id());
|
||||
match ty.kind() {
|
||||
ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
|
||||
self.mir_def_id().to_def_id(),
|
||||
self.mir_def_id(),
|
||||
self.infcx.tcx.fn_sig(self.mir_def_id()),
|
||||
),
|
||||
_ => None,
|
||||
|
@ -2267,8 +2267,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
// Check if our `target` was captured by a closure.
|
||||
if let Rvalue::Aggregate(
|
||||
box AggregateKind::Closure(def_id, substs),
|
||||
operands,
|
||||
) = rvalue
|
||||
ref operands,
|
||||
) = *rvalue
|
||||
{
|
||||
for operand in operands {
|
||||
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else {
|
||||
|
@ -2292,7 +2292,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
// into a place then we should annotate the closure in
|
||||
// case it ends up being assigned into the return place.
|
||||
annotated_closure =
|
||||
self.annotate_fn_sig(*def_id, substs.as_closure().sig());
|
||||
self.annotate_fn_sig(def_id, substs.as_closure().sig());
|
||||
debug!(
|
||||
"annotate_argument_and_return_for_borrow: \
|
||||
annotated_closure={:?} assigned_from_local={:?} \
|
||||
|
@ -2414,12 +2414,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
/// references.
|
||||
fn annotate_fn_sig(
|
||||
&self,
|
||||
did: DefId,
|
||||
did: LocalDefId,
|
||||
sig: ty::PolyFnSig<'tcx>,
|
||||
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
|
||||
debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
|
||||
let is_closure = self.infcx.tcx.is_closure(did);
|
||||
let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did.as_local()?);
|
||||
let is_closure = self.infcx.tcx.is_closure(did.to_def_id());
|
||||
let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did);
|
||||
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
|
||||
|
||||
// We need to work out which arguments to highlight. We do this by looking
|
||||
|
|
|
@ -5,9 +5,9 @@ use rustc_const_eval::util::{call_kind, CallDesugaringKind};
|
|||
use rustc_errors::{Applicability, Diagnostic};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::Namespace;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::GeneratorKind;
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_middle::mir::tcx::PlaceTy;
|
||||
use rustc_middle::mir::{
|
||||
AggregateKind, Constant, FakeReadCause, Field, Local, LocalInfo, LocalKind, Location, Operand,
|
||||
Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
|
||||
|
@ -15,6 +15,7 @@ use rustc_middle::mir::{
|
|||
use rustc_middle::ty::print::Print;
|
||||
use rustc_middle::ty::{self, DefIdTree, Instance, Ty, TyCtxt};
|
||||
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult};
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::{symbol::sym, Span, DUMMY_SP};
|
||||
use rustc_target::abi::VariantIdx;
|
||||
use rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions;
|
||||
|
@ -41,7 +42,6 @@ pub(crate) use outlives_suggestion::OutlivesSuggestionBuilder;
|
|||
pub(crate) use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
|
||||
pub(crate) use region_name::{RegionName, RegionNameSource};
|
||||
pub(crate) use rustc_const_eval::util::CallKind;
|
||||
use rustc_middle::mir::tcx::PlaceTy;
|
||||
|
||||
pub(super) struct IncludingDowncast(pub(super) bool);
|
||||
|
||||
|
@ -325,10 +325,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
// so it's safe to call `expect_local`.
|
||||
//
|
||||
// We know the field exists so it's safe to call operator[] and `unwrap` here.
|
||||
let def_id = def_id.expect_local();
|
||||
let var_id = self
|
||||
.infcx
|
||||
.tcx
|
||||
.typeck(def_id.expect_local())
|
||||
.typeck(def_id)
|
||||
.closure_min_captures_flattened(def_id)
|
||||
.nth(field.index())
|
||||
.unwrap()
|
||||
|
@ -715,12 +716,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
|
||||
debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
|
||||
if let StatementKind::Assign(box (_, Rvalue::Aggregate(ref kind, ref places))) = stmt.kind {
|
||||
match kind {
|
||||
box AggregateKind::Closure(def_id, _)
|
||||
| box AggregateKind::Generator(def_id, _, _) => {
|
||||
match **kind {
|
||||
AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
|
||||
debug!("move_spans: def_id={:?} places={:?}", def_id, places);
|
||||
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
|
||||
self.closure_span(*def_id, moved_place, places)
|
||||
self.closure_span(def_id, moved_place, places)
|
||||
{
|
||||
return ClosureUse {
|
||||
generator_kind,
|
||||
|
@ -847,7 +847,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
if let StatementKind::Assign(box (_, Rvalue::Aggregate(ref kind, ref places))) =
|
||||
stmt.kind
|
||||
{
|
||||
let (def_id, is_generator) = match kind {
|
||||
let (&def_id, is_generator) = match kind {
|
||||
box AggregateKind::Closure(def_id, _) => (def_id, false),
|
||||
box AggregateKind::Generator(def_id, _, _) => (def_id, true),
|
||||
_ => continue,
|
||||
|
@ -858,7 +858,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
def_id, is_generator, places
|
||||
);
|
||||
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
|
||||
self.closure_span(*def_id, Place::from(target).as_ref(), places)
|
||||
self.closure_span(def_id, Place::from(target).as_ref(), places)
|
||||
{
|
||||
return ClosureUse { generator_kind, args_span, capture_kind_span, path_span };
|
||||
} else {
|
||||
|
@ -879,7 +879,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
/// The second span is the location the use resulting in the captured path of the capture
|
||||
fn closure_span(
|
||||
&self,
|
||||
def_id: DefId,
|
||||
def_id: LocalDefId,
|
||||
target_place: PlaceRef<'tcx>,
|
||||
places: &[Operand<'tcx>],
|
||||
) -> Option<(Span, Option<GeneratorKind>, Span, Span)> {
|
||||
|
@ -887,17 +887,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
"closure_span: def_id={:?} target_place={:?} places={:?}",
|
||||
def_id, target_place, places
|
||||
);
|
||||
let local_did = def_id.as_local()?;
|
||||
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(local_did);
|
||||
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(def_id);
|
||||
let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
|
||||
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
|
||||
if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }) = expr {
|
||||
for (captured_place, place) in self
|
||||
.infcx
|
||||
.tcx
|
||||
.typeck(def_id.expect_local())
|
||||
.closure_min_captures_flattened(def_id)
|
||||
.zip(places)
|
||||
for (captured_place, place) in
|
||||
self.infcx.tcx.typeck(def_id).closure_min_captures_flattened(def_id).zip(places)
|
||||
{
|
||||
match place {
|
||||
Operand::Copy(place) | Operand::Move(place)
|
||||
|
|
|
@ -343,7 +343,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||
);
|
||||
let tcx = self.infcx.tcx;
|
||||
if let ty::Closure(id, _) = *the_place_err.ty(self.body, tcx).ty.kind() {
|
||||
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
|
||||
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -382,7 +382,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
|
||||
&& let ty::Closure(id, _) = *ty.kind()
|
||||
{
|
||||
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
|
||||
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -685,11 +685,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||
fn show_mutating_upvar(
|
||||
&self,
|
||||
tcx: TyCtxt<'_>,
|
||||
id: hir::def_id::DefId,
|
||||
closure_local_def_id: hir::def_id::LocalDefId,
|
||||
the_place_err: PlaceRef<'tcx>,
|
||||
err: &mut Diagnostic,
|
||||
) {
|
||||
let closure_local_def_id = id.expect_local();
|
||||
let tables = tcx.typeck(closure_local_def_id);
|
||||
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_local_def_id);
|
||||
if let Some((span, closure_kind_origin)) =
|
||||
|
@ -699,7 +698,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||
let upvar = ty::place_to_string_for_capture(tcx, closure_kind_origin);
|
||||
let root_hir_id = upvar_id.var_path.hir_id;
|
||||
// we have an origin for this closure kind starting at this root variable so it's safe to unwrap here
|
||||
let captured_places = tables.closure_min_captures[&id].get(&root_hir_id).unwrap();
|
||||
let captured_places =
|
||||
tables.closure_min_captures[&closure_local_def_id].get(&root_hir_id).unwrap();
|
||||
|
||||
let origin_projection = closure_kind_origin
|
||||
.projections
|
||||
|
|
|
@ -189,7 +189,7 @@ impl Display for RegionName {
|
|||
|
||||
impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
|
||||
pub(crate) fn mir_def_id(&self) -> hir::def_id::LocalDefId {
|
||||
self.body.source.def_id().as_local().unwrap()
|
||||
self.body.source.def_id().expect_local()
|
||||
}
|
||||
|
||||
pub(crate) fn mir_hir_id(&self) -> hir::HirId {
|
||||
|
|
|
@ -189,7 +189,7 @@ fn do_mir_borrowck<'a, 'tcx>(
|
|||
errors.set_tainted_by_errors();
|
||||
}
|
||||
let upvars: Vec<_> = tables
|
||||
.closure_min_captures_flattened(def.did.to_def_id())
|
||||
.closure_min_captures_flattened(def.did)
|
||||
.map(|captured_place| {
|
||||
let capture = captured_place.info.capture_kind;
|
||||
let by_ref = match capture {
|
||||
|
@ -1295,7 +1295,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
match **aggregate_kind {
|
||||
AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
|
||||
let BorrowCheckResult { used_mut_upvars, .. } =
|
||||
self.infcx.tcx.mir_borrowck(def_id.expect_local());
|
||||
self.infcx.tcx.mir_borrowck(def_id);
|
||||
debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);
|
||||
for field in used_mut_upvars {
|
||||
self.propagate_closure_used_mut_upvar(&operands[field.index()]);
|
||||
|
|
|
@ -1847,14 +1847,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
|
|||
let tcx = self.tcx();
|
||||
let def_id = uv.def.def_id_for_type_of();
|
||||
if tcx.def_kind(def_id) == DefKind::InlineConst {
|
||||
let predicates = self.prove_closure_bounds(
|
||||
tcx,
|
||||
def_id.expect_local(),
|
||||
uv.substs,
|
||||
location,
|
||||
);
|
||||
let def_id = def_id.expect_local();
|
||||
let predicates =
|
||||
self.prove_closure_bounds(tcx, def_id, uv.substs, location);
|
||||
self.normalize_and_prove_instantiated_predicates(
|
||||
def_id,
|
||||
def_id.to_def_id(),
|
||||
predicates,
|
||||
location.to_locations(),
|
||||
);
|
||||
|
@ -2514,9 +2511,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
|
|||
aggregate_kind, location
|
||||
);
|
||||
|
||||
let (def_id, instantiated_predicates) = match aggregate_kind {
|
||||
let (def_id, instantiated_predicates) = match *aggregate_kind {
|
||||
AggregateKind::Adt(adt_did, _, substs, _, _) => {
|
||||
(*adt_did, tcx.predicates_of(*adt_did).instantiate(tcx, substs))
|
||||
(adt_did, tcx.predicates_of(adt_did).instantiate(tcx, substs))
|
||||
}
|
||||
|
||||
// For closures, we have some **extra requirements** we
|
||||
|
@ -2541,7 +2538,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
|
|||
// clauses on the struct.
|
||||
AggregateKind::Closure(def_id, substs)
|
||||
| AggregateKind::Generator(def_id, substs, _) => {
|
||||
(*def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), substs, location))
|
||||
(def_id.to_def_id(), self.prove_closure_bounds(tcx, def_id, substs, location))
|
||||
}
|
||||
|
||||
AggregateKind::Array(_) | AggregateKind::Tuple => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue