Skip to content

ClosureSubsts/GeneratorSubsts refactoring #72633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/librustc_codegen_llvm/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::interpret::truncate;
use rustc_middle::mir::{self, Field, GeneratorLayout};
use rustc_middle::ty::layout::{self, IntegerExt, PrimitiveExt, TyAndLayout};
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::Instance;
use rustc_middle::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::{self, AdtKind, GeneratorSubsts, ParamEnv, Ty, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::config::{self, DebugInfo};
use rustc_span::symbol::{Interner, Symbol};
Expand Down Expand Up @@ -660,8 +660,8 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp
// This is actually a function pointer, so wrap it in pointer DI.
MetadataCreationResult::new(pointer_type_metadata(cx, t, fn_metadata), false)
}
ty::Closure(def_id, substs) => {
let upvar_tys: Vec<_> = substs.as_closure().upvar_tys().collect();
ty::Closure(def_id, closure_substs) => {
let upvar_tys: Vec<_> = closure_substs.upvar_tys().collect();
let containing_scope = get_namespace_for_item(cx, def_id);
prepare_tuple_metadata(
cx,
Expand All @@ -673,9 +673,8 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp
)
.finalize(cx)
}
ty::Generator(def_id, substs, _) => {
let upvar_tys: Vec<_> = substs
.as_generator()
ty::Generator(def_id, generator_substs, _) => {
let upvar_tys: Vec<_> = generator_substs
.prefix_tys()
.map(|t| cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t))
.collect();
Expand Down Expand Up @@ -1356,11 +1355,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {

let variant_info_for = |index: VariantIdx| match self.enum_type.kind {
ty::Adt(adt, _) => VariantInfo::Adt(&adt.variants[index]),
ty::Generator(_, substs, _) => {
ty::Generator(_, generator_substs, _) => {
let (generator_layout, generator_saved_local_names) =
generator_variant_info_data.as_ref().unwrap();
VariantInfo::Generator {
substs,
generator_substs,
generator_layout: *generator_layout,
generator_saved_local_names,
variant_index: index,
Expand Down Expand Up @@ -1653,7 +1652,7 @@ enum EnumDiscriminantInfo<'ll> {
enum VariantInfo<'a, 'tcx> {
Adt(&'tcx ty::VariantDef),
Generator {
substs: SubstsRef<'tcx>,
generator_substs: GeneratorSubsts<'tcx>,
generator_layout: &'tcx GeneratorLayout<'tcx>,
generator_saved_local_names: &'a IndexVec<mir::GeneratorSavedLocal, Option<Symbol>>,
variant_index: VariantIdx,
Expand All @@ -1664,8 +1663,8 @@ impl<'tcx> VariantInfo<'_, 'tcx> {
fn map_struct_name<R>(&self, f: impl FnOnce(&str) -> R) -> R {
match self {
VariantInfo::Adt(variant) => f(&variant.ident.as_str()),
VariantInfo::Generator { substs, variant_index, .. } => {
f(&substs.as_generator().variant_name(*variant_index))
VariantInfo::Generator { generator_substs, variant_index, .. } => {
f(&generator_substs.variant_name(*variant_index))
}
}
}
Expand Down Expand Up @@ -1815,11 +1814,10 @@ fn prepare_enum_metadata(
}
})
.collect(),
ty::Generator(_, substs, _) => substs
.as_generator()
ty::Generator(_, generator_substs, _) => generator_substs
.variant_range(enum_def_id, cx.tcx)
.map(|variant_index| {
let name = substs.as_generator().variant_name(variant_index);
let name = generator_substs.variant_name(variant_index);
unsafe {
Some(llvm::LLVMRustDIBuilderCreateEnumerator(
DIB(cx),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ fn uncached_llvm_type<'a, 'tcx>(
write!(&mut name, "::{}", def.variants[index].ident).unwrap();
}
}
if let (&ty::Generator(_, substs, _), &Variants::Single { index })
if let (&ty::Generator(_, generator_substs, _), &Variants::Single { index })
= (&layout.ty.kind, &layout.variants)
{
write!(&mut name, "::{}", substs.as_generator().variant_name(index)).unwrap();
write!(&mut name, "::{}", generator_substs.variant_name(index)).unwrap();
}
Some(name)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
match operand.layout.ty.kind {
ty::Closure(def_id, substs) => {
ty::Closure(def_id, closure_substs) => {
let instance = Instance::resolve_closure(
bx.cx().tcx(),
def_id,
substs,
closure_substs,
ty::ClosureKind::FnOnce,
);
OperandValue::Immediate(bx.cx().get_fn_addr(instance))
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_infer/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

let ty_msg = match (local_visitor.found_node_ty, local_visitor.found_exact_method_call) {
(_, Some(_)) => String::new(),
(Some(ty::TyS { kind: ty::Closure(_, substs), .. }), _) => {
let fn_sig = substs.as_closure().sig();
(Some(ty::TyS { kind: ty::Closure(_, closure_substs), .. }), _) => {
let fn_sig = closure_substs.sig();
let args = closure_args(&fn_sig);
let ret = fn_sig.output().skip_binder().to_string();
format!(" for the closure `fn({}) -> {}`", args, ret)
Expand Down Expand Up @@ -352,8 +352,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
);

let suffix = match local_visitor.found_node_ty {
Some(ty::TyS { kind: ty::Closure(_, substs), .. }) => {
let fn_sig = substs.as_closure().sig();
Some(ty::TyS { kind: ty::Closure(_, closure_substs), .. }) => {
let fn_sig = closure_substs.sig();
let ret = fn_sig.output().skip_binder().to_string();

let closure_decl_and_body_id =
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_infer/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::relate::RelateResult;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
pub use rustc_middle::ty::IntVarValue;
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
use rustc_middle::ty::{self, ClosureSubsts, GenericParamDefKind, InferConst, Ty, TyCtxt};
use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
use rustc_session::config::BorrowckMode;
use rustc_span::symbol::Symbol;
Expand Down Expand Up @@ -1545,8 +1545,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
/// Obtains the latest type of the given closure; this may be a
/// closure in the current function, in which case its
/// `ClosureKind` may not yet be known.
pub fn closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind> {
let closure_kind_ty = closure_substs.as_closure().kind_ty();
pub fn closure_kind(&self, closure_substs: ClosureSubsts<'tcx>) -> Option<ty::ClosureKind> {
let closure_kind_ty = closure_substs.kind_ty();
let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
closure_kind_ty.to_opt_closure_kind()
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,8 +1329,8 @@ impl EncodeContext<'tcx> {
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]);
self.encode_item_type(def_id.to_def_id());
if let ty::Closure(def_id, substs) = ty.kind {
record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig());
if let ty::Closure(def_id, closure_substs) = ty.kind {
record!(self.tables.fn_sig[def_id] <- closure_substs.sig());
}
self.encode_generics(def_id.to_def_id());
self.encode_optimized_mir(def_id);
Expand Down
55 changes: 29 additions & 26 deletions src/librustc_middle/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use crate::ty::print::{FmtPrinter, Printer};
use crate::ty::subst::{Subst, SubstsRef};
use crate::ty::{
self, AdtDef, CanonicalUserTypeAnnotations, List, Region, Ty, TyCtxt, UserTypeAnnotationIndex,
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, List, Region, Ty,
TyCtxt, UserTypeAnnotationIndex,
};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, Namespace};
Expand Down Expand Up @@ -2258,8 +2259,8 @@ pub enum AggregateKind<'tcx> {
/// active field index would identity the field `c`
Adt(&'tcx AdtDef, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),

Closure(DefId, SubstsRef<'tcx>),
Generator(DefId, SubstsRef<'tcx>, hir::Movability),
Closure(DefId, ClosureSubsts<'tcx>),
Generator(DefId, GeneratorSubsts<'tcx>, hir::Movability),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
Expand Down Expand Up @@ -2425,32 +2426,34 @@ impl<'tcx> Debug for Rvalue<'tcx> {
}
}

AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
let hir_id = tcx.hir().as_local_hir_id(def_id);
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
let substs = tcx.lift(&substs).unwrap();
format!(
"[closure@{}]",
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
)
} else {
format!("[closure@{:?}]", tcx.hir().span(hir_id))
};
let mut struct_fmt = fmt.debug_struct(&name);
AggregateKind::Closure(def_id, ClosureSubsts { substs }) => {
ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
let hir_id = tcx.hir().as_local_hir_id(def_id);
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
let substs = tcx.lift(&substs).unwrap();
format!(
"[closure@{}]",
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
)
} else {
format!("[closure@{:?}]", tcx.hir().span(hir_id))
};
let mut struct_fmt = fmt.debug_struct(&name);

if let Some(upvars) = tcx.upvars(def_id) {
for (&var_id, place) in upvars.keys().zip(places) {
let var_name = tcx.hir().name(var_id);
struct_fmt.field(&var_name.as_str(), place);
if let Some(upvars) = tcx.upvars(def_id) {
for (&var_id, place) in upvars.keys().zip(places) {
let var_name = tcx.hir().name(var_id);
struct_fmt.field(&var_name.as_str(), place);
}
}
}

struct_fmt.finish()
} else {
write!(fmt, "[closure]")
}
}),
struct_fmt.finish()
} else {
write!(fmt, "[closure]")
}
})
}

AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
if let Some(def_id) = def_id.as_local() {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_middle/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'tcx> Rvalue<'tcx> {
let ty = place.ty(local_decls, tcx).ty;
match ty.kind {
ty::Adt(adt_def, _) => adt_def.repr.discr_type().to_ty(tcx),
ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx),
ty::Generator(_, generator_substs, _) => generator_substs.discr_ty(tcx),
_ => {
// This can only be `0`, for now, so `u8` will suffice.
tcx.types.u8
Expand All @@ -191,9 +191,9 @@ impl<'tcx> Rvalue<'tcx> {
AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
AggregateKind::Tuple => tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx))),
AggregateKind::Adt(def, _, substs, _, _) => tcx.type_of(def.did).subst(tcx, substs),
AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
AggregateKind::Generator(did, substs, movability) => {
tcx.mk_generator(did, substs, movability)
AggregateKind::Closure(did, closure_substs) => tcx.mk_closure(did, closure_substs),
AggregateKind::Generator(did, generator_substs, movability) => {
tcx.mk_generator(did, generator_substs, movability)
}
},
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_middle/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,16 +684,16 @@ macro_rules! make_mir_visitor {
}
AggregateKind::Closure(
_,
closure_substs
ClosureSubsts { substs }
) => {
self.visit_substs(closure_substs, location);
self.visit_substs(substs, location);
}
AggregateKind::Generator(
_,
generator_substs,
GeneratorSubsts { substs },
_movability,
) => {
self.visit_substs(generator_substs, location);
self.visit_substs(substs, location);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/librustc_middle/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod structural_impls;
use crate::infer::canonical::Canonical;
use crate::mir::interpret::ErrorHandled;
use crate::ty::subst::SubstsRef;
use crate::ty::{self, AdtKind, Ty, TyCtxt};
use crate::ty::{self, AdtKind, ClosureSubsts, GeneratorSubsts, Ty, TyCtxt};

use rustc_hir as hir;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -472,12 +472,12 @@ impl<'tcx, N> Vtable<'tcx, N> {
}),
VtableClosure(c) => VtableClosure(VtableClosureData {
closure_def_id: c.closure_def_id,
substs: c.substs,
closure_substs: c.closure_substs,
nested: c.nested.into_iter().map(f).collect(),
}),
VtableGenerator(c) => VtableGenerator(VtableGeneratorData {
generator_def_id: c.generator_def_id,
substs: c.substs,
generator_substs: c.generator_substs,
nested: c.nested.into_iter().map(f).collect(),
}),
VtableFnPointer(p) => VtableFnPointer(VtableFnPointerData {
Expand Down Expand Up @@ -513,7 +513,7 @@ pub struct VtableImplData<'tcx, N> {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
pub struct VtableGeneratorData<'tcx, N> {
pub generator_def_id: DefId,
pub substs: SubstsRef<'tcx>,
pub generator_substs: GeneratorSubsts<'tcx>,
/// Nested obligations. This can be non-empty if the generator
/// signature contains associated types.
pub nested: Vec<N>,
Expand All @@ -522,7 +522,7 @@ pub struct VtableGeneratorData<'tcx, N> {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable, TypeFoldable)]
pub struct VtableClosureData<'tcx, N> {
pub closure_def_id: DefId,
pub substs: SubstsRef<'tcx>,
pub closure_substs: ClosureSubsts<'tcx>,
/// Nested obligations. This can be non-empty if the closure
/// signature contains associated types.
pub nested: Vec<N>,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/traits/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
// (T1..Tn) and closures have same properties as T1..Tn --
// check if *any* of those are trivial.
ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
ty::Closure(_, ref substs) => {
substs.as_closure().upvar_tys().all(|t| trivial_dropck_outlives(tcx, t))
ty::Closure(_, ref closure_substs) => {
closure_substs.upvar_tys().all(|t| trivial_dropck_outlives(tcx, t))
}

ty::Adt(def, _) => {
Expand Down
28 changes: 15 additions & 13 deletions src/librustc_middle/traits/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableGeneratorData<'tcx, N> {
write!(
f,
"VtableGeneratorData(generator_def_id={:?}, substs={:?}, nested={:?})",
self.generator_def_id, self.substs, self.nested
self.generator_def_id, self.generator_substs, self.nested
)
}
}
Expand All @@ -55,7 +55,7 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::VtableClosureData<'tcx, N> {
write!(
f,
"VtableClosureData(closure_def_id={:?}, substs={:?}, nested={:?})",
self.closure_def_id, self.substs, self.nested
self.closure_def_id, self.closure_substs, self.nested
)
}
}
Expand Down Expand Up @@ -251,24 +251,26 @@ impl<'a, 'tcx> Lift<'tcx> for traits::Vtable<'a, ()> {
traits::VtableAutoImpl(t) => Some(traits::VtableAutoImpl(t)),
traits::VtableGenerator(traits::VtableGeneratorData {
generator_def_id,
substs,
generator_substs,
nested,
}) => tcx.lift(&substs).map(|substs| {
}) => tcx.lift(&generator_substs).map(|generator_substs| {
traits::VtableGenerator(traits::VtableGeneratorData {
generator_def_id,
substs,
generator_substs,
nested,
})
}),
traits::VtableClosure(traits::VtableClosureData { closure_def_id, substs, nested }) => {
tcx.lift(&substs).map(|substs| {
traits::VtableClosure(traits::VtableClosureData {
closure_def_id,
substs,
nested,
})
traits::VtableClosure(traits::VtableClosureData {
closure_def_id,
closure_substs,
nested,
}) => tcx.lift(&closure_substs).map(|closure_substs| {
traits::VtableClosure(traits::VtableClosureData {
closure_def_id,
closure_substs,
nested,
})
}
}),
traits::VtableFnPointer(traits::VtableFnPointerData { fn_ty, nested }) => {
tcx.lift(&fn_ty).map(|fn_ty| {
traits::VtableFnPointer(traits::VtableFnPointerData { fn_ty, nested })
Expand Down
Loading