Skip to content

Commit

Permalink
Don't keep {Closure,Generator}Substs synthetics in an Instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jul 14, 2020
1 parent 5533705 commit 6e4b8ef
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let instance = Instance::resolve_closure(
bx.cx().tcx(),
def_id,
substs,
substs.as_closure(),
ty::ClosureKind::FnOnce,
);
OperandValue::Immediate(bx.cx().get_fn_addr(instance))
Expand Down
18 changes: 9 additions & 9 deletions src/librustc_middle/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,14 @@ impl<'tcx> Instance<'tcx> {
pub fn resolve_closure(
tcx: TyCtxt<'tcx>,
def_id: DefId,
substs: ty::SubstsRef<'tcx>,
closure_substs: ty::ClosureSubsts<'tcx>,
requested_kind: ty::ClosureKind,
) -> Instance<'tcx> {
let actual_kind = substs.as_closure().kind();
let actual_kind = closure_substs.kind();

match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
_ => Instance::new(def_id, substs),
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, closure_substs),
_ => Instance::new(def_id, tcx.intern_substs(closure_substs.base_substs())),
}
}

Expand All @@ -399,12 +399,12 @@ impl<'tcx> Instance<'tcx> {
Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap().unwrap()
}

pub fn fn_once_adapter_instance(
fn fn_once_adapter_instance(
tcx: TyCtxt<'tcx>,
closure_did: DefId,
substs: ty::SubstsRef<'tcx>,
closure_substs: ty::ClosureSubsts<'tcx>,
) -> Instance<'tcx> {
debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, substs);
debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, closure_substs);
let fn_once = tcx.require_lang_item(FnOnceTraitLangItem, None);
let call_once = tcx
.associated_items(fn_once)
Expand All @@ -414,9 +414,9 @@ impl<'tcx> Instance<'tcx> {
.def_id;
let def = ty::InstanceDef::ClosureOnceShim { call_once };

let self_ty = tcx.mk_closure(closure_did, substs);
let self_ty = tcx.mk_closure(closure_did, closure_substs.substs);

let sig = substs.as_closure().sig();
let sig = closure_substs.sig();
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
assert_eq!(sig.inputs().len(), 1);
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let instance = ty::Instance::resolve_closure(
*self.tcx,
def_id,
substs,
substs.as_closure(),
ty::ClosureKind::FnOnce,
);
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/monomorphize/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
let instance = Instance::resolve_closure(
self.tcx,
def_id,
substs,
substs.as_closure(),
ty::ClosureKind::FnOnce,
);
if should_monomorphize_locally(self.tcx, &instance) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn resolve_associated_item<'tcx>(
Some(Instance::resolve_closure(
tcx,
closure_data.closure_def_id,
closure_data.substs,
closure_data.substs.as_closure(),
trait_closure_kind,
))
}
Expand Down
24 changes: 0 additions & 24 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
if let hir::ExprKind::Closure(..) = expr.kind {
let def_id = self.tcx.hir().local_def_id(expr.hir_id);
self.tcx.ensure().generics_of(def_id);
self.tcx.ensure().type_of(def_id);
}
intravisit::walk_expr(self, expr);
}
Expand Down Expand Up @@ -1362,29 +1361,6 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
}
}));

// provide junk type parameter defs - the only place that
// cares about anything but the length is instantiation,
// and we don't do that for closures.
if let Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(.., gen), .. }) = node {
let dummy_args = if gen.is_some() {
&["<resume_ty>", "<yield_ty>", "<return_ty>", "<witness>", "<upvars>"][..]
} else {
&["<closure_kind>", "<closure_signature>", "<upvars>"][..]
};

params.extend(dummy_args.iter().enumerate().map(|(i, &arg)| ty::GenericParamDef {
index: type_start + i as u32,
name: Symbol::intern(arg),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type {
has_default: false,
object_lifetime_default: rl::Set1::Empty,
synthetic: None,
},
}));
}

let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect();

ty::Generics {
Expand Down
9 changes: 2 additions & 7 deletions src/librustc_typeck/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {

Node::Field(field) => icx.to_ty(&field.ty),

Node::Expr(&Expr { kind: ExprKind::Closure(.., gen), .. }) => {
let substs = InternalSubsts::identity_for_item(tcx, def_id);
if let Some(movability) = gen {
tcx.mk_generator(def_id, substs, movability)
} else {
tcx.mk_closure(def_id, substs)
}
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => {
tcx.typeck_tables_of(def_id.expect_local()).node_type(hir_id)
}

Node::AnonConst(_) => {
Expand Down

0 comments on commit 6e4b8ef

Please sign in to comment.