Rollup merge of #119969 - compiler-errors:simplify-closure-env-ty, r=oli-obk

Simplify `closure_env_ty` and `closure_env_param`

Random cleanup that I found when working on async closures. This makes it easier to separate the latter into a new tykind.
This commit is contained in:
Matthias Krüger 2024-01-16 17:55:23 +01:00 committed by GitHub
commit 4735171667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 54 deletions

View file

@ -668,7 +668,11 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
kind: ty::BrEnv,
};
let env_region = ty::Region::new_bound(tcx, ty::INNERMOST, br);
let closure_ty = tcx.closure_env_ty(def_id, args, env_region).unwrap();
let closure_ty = tcx.closure_env_ty(
Ty::new_closure(tcx, def_id, args),
args.as_closure().kind(),
env_region,
);
// The "inputs" of the closure in the
// signature appear as a tuple. The MIR side

View file

@ -604,19 +604,15 @@ impl<'tcx> TyCtxt<'tcx> {
/// wrapped in a binder.
pub fn closure_env_ty(
self,
closure_def_id: DefId,
closure_args: GenericArgsRef<'tcx>,
closure_ty: Ty<'tcx>,
closure_kind: ty::ClosureKind,
env_region: ty::Region<'tcx>,
) -> Option<Ty<'tcx>> {
let closure_ty = Ty::new_closure(self, closure_def_id, closure_args);
let closure_kind_ty = closure_args.as_closure().kind_ty();
let closure_kind = closure_kind_ty.to_opt_closure_kind()?;
let env_ty = match closure_kind {
) -> Ty<'tcx> {
match closure_kind {
ty::ClosureKind::Fn => Ty::new_imm_ref(self, env_region, closure_ty),
ty::ClosureKind::FnMut => Ty::new_mut_ref(self, env_region, closure_ty),
ty::ClosureKind::FnOnce => closure_ty,
};
Some(env_ty)
}
}
/// Returns `true` if the node pointed to by `def_id` is a `static` item.

View file

@ -117,50 +117,32 @@ impl<'tcx> Cx<'tcx> {
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
}
fn closure_env_param(&self, owner_def: LocalDefId, owner_id: HirId) -> Option<Param<'tcx>> {
match self.tcx.def_kind(owner_def) {
DefKind::Closure if self.tcx.is_coroutine(owner_def.to_def_id()) => {
let coroutine_ty = self.typeck_results.node_type(owner_id);
let coroutine_param = Param {
ty: coroutine_ty,
pat: None,
ty_span: None,
self_kind: None,
hir_id: None,
};
Some(coroutine_param)
}
DefKind::Closure => {
let closure_ty = self.typeck_results.node_type(owner_id);
let ty::Closure(closure_def_id, closure_args) = *closure_ty.kind() else {
bug!("closure expr does not have closure type: {:?}", closure_ty);
};
let bound_vars =
self.tcx.mk_bound_variable_kinds(&[ty::BoundVariableKind::Region(ty::BrEnv)]);
let br = ty::BoundRegion {
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
kind: ty::BrEnv,
};
let env_region = ty::Region::new_bound(self.tcx, ty::INNERMOST, br);
let closure_env_ty =
self.tcx.closure_env_ty(closure_def_id, closure_args, env_region).unwrap();
let liberated_closure_env_ty = self.tcx.instantiate_bound_regions_with_erased(
ty::Binder::bind_with_vars(closure_env_ty, bound_vars),
);
let env_param = Param {
ty: liberated_closure_env_ty,
pat: None,
ty_span: None,
self_kind: None,
hir_id: None,
};
Some(env_param)
}
_ => None,
fn closure_env_param(&self, owner_def: LocalDefId, expr_id: HirId) -> Option<Param<'tcx>> {
if self.tcx.def_kind(owner_def) != DefKind::Closure {
return None;
}
let closure_ty = self.typeck_results.node_type(expr_id);
Some(match *closure_ty.kind() {
ty::Coroutine(..) => {
Param { ty: closure_ty, pat: None, ty_span: None, self_kind: None, hir_id: None }
}
ty::Closure(_, closure_args) => {
let closure_env_ty = self.tcx.closure_env_ty(
closure_ty,
closure_args.as_closure().kind(),
self.tcx.lifetimes.re_erased,
);
Param {
ty: closure_env_ty,
pat: None,
ty_span: None,
self_kind: None,
hir_id: None,
}
}
_ => bug!("unexpected closure type: {closure_ty}"),
})
}
fn explicit_params<'a>(

View file

@ -83,7 +83,11 @@ fn fn_sig_for_fn_abi<'tcx>(
kind: ty::BoundRegionKind::BrEnv,
};
let env_region = ty::Region::new_bound(tcx, ty::INNERMOST, br);
let env_ty = tcx.closure_env_ty(def_id, args, env_region).unwrap();
let env_ty = tcx.closure_env_ty(
Ty::new_closure(tcx, def_id, args),
args.as_closure().kind(),
env_region,
);
let sig = sig.skip_binder();
ty::Binder::bind_with_vars(