Skip to content

Commit 1f1bbfc

Browse files
authored
Rollup merge of rust-lang#104732 - WaffleLapkin:from_def_idn't, r=compiler-errors
Refactor `ty::ClosureKind` related stuff I've tried to fix all duplication and weirdness, but if I missed something do tell :p r? `@compiler-errors`
2 parents 8aa6ab8 + 48fee7a commit 1f1bbfc

File tree

14 files changed

+44
-60
lines changed

14 files changed

+44
-60
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
178178
});
179179
let kind = object_type
180180
.principal_def_id()
181-
.and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did));
181+
.and_then(|did| self.tcx.fn_trait_kind_from_def_id(did));
182182
(sig, kind)
183183
}
184184
ty::Infer(ty::TyVar(vid)) => self.deduce_signature_from_predicates(
@@ -235,7 +235,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
235235
_ => None,
236236
};
237237
if let Some(closure_kind) =
238-
trait_def_id.and_then(|def_id| self.tcx.fn_trait_kind_from_lang_item(def_id))
238+
trait_def_id.and_then(|def_id| self.tcx.fn_trait_kind_from_def_id(def_id))
239239
{
240240
expected_kind = Some(
241241
expected_kind
@@ -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_lang_item(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

+2-2
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.is_fn_trait(pred.def_id())
21272127
{
21282128
err.span_note(span, "callable defined here");
21292129
return;

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
399399
self_ty.highlight.maybe_highlighting_region(vid, actual_has_vid);
400400

401401
if self_ty.value.is_closure()
402-
&& self
403-
.tcx()
404-
.fn_trait_kind_from_lang_item(expected_trait_ref.value.def_id)
405-
.is_some()
402+
&& self.tcx().is_fn_trait(expected_trait_ref.value.def_id)
406403
{
407404
let closure_sig = self_ty.map(|closure| {
408405
if let ty::Closure(_, substs) = closure.kind() {

compiler/rustc_middle/src/middle/lang_items.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ impl<'tcx> TyCtxt<'tcx> {
2727
})
2828
}
2929

30-
pub fn fn_trait_kind_from_lang_item(self, id: DefId) -> Option<ty::ClosureKind> {
30+
/// Given a [`DefId`] of a [`Fn`], [`FnMut`] or [`FnOnce`] traits,
31+
/// returns a corresponding [`ty::ClosureKind`].
32+
/// For any other [`DefId`] return `None`.
33+
pub fn fn_trait_kind_from_def_id(self, id: DefId) -> Option<ty::ClosureKind> {
3134
let items = self.lang_items();
3235
match Some(id) {
3336
x if x == items.fn_trait() => Some(ty::ClosureKind::Fn),
@@ -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_middle/src/ty/closure.rs

+12-29
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,18 @@ impl<'tcx> ClosureKind {
9696
/// Returns `true` if a type that impls this closure kind
9797
/// must also implement `other`.
9898
pub fn extends(self, other: ty::ClosureKind) -> bool {
99-
matches!(
100-
(self, other),
101-
(ClosureKind::Fn, ClosureKind::Fn)
102-
| (ClosureKind::Fn, ClosureKind::FnMut)
103-
| (ClosureKind::Fn, ClosureKind::FnOnce)
104-
| (ClosureKind::FnMut, ClosureKind::FnMut)
105-
| (ClosureKind::FnMut, ClosureKind::FnOnce)
106-
| (ClosureKind::FnOnce, ClosureKind::FnOnce)
107-
)
99+
self <= other
100+
}
101+
102+
/// Converts `self` to a [`DefId`] of the corresponding trait.
103+
///
104+
/// Note: the inverse of this function is [`TyCtxt::fn_trait_kind_from_def_id`].
105+
pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
106+
match self {
107+
ClosureKind::Fn => tcx.lang_items().fn_once_trait().unwrap(),
108+
ClosureKind::FnMut => tcx.lang_items().fn_mut_trait().unwrap(),
109+
ClosureKind::FnOnce => tcx.lang_items().fn_trait().unwrap(),
110+
}
108111
}
109112

110113
/// Returns the representative scalar type for this closure kind.
@@ -116,26 +119,6 @@ impl<'tcx> ClosureKind {
116119
ClosureKind::FnOnce => tcx.types.i32,
117120
}
118121
}
119-
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-
132-
pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
133-
match self {
134-
ClosureKind::Fn => tcx.lang_items().fn_once_trait().unwrap(),
135-
ClosureKind::FnMut => tcx.lang_items().fn_mut_trait().unwrap(),
136-
ClosureKind::FnOnce => tcx.lang_items().fn_trait().unwrap(),
137-
}
138-
}
139122
}
140123

141124
/// A composite describing a `Place` that is captured by a closure.

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ pub trait PrettyPrinter<'tcx>:
10731073
let mut resugared = false;
10741074

10751075
// Special-case `Fn(...) -> ...` and re-sugar it.
1076-
let fn_trait_kind = cx.tcx().fn_trait_kind_from_lang_item(principal.def_id);
1076+
let fn_trait_kind = cx.tcx().fn_trait_kind_from_def_id(principal.def_id);
10771077
if !cx.should_print_verbose() && fn_trait_kind.is_some() {
10781078
if let ty::Tuple(tys) = principal.substs.type_at(0).kind() {
10791079
let mut projections = predicates.projection_bounds();

compiler/rustc_middle/src/ty/sty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2130,8 +2130,7 @@ impl<'tcx> Ty<'tcx> {
21302130
/// parameter. This is kind of a phantom type, except that the
21312131
/// most convenient thing for us to are the integral types. This
21322132
/// function converts such a special type into the closure
2133-
/// kind. To go the other way, use
2134-
/// `tcx.closure_kind_ty(closure_kind)`.
2133+
/// kind. To go the other way, use `closure_kind.to_ty(tcx)`.
21352134
///
21362135
/// Note that during type checking, we use an inference variable
21372136
/// to represent the closure kind, because it has not yet been

compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
3737
}
3838
ty::InstanceDef::FnPtrShim(def_id, ty) => {
3939
let trait_ = tcx.trait_of_item(def_id).unwrap();
40-
let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) {
40+
let adjustment = match tcx.fn_trait_kind_from_def_id(trait_) {
4141
Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
4242
Some(ty::ClosureKind::FnMut | ty::ClosureKind::Fn) => Adjustment::Deref,
4343
None => bug!("fn pointer {:?} is not an fn", ty),

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

+6-6
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.is_fn_trait(*def_id) =>
691692
{
692693
err.code(rustc_errors::error_code!(E0059));
693694
err.set_primary_message(format!(
@@ -847,8 +848,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
847848
);
848849
}
849850

850-
let is_fn_trait =
851-
ty::ClosureKind::from_def_id(tcx, trait_ref.def_id()).is_some();
851+
let is_fn_trait = tcx.is_fn_trait(trait_ref.def_id());
852852
let is_target_feature_fn = if let ty::FnDef(def_id, _) =
853853
*trait_ref.skip_binder().self_ty().kind()
854854
{
@@ -878,7 +878,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
878878
// Note if the `FnMut` or `FnOnce` is less general than the trait we're trying
879879
// to implement.
880880
let selected_kind =
881-
ty::ClosureKind::from_def_id(self.tcx, trait_ref.def_id())
881+
self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id())
882882
.expect("expected to map DefId to ClosureKind");
883883
if !implemented_kind.extends(selected_kind) {
884884
err.note(
@@ -2154,7 +2154,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21542154
if generics.params.iter().any(|p| p.name != kw::SelfUpper)
21552155
&& !snippet.ends_with('>')
21562156
&& !generics.has_impl_trait()
2157-
&& !self.tcx.fn_trait_kind_from_lang_item(def_id).is_some()
2157+
&& !self.tcx.is_fn_trait(def_id)
21582158
{
21592159
// FIXME: To avoid spurious suggestions in functions where type arguments
21602160
// where already supplied, we check the snippet to make sure it doesn't

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

+3-6
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_lang_item(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-
&& ty::ClosureKind::from_def_id(self.tcx, 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()));
@@ -1771,8 +1769,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
17711769
.enumerate()
17721770
.find(|(other_idx, (pred, _))| match pred.kind().skip_binder() {
17731771
ty::PredicateKind::Trait(trait_pred)
1774-
if ty::ClosureKind::from_def_id(self.tcx, trait_pred.def_id())
1775-
.is_some()
1772+
if self.tcx.is_fn_trait(trait_pred.def_id())
17761773
&& other_idx != idx
17771774
// Make sure that the self type matches
17781775
// (i.e. constraining this closure)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
435435
obligation: &TraitObligation<'tcx>,
436436
candidates: &mut SelectionCandidateSet<'tcx>,
437437
) {
438-
let Some(kind) = self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) else {
438+
let Some(kind) = self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()) else {
439439
return;
440440
};
441441

@@ -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_lang_item(obligation.predicate.def_id()).is_none() {
476+
if !self.tcx().is_fn_trait(obligation.predicate.def_id()) {
477477
return;
478478
}
479479

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
700700
) -> Result<ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
701701
let kind = self
702702
.tcx()
703-
.fn_trait_kind_from_lang_item(obligation.predicate.def_id())
703+
.fn_trait_kind_from_def_id(obligation.predicate.def_id())
704704
.unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation));
705705

706706
// Okay to skip binder because the substs on closure types never

compiler/rustc_ty_utils/src/instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn resolve_associated_item<'tcx>(
203203
substs: generator_data.substs,
204204
}),
205205
traits::ImplSource::Closure(closure_data) => {
206-
let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap();
206+
let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap();
207207
Instance::resolve_closure(
208208
tcx,
209209
closure_data.closure_def_id,

src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn external_generic_args<'tcx>(
106106
) -> GenericArgs {
107107
let args = substs_to_args(cx, substs, has_self);
108108

109-
if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() {
109+
if cx.tcx.fn_trait_kind_from_def_id(did).is_some() {
110110
let inputs =
111111
// The trait's first substitution is the one after self, if there is one.
112112
match substs.iter().nth(if has_self { 1 } else { 0 }).unwrap().expect_ty().kind() {

0 commit comments

Comments
 (0)