Skip to content

Commit 632b96f

Browse files
committed
Remove ty::ClosureKind::from_def_id
…in favour of `TyCtxt::fn_trait_kind_from_def_id`
1 parent ec41bc6 commit 632b96f

File tree

4 files changed

+12
-20
lines changed

4 files changed

+12
-20
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20972097
&& let maybe_trait_item_def_id = assoc_item.trait_item_def_id.unwrap_or(def_id)
20982098
&& let maybe_trait_def_id = self.tcx.parent(maybe_trait_item_def_id)
20992099
// Just an easy way to check "trait_def_id == Fn/FnMut/FnOnce"
2100-
&& let Some(call_kind) = ty::ClosureKind::from_def_id(self.tcx, maybe_trait_def_id)
2100+
&& let Some(call_kind) = self.tcx.fn_trait_kind_from_def_id(maybe_trait_def_id)
21012101
&& let Some(callee_ty) = callee_ty
21022102
{
21032103
let callee_ty = callee_ty.peel_refs();
@@ -2123,7 +2123,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21232123
{
21242124
if let ty::PredicateKind::Trait(pred) = predicate.kind().skip_binder()
21252125
&& pred.self_ty().peel_refs() == callee_ty
2126-
&& ty::ClosureKind::from_def_id(self.tcx, pred.def_id()).is_some()
2126+
&& self.tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
21272127
{
21282128
err.span_note(span, "callable defined here");
21292129
return;

compiler/rustc_middle/src/ty/closure.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,9 @@ impl<'tcx> ClosureKind {
117117
}
118118
}
119119

120-
pub fn from_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ClosureKind> {
121-
if Some(def_id) == tcx.lang_items().fn_once_trait() {
122-
Some(ClosureKind::FnOnce)
123-
} else if Some(def_id) == tcx.lang_items().fn_mut_trait() {
124-
Some(ClosureKind::FnMut)
125-
} else if Some(def_id) == tcx.lang_items().fn_trait() {
126-
Some(ClosureKind::Fn)
127-
} else {
128-
None
129-
}
130-
}
131-
120+
/// Converts `self` to a [`DefId`] of the corresponding trait.
121+
///
122+
/// Note: the inverse of this function is [`TyCtxt::fn_trait_kind_from_def_id`]
132123
pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
133124
match self {
134125
ClosureKind::Fn => tcx.lang_items().fn_once_trait().unwrap(),

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
358358
fulfill_cx.register_predicate_obligation(self, obligation);
359359
if fulfill_cx.select_all_or_error(self).is_empty() {
360360
return Ok((
361-
ty::ClosureKind::from_def_id(self.tcx, trait_def_id)
361+
self.tcx
362+
.fn_trait_kind_from_def_id(trait_def_id)
362363
.expect("expected to map DefId to ClosureKind"),
363364
ty.rebind(self.resolve_vars_if_possible(var)),
364365
));
@@ -687,7 +688,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
687688
}
688689
ObligationCauseCode::BindingObligation(def_id, _)
689690
| ObligationCauseCode::ItemObligation(def_id)
690-
if ty::ClosureKind::from_def_id(tcx, *def_id).is_some() =>
691+
if tcx.fn_trait_kind_from_def_id(*def_id).is_some() =>
691692
{
692693
err.code(rustc_errors::error_code!(E0059));
693694
err.set_primary_message(format!(
@@ -848,7 +849,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
848849
}
849850

850851
let is_fn_trait =
851-
ty::ClosureKind::from_def_id(tcx, trait_ref.def_id()).is_some();
852+
tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some();
852853
let is_target_feature_fn = if let ty::FnDef(def_id, _) =
853854
*trait_ref.skip_binder().self_ty().kind()
854855
{
@@ -878,7 +879,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
878879
// Note if the `FnMut` or `FnOnce` is less general than the trait we're trying
879880
// to implement.
880881
let selected_kind =
881-
ty::ClosureKind::from_def_id(self.tcx, trait_ref.def_id())
882+
self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id())
882883
.expect("expected to map DefId to ClosureKind");
883884
if !implemented_kind.extends(selected_kind) {
884885
err.note(

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17571757
&& let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
17581758
&& let Some(pred) = predicates.predicates.get(*idx)
17591759
&& let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder()
1760-
&& ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id()).is_some()
1760+
&& self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()).is_some()
17611761
{
17621762
let expected_self =
17631763
self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty()));
@@ -1771,7 +1771,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17711771
.enumerate()
17721772
.find(|(other_idx, (pred, _))| match pred.kind().skip_binder() {
17731773
ty::PredicateKind::Trait(trait_pred)
1774-
if ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id())
1774+
if self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id())
17751775
.is_some()
17761776
&& other_idx != idx
17771777
// Make sure that the self type matches

0 commit comments

Comments
 (0)