Skip to content

Commit d6f73c1

Browse files
committed
Add TyCtxt::is_fn_trait
1 parent 632b96f commit d6f73c1

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
263263

264264
let trait_def_id = projection.trait_def_id(tcx);
265265

266-
let is_fn = tcx.fn_trait_kind_from_def_id(trait_def_id).is_some();
266+
let is_fn = tcx.is_fn_trait(trait_def_id);
267267
let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span);
268268
let is_gen = gen_trait == trait_def_id;
269269
if !is_fn && !is_gen {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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-
&& self.tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
2126+
&& self.tcx.is_fn_trait(pred.def_id())
21272127
{
21282128
err.span_note(span, "callable defined here");
21292129
return;

compiler/rustc_middle/src/middle/lang_items.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ impl<'tcx> TyCtxt<'tcx> {
2727
})
2828
}
2929

30+
/// Given a [`DefId`] of a [`Fn`], [`FnMut`] or [`FnOnce`] traits,
31+
/// returns a corresponding [`ty::ClosureKind`].
32+
/// For any other [`DefId`] return `None`.
3033
pub fn fn_trait_kind_from_def_id(self, id: DefId) -> Option<ty::ClosureKind> {
3134
let items = self.lang_items();
3235
match Some(id) {
@@ -36,6 +39,11 @@ impl<'tcx> TyCtxt<'tcx> {
3639
_ => None,
3740
}
3841
}
42+
43+
/// Returns `true` if `id` is a `DefId` of [`Fn`], [`FnMut`] or [`FnOnce`] traits.
44+
pub fn is_fn_trait(self, id: DefId) -> bool {
45+
self.fn_trait_kind_from_def_id(id).is_some()
46+
}
3947
}
4048

4149
/// Returns `true` if the specified `lang_item` must be present for this

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
688688
}
689689
ObligationCauseCode::BindingObligation(def_id, _)
690690
| ObligationCauseCode::ItemObligation(def_id)
691-
if tcx.fn_trait_kind_from_def_id(*def_id).is_some() =>
691+
if tcx.is_fn_trait(*def_id) =>
692692
{
693693
err.code(rustc_errors::error_code!(E0059));
694694
err.set_primary_message(format!(
@@ -848,8 +848,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
848848
);
849849
}
850850

851-
let is_fn_trait =
852-
tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some();
851+
let is_fn_trait = tcx.is_fn_trait(trait_ref.def_id());
853852
let is_target_feature_fn = if let ty::FnDef(def_id, _) =
854853
*trait_ref.skip_binder().self_ty().kind()
855854
{
@@ -2153,7 +2152,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21532152
if generics.params.iter().any(|p| p.name != kw::SelfUpper)
21542153
&& !snippet.ends_with('>')
21552154
&& !generics.has_impl_trait()
2156-
&& !self.tcx.fn_trait_kind_from_def_id(def_id).is_some()
2155+
&& !self.tcx.is_fn_trait(def_id)
21572156
{
21582157
// FIXME: To avoid spurious suggestions in functions where type arguments
21592158
// where already supplied, we check the snippet to make sure it doesn't

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,9 +1684,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16841684
) -> Ty<'tcx> {
16851685
let inputs = trait_ref.skip_binder().substs.type_at(1);
16861686
let sig = match inputs.kind() {
1687-
ty::Tuple(inputs)
1688-
if infcx.tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some() =>
1689-
{
1687+
ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id()) => {
16901688
infcx.tcx.mk_fn_sig(
16911689
inputs.iter(),
16921690
infcx.next_ty_var(TypeVariableOrigin {
@@ -1757,7 +1755,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17571755
&& let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
17581756
&& let Some(pred) = predicates.predicates.get(*idx)
17591757
&& let ty::PredicateKind::Trait(trait_pred) = pred.kind().skip_binder()
1760-
&& self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()).is_some()
1758+
&& self.tcx.is_fn_trait(trait_pred.def_id())
17611759
{
17621760
let expected_self =
17631761
self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty()));

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
473473
candidates: &mut SelectionCandidateSet<'tcx>,
474474
) {
475475
// We provide impl of all fn traits for fn pointers.
476-
if self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()).is_none() {
476+
if !self.tcx().is_fn_trait(obligation.predicate.def_id()) {
477477
return;
478478
}
479479

0 commit comments

Comments
 (0)