1
Fork 0

Put a DefId in AggregateKind.

This commit is contained in:
Camille GILLOT 2023-01-23 22:15:33 +00:00
parent f3126500f2
commit 0241c29123
10 changed files with 33 additions and 17 deletions

View file

@ -2645,6 +2645,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
operands, operands,
) = rvalue ) = rvalue
{ {
let def_id = def_id.expect_local();
for operand in operands { for operand in operands {
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else { let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else {
continue; continue;
@ -2667,7 +2668,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// into a place then we should annotate the closure in // into a place then we should annotate the closure in
// case it ends up being assigned into the return place. // case it ends up being assigned into the return place.
annotated_closure = annotated_closure =
self.annotate_fn_sig(*def_id, substs.as_closure().sig()); self.annotate_fn_sig(def_id, substs.as_closure().sig());
debug!( debug!(
"annotate_argument_and_return_for_borrow: \ "annotate_argument_and_return_for_borrow: \
annotated_closure={:?} assigned_from_local={:?} \ annotated_closure={:?} assigned_from_local={:?} \

View file

@ -817,6 +817,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&& let AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) = **kind && let AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) = **kind
{ {
debug!("move_spans: def_id={:?} places={:?}", def_id, places); debug!("move_spans: def_id={:?} places={:?}", def_id, places);
let def_id = def_id.expect_local();
if let Some((args_span, generator_kind, capture_kind_span, path_span)) = 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)
{ {
@ -945,6 +946,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
box AggregateKind::Generator(def_id, _, _) => (def_id, true), box AggregateKind::Generator(def_id, _, _) => (def_id, true),
_ => continue, _ => continue,
}; };
let def_id = def_id.expect_local();
debug!( debug!(
"borrow_spans: def_id={:?} is_generator={:?} places={:?}", "borrow_spans: def_id={:?} is_generator={:?} places={:?}",

View file

@ -1278,6 +1278,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// in order to populate our used_mut set. // in order to populate our used_mut set.
match **aggregate_kind { match **aggregate_kind {
AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => { AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
let def_id = def_id.expect_local();
let BorrowCheckResult { used_mut_upvars, .. } = let BorrowCheckResult { used_mut_upvars, .. } =
self.infcx.tcx.mir_borrowck(def_id); self.infcx.tcx.mir_borrowck(def_id);
debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars); debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);

View file

@ -2536,7 +2536,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// clauses on the struct. // clauses on the struct.
AggregateKind::Closure(def_id, substs) AggregateKind::Closure(def_id, substs)
| AggregateKind::Generator(def_id, substs, _) => { | AggregateKind::Generator(def_id, substs, _) => {
(def_id.to_def_id(), self.prove_closure_bounds(tcx, def_id, substs, location)) (def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), substs, location))
} }
AggregateKind::Array(_) | AggregateKind::Tuple => { AggregateKind::Array(_) | AggregateKind::Tuple => {

View file

@ -453,7 +453,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
Rvalue::Aggregate(kind, ..) => { Rvalue::Aggregate(kind, ..) => {
if let AggregateKind::Generator(def_id, ..) = kind.as_ref() if let AggregateKind::Generator(def_id, ..) = kind.as_ref()
&& let Some(generator_kind @ hir::GeneratorKind::Async(..)) = self.tcx.generator_kind(def_id.to_def_id()) && let Some(generator_kind @ hir::GeneratorKind::Async(..)) = self.tcx.generator_kind(def_id)
{ {
self.check_op(ops::Generator(generator_kind)); self.check_op(ops::Generator(generator_kind));
} }

View file

@ -2098,10 +2098,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
let name = if tcx.sess.opts.unstable_opts.span_free_formats { let name = if tcx.sess.opts.unstable_opts.span_free_formats {
let substs = tcx.lift(substs).unwrap(); let substs = tcx.lift(substs).unwrap();
format!( format!("[closure@{}]", tcx.def_path_str_with_substs(def_id, substs),)
"[closure@{}]",
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
)
} else { } else {
let span = tcx.def_span(def_id); let span = tcx.def_span(def_id);
format!( format!(
@ -2112,11 +2109,17 @@ impl<'tcx> Debug for Rvalue<'tcx> {
let mut struct_fmt = fmt.debug_struct(&name); let mut struct_fmt = fmt.debug_struct(&name);
// FIXME(project-rfc-2229#48): This should be a list of capture names/places // FIXME(project-rfc-2229#48): This should be a list of capture names/places
if let Some(upvars) = tcx.upvars_mentioned(def_id) { if let Some(def_id) = def_id.as_local()
&& let Some(upvars) = tcx.upvars_mentioned(def_id)
{
for (&var_id, place) in iter::zip(upvars.keys(), places) { for (&var_id, place) in iter::zip(upvars.keys(), places) {
let var_name = tcx.hir().name(var_id); let var_name = tcx.hir().name(var_id);
struct_fmt.field(var_name.as_str(), place); struct_fmt.field(var_name.as_str(), place);
} }
} else {
for (index, place) in places.iter().enumerate() {
struct_fmt.field(&format!("{index}"), place);
}
} }
struct_fmt.finish() struct_fmt.finish()
@ -2127,11 +2130,17 @@ impl<'tcx> Debug for Rvalue<'tcx> {
let mut struct_fmt = fmt.debug_struct(&name); let mut struct_fmt = fmt.debug_struct(&name);
// FIXME(project-rfc-2229#48): This should be a list of capture names/places // FIXME(project-rfc-2229#48): This should be a list of capture names/places
if let Some(upvars) = tcx.upvars_mentioned(def_id) { if let Some(def_id) = def_id.as_local()
&& let Some(upvars) = tcx.upvars_mentioned(def_id)
{
for (&var_id, place) in iter::zip(upvars.keys(), places) { for (&var_id, place) in iter::zip(upvars.keys(), places) {
let var_name = tcx.hir().name(var_id); let var_name = tcx.hir().name(var_id);
struct_fmt.field(var_name.as_str(), place); struct_fmt.field(var_name.as_str(), place);
} }
} else {
for (index, place) in places.iter().enumerate() {
struct_fmt.field(&format!("{index}"), place);
}
} }
struct_fmt.finish() struct_fmt.finish()

View file

@ -1203,10 +1203,8 @@ pub enum AggregateKind<'tcx> {
/// active field index would identity the field `c` /// active field index would identity the field `c`
Adt(DefId, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>), Adt(DefId, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
// Note: We can use LocalDefId since closures and generators a deaggregated Closure(DefId, SubstsRef<'tcx>),
// before codegen. Generator(DefId, SubstsRef<'tcx>, hir::Movability),
Closure(LocalDefId, SubstsRef<'tcx>),
Generator(LocalDefId, SubstsRef<'tcx>, hir::Movability),
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)] #[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]

View file

@ -205,9 +205,9 @@ impl<'tcx> Rvalue<'tcx> {
AggregateKind::Adt(did, _, substs, _, _) => { AggregateKind::Adt(did, _, substs, _, _) => {
tcx.bound_type_of(did).subst(tcx, substs) tcx.bound_type_of(did).subst(tcx, substs)
} }
AggregateKind::Closure(did, substs) => tcx.mk_closure(did.to_def_id(), substs), AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
AggregateKind::Generator(did, substs, movability) => { AggregateKind::Generator(did, substs, movability) => {
tcx.mk_generator(did.to_def_id(), substs, movability) tcx.mk_generator(did, substs, movability)
} }
}, },
Rvalue::ShallowInitBox(_, ty) => tcx.mk_box(ty), Rvalue::ShallowInitBox(_, ty) => tcx.mk_box(ty),

View file

@ -439,10 +439,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// We implicitly set the discriminant to 0. See // We implicitly set the discriminant to 0. See
// librustc_mir/transform/deaggregator.rs for details. // librustc_mir/transform/deaggregator.rs for details.
let movability = movability.unwrap(); let movability = movability.unwrap();
Box::new(AggregateKind::Generator(closure_id, substs, movability)) Box::new(AggregateKind::Generator(
closure_id.to_def_id(),
substs,
movability,
))
} }
UpvarSubsts::Closure(substs) => { UpvarSubsts::Closure(substs) => {
Box::new(AggregateKind::Closure(closure_id, substs)) Box::new(AggregateKind::Closure(closure_id.to_def_id(), substs))
} }
}; };
block.and(Rvalue::Aggregate(result, operands)) block.and(Rvalue::Aggregate(result, operands))

View file

@ -126,6 +126,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
} }
} }
&AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => { &AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => {
let def_id = def_id.expect_local();
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } = let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
self.tcx.unsafety_check_result(def_id); self.tcx.unsafety_check_result(def_id);
self.register_violations(violations, used_unsafe_blocks.iter().copied()); self.register_violations(violations, used_unsafe_blocks.iter().copied());