Skip to content

Commit 742f193

Browse files
Move special methods from ClosureKind back into rustc
1 parent 9f0849f commit 742f193

File tree

8 files changed

+27
-57
lines changed

8 files changed

+27
-57
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
141141
debug!(?sig, ?opt_kind);
142142

143143
let closure_kind_ty = match opt_kind {
144-
Some(kind) => kind.to_ty(self.tcx),
144+
Some(kind) => Ty::from_closure_kind(self.tcx, kind),
145145

146146
// Create a type variable (for now) to represent the closure kind.
147147
// It will be unified during the upvar inference phase (`upvar.rs`)

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20182018
let new_def_id = self.probe(|_| {
20192019
let trait_ref = ty::TraitRef::new(
20202020
self.tcx,
2021-
call_kind.to_def_id(self.tcx),
2021+
self.tcx.fn_trait_kind_to_def_id(call_kind)?,
20222022
[
20232023
callee_ty,
20242024
self.next_ty_var(TypeVariableOrigin {

compiler/rustc_hir_typeck/src/upvar.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
261261
// Unify the (as yet unbound) type variable in the closure
262262
// args with the kind we inferred.
263263
let closure_kind_ty = closure_args.as_closure().kind_ty();
264-
self.demand_eqtype(span, closure_kind.to_ty(self.tcx), closure_kind_ty);
264+
self.demand_eqtype(
265+
span,
266+
Ty::from_closure_kind(self.tcx, closure_kind),
267+
closure_kind_ty,
268+
);
265269

266270
// If we have an origin, store it.
267271
if let Some(mut origin) = origin {

compiler/rustc_middle/src/middle/lang_items.rs

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ impl<'tcx> TyCtxt<'tcx> {
3636
}
3737
}
3838

39+
/// Given a [`ty::ClosureKind`], get the [`DefId`] of its corresponding `Fn`-family
40+
/// trait, if it is defined.
41+
pub fn fn_trait_kind_to_def_id(self, kind: ty::ClosureKind) -> Option<DefId> {
42+
let items = self.lang_items();
43+
match kind {
44+
ty::ClosureKind::Fn => items.fn_trait(),
45+
ty::ClosureKind::FnMut => items.fn_mut_trait(),
46+
ty::ClosureKind::FnOnce => items.fn_once_trait(),
47+
}
48+
}
49+
3950
/// Returns `true` if `id` is a `DefId` of [`Fn`], [`FnMut`] or [`FnOnce`] traits.
4051
pub fn is_fn_trait(self, id: DefId) -> bool {
4152
self.fn_trait_kind_from_def_id(id).is_some()

compiler/rustc_middle/src/ty/context.rs

-24
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
151151
) -> Self::Const {
152152
Const::new_bound(self, debruijn, var, ty)
153153
}
154-
155-
fn fn_def_id(self) -> Self::DefId {
156-
self.require_lang_item(hir::LangItem::Fn, None)
157-
}
158-
159-
fn fn_mut_def_id(self) -> Self::DefId {
160-
self.require_lang_item(hir::LangItem::FnMut, None)
161-
}
162-
163-
fn fn_once_def_id(self) -> Self::DefId {
164-
self.require_lang_item(hir::LangItem::FnOnce, None)
165-
}
166-
167-
fn i8_type(self) -> Self::Ty {
168-
self.types.i8
169-
}
170-
171-
fn i16_type(self) -> Self::Ty {
172-
self.types.i16
173-
}
174-
175-
fn i32_type(self) -> Self::Ty {
176-
self.types.i32
177-
}
178154
}
179155

180156
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;

compiler/rustc_middle/src/ty/sty.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,15 @@ impl<'tcx> Ty<'tcx> {
28112811
}
28122812
}
28132813

2814+
/// Inverse of [`Ty::to_opt_closure_kind`].
2815+
pub fn from_closure_kind(tcx: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Ty<'tcx> {
2816+
match kind {
2817+
ty::ClosureKind::Fn => tcx.types.i8,
2818+
ty::ClosureKind::FnMut => tcx.types.i16,
2819+
ty::ClosureKind::FnOnce => tcx.types.i32,
2820+
}
2821+
}
2822+
28142823
/// Fast path helper for testing if a type is `Sized`.
28152824
///
28162825
/// Returning true means the type is known to be sized. Returning

compiler/rustc_type_ir/src/interner.rs

-9
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ pub trait Interner: Sized {
8686
fn mk_bound_ty(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Ty;
8787
fn mk_bound_region(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Region;
8888
fn mk_bound_const(self, debruijn: DebruijnIndex, var: BoundVar, ty: Self::Ty) -> Self::Const;
89-
90-
// FIXME: these could be consolidated into some "WellKnownTraits" thing like chalk does.
91-
fn fn_def_id(self) -> Self::DefId;
92-
fn fn_mut_def_id(self) -> Self::DefId;
93-
fn fn_once_def_id(self) -> Self::DefId;
94-
95-
fn i8_type(self) -> Self::Ty;
96-
fn i16_type(self) -> Self::Ty;
97-
fn i32_type(self) -> Self::Ty;
9889
}
9990

10091
/// Common capabilities of placeholder kinds

compiler/rustc_type_ir/src/lib.rs

-21
Original file line numberDiff line numberDiff line change
@@ -394,27 +394,6 @@ impl ClosureKind {
394394
pub fn extends(self, other: ClosureKind) -> bool {
395395
self <= other
396396
}
397-
398-
/// Converts `self` to a `DefId` of the corresponding trait.
399-
///
400-
/// Note: the inverse of this function is `TyCtxt::fn_trait_kind_from_def_id`.
401-
pub fn to_def_id<I: Interner>(self, interner: I) -> I::DefId {
402-
match self {
403-
ClosureKind::Fn => interner.fn_def_id(),
404-
ClosureKind::FnMut => interner.fn_mut_def_id(),
405-
ClosureKind::FnOnce => interner.fn_once_def_id(),
406-
}
407-
}
408-
409-
/// Returns the representative scalar type for this closure kind.
410-
/// See `Ty::to_opt_closure_kind` for more details.
411-
pub fn to_ty<I: Interner>(self, interner: I) -> I::Ty {
412-
match self {
413-
ClosureKind::Fn => interner.i8_type(),
414-
ClosureKind::FnMut => interner.i16_type(),
415-
ClosureKind::FnOnce => interner.i32_type(),
416-
}
417-
}
418397
}
419398

420399
impl fmt::Display for ClosureKind {

0 commit comments

Comments
 (0)