Skip to content

Commit 4b6e1d1

Browse files
committed
Add TyCtxt::is_fn_trait
1 parent d0c7ed3 commit 4b6e1d1

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
@@ -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-
&& self.tcx.fn_trait_kind_from_def_id(pred.def_id()).is_some()
2118+
&& self.tcx.is_fn_trait(pred.def_id())
21192119
{
21202120
err.span_note(span, "callable defined here");
21212121
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
@@ -687,7 +687,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
687687
}
688688
ObligationCauseCode::BindingObligation(def_id, _)
689689
| ObligationCauseCode::ItemObligation(def_id)
690-
if tcx.fn_trait_kind_from_def_id(*def_id).is_some() =>
690+
if tcx.is_fn_trait(*def_id) =>
691691
{
692692
err.code(rustc_errors::error_code!(E0059));
693693
err.set_primary_message(format!(
@@ -847,8 +847,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
847847
);
848848
}
849849

850-
let is_fn_trait =
851-
tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some();
850+
let is_fn_trait = tcx.is_fn_trait(trait_ref.def_id());
852851
let is_target_feature_fn = if let ty::FnDef(def_id, _) =
853852
*trait_ref.skip_binder().self_ty().kind()
854853
{
@@ -2156,7 +2155,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21562155
if generics.params.iter().any(|p| p.name != kw::SelfUpper)
21572156
&& !snippet.ends_with('>')
21582157
&& !generics.has_impl_trait()
2159-
&& !self.tcx.fn_trait_kind_from_def_id(def_id).is_some()
2158+
&& !self.tcx.is_fn_trait(def_id)
21602159
{
21612160
// FIXME: To avoid spurious suggestions in functions where type arguments
21622161
// 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
@@ -1679,9 +1679,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16791679
) -> Ty<'tcx> {
16801680
let inputs = trait_ref.skip_binder().substs.type_at(1);
16811681
let sig = match inputs.kind() {
1682-
ty::Tuple(inputs)
1683-
if infcx.tcx.fn_trait_kind_from_def_id(trait_ref.def_id()).is_some() =>
1684-
{
1682+
ty::Tuple(inputs) if infcx.tcx.is_fn_trait(trait_ref.def_id()) => {
16851683
infcx.tcx.mk_fn_sig(
16861684
inputs.iter(),
16871685
infcx.next_ty_var(TypeVariableOrigin {
@@ -1752,7 +1750,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17521750
&& let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
17531751
&& let Some(pred) = predicates.predicates.get(*idx)
17541752
&& let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = pred.kind().skip_binder()
1755-
&& self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()).is_some()
1753+
&& self.tcx.is_fn_trait(trait_pred.def_id())
17561754
{
17571755
let expected_self =
17581756
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
@@ -489,7 +489,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
489489
candidates: &mut SelectionCandidateSet<'tcx>,
490490
) {
491491
// We provide impl of all fn traits for fn pointers.
492-
if self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()).is_none() {
492+
if !self.tcx().is_fn_trait(obligation.predicate.def_id()) {
493493
return;
494494
}
495495

0 commit comments

Comments
 (0)