Skip to content

Commit d0c7ed3

Browse files
committed
Remove ty::ClosureKind::from_def_id
…in favour of `TyCtxt::fn_trait_kind_from_def_id`
1 parent 881862e commit d0c7ed3

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
@@ -2089,7 +2089,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20892089
&& let maybe_trait_item_def_id = assoc_item.trait_item_def_id.unwrap_or(def_id)
20902090
&& let maybe_trait_def_id = self.tcx.parent(maybe_trait_item_def_id)
20912091
// Just an easy way to check "trait_def_id == Fn/FnMut/FnOnce"
2092-
&& let Some(call_kind) = ty::ClosureKind::from_def_id(self.tcx, maybe_trait_def_id)
2092+
&& let Some(call_kind) = self.tcx.fn_trait_kind_from_def_id(maybe_trait_def_id)
20932093
&& let Some(callee_ty) = callee_ty
20942094
{
20952095
let callee_ty = callee_ty.peel_refs();
@@ -2115,7 +2115,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21152115
{
21162116
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = predicate.kind().skip_binder()
21172117
&& pred.self_ty().peel_refs() == callee_ty
2118-
&& ty::ClosureKind::from_def_id(self.tcx, pred.def_id()).is_some()
2118+
&& self.tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
21192119
{
21202120
err.span_note(span, "callable defined here");
21212121
return;

compiler/rustc_middle/src/ty/closure.rs

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

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

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
357357
ocx.register_obligation(obligation);
358358
if ocx.select_all_or_error().is_empty() {
359359
return Ok((
360-
ty::ClosureKind::from_def_id(self.tcx, trait_def_id)
360+
self.tcx
361+
.fn_trait_kind_from_def_id(trait_def_id)
361362
.expect("expected to map DefId to ClosureKind"),
362363
ty.rebind(self.resolve_vars_if_possible(var)),
363364
));
@@ -686,7 +687,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
686687
}
687688
ObligationCauseCode::BindingObligation(def_id, _)
688689
| ObligationCauseCode::ItemObligation(def_id)
689-
if ty::ClosureKind::from_def_id(tcx, *def_id).is_some() =>
690+
if tcx.fn_trait_kind_from_def_id(*def_id).is_some() =>
690691
{
691692
err.code(rustc_errors::error_code!(E0059));
692693
err.set_primary_message(format!(
@@ -847,7 +848,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
847848
}
848849

849850
let is_fn_trait =
850-
ty::ClosureKind::from_def_id(tcx, trait_ref.def_id()).is_some();
851+
tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some();
851852
let is_target_feature_fn = if let ty::FnDef(def_id, _) =
852853
*trait_ref.skip_binder().self_ty().kind()
853854
{
@@ -877,7 +878,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
877878
// Note if the `FnMut` or `FnOnce` is less general than the trait we're trying
878879
// to implement.
879880
let selected_kind =
880-
ty::ClosureKind::from_def_id(self.tcx, trait_ref.def_id())
881+
self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id())
881882
.expect("expected to map DefId to ClosureKind");
882883
if !implemented_kind.extends(selected_kind) {
883884
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
@@ -1752,7 +1752,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17521752
&& let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
17531753
&& let Some(pred) = predicates.predicates.get(*idx)
17541754
&& let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder()
1755-
&& ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id()).is_some()
1755+
&& self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()).is_some()
17561756
{
17571757
let expected_self =
17581758
self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty()));
@@ -1766,7 +1766,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17661766
.enumerate()
17671767
.find(|(other_idx, (pred, _))| match pred.kind().skip_binder() {
17681768
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred))
1769-
if ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id())
1769+
if self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id())
17701770
.is_some()
17711771
&& other_idx != idx
17721772
// Make sure that the self type matches

0 commit comments

Comments
 (0)