From 19fc3a1a6e9048ecf62c490023b35d41642dd07d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 28 Apr 2025 08:55:11 +0000 Subject: [PATCH 1/3] Replace if/elseif chain with match --- .../src/traits/select/candidate_assembly.rs | 143 ++++++++++-------- 1 file changed, 81 insertions(+), 62 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 5d0b9dd41b208..99d0a383109f2 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -66,71 +66,90 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let def_id = obligation.predicate.def_id(); let tcx = self.tcx(); - if tcx.is_lang_item(def_id, LangItem::Copy) { - debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty()); - - // User-defined copy impls are permitted, but only for - // structs and enums. - self.assemble_candidates_from_impls(obligation, &mut candidates); - - // For other types, we'll use the builtin rules. - let copy_conditions = self.copy_clone_conditions(obligation); - self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::DiscriminantKind) { - // `DiscriminantKind` is automatically implemented for every type. - candidates.vec.push(BuiltinCandidate { has_nested: false }); - } else if tcx.is_lang_item(def_id, LangItem::PointeeTrait) { - // `Pointee` is automatically implemented for every type. - candidates.vec.push(BuiltinCandidate { has_nested: false }); - } else if tcx.is_lang_item(def_id, LangItem::Sized) { - self.assemble_builtin_sized_candidate(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::Unsize) { - self.assemble_candidates_for_unsizing(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::Destruct) { - self.assemble_const_destruct_candidates(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::TransmuteTrait) { - // User-defined transmutability impls are permitted. - self.assemble_candidates_from_impls(obligation, &mut candidates); - self.assemble_candidates_for_transmutability(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::Tuple) { - self.assemble_candidate_for_tuple(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::FnPtrTrait) { - self.assemble_candidates_for_fn_ptr_trait(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::BikeshedGuaranteedNoDrop) { - self.assemble_candidates_for_bikeshed_guaranteed_no_drop_trait( - obligation, - &mut candidates, - ); - } else { - if tcx.is_lang_item(def_id, LangItem::Clone) { - // Same builtin conditions as `Copy`, i.e., every type which has builtin support - // for `Copy` also has builtin support for `Clone`, and tuples/arrays of `Clone` - // types have builtin support for `Clone`. - let clone_conditions = self.copy_clone_conditions(obligation); - self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates); + let lang_item = tcx.as_lang_item(def_id); + match lang_item { + Some(LangItem::Copy | LangItem::Clone) => { + debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty()); + + // User-defined copy impls are permitted, but only for + // structs and enums. + self.assemble_candidates_from_impls(obligation, &mut candidates); + + // For other types, we'll use the builtin rules. + let copy_conditions = self.copy_clone_conditions(obligation); + self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates); } - - if tcx.is_lang_item(def_id, LangItem::Coroutine) { - self.assemble_coroutine_candidates(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::Future) { - self.assemble_future_candidates(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::Iterator) { - self.assemble_iterator_candidates(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::FusedIterator) { - self.assemble_fused_iterator_candidates(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::AsyncIterator) { - self.assemble_async_iterator_candidates(obligation, &mut candidates); - } else if tcx.is_lang_item(def_id, LangItem::AsyncFnKindHelper) { - self.assemble_async_fn_kind_helper_candidates(obligation, &mut candidates); + Some(LangItem::DiscriminantKind) => { + // `DiscriminantKind` is automatically implemented for every type. + candidates.vec.push(BuiltinCandidate { has_nested: false }); } + Some(LangItem::PointeeTrait) => { + // `Pointee` is automatically implemented for every type. + candidates.vec.push(BuiltinCandidate { has_nested: false }); + } + Some(LangItem::Sized) => { + self.assemble_builtin_sized_candidate(obligation, &mut candidates); + } + Some(LangItem::Unsize) => { + self.assemble_candidates_for_unsizing(obligation, &mut candidates); + } + Some(LangItem::Destruct) => { + self.assemble_const_destruct_candidates(obligation, &mut candidates); + } + Some(LangItem::TransmuteTrait) => { + // User-defined transmutability impls are permitted. + self.assemble_candidates_from_impls(obligation, &mut candidates); + self.assemble_candidates_for_transmutability(obligation, &mut candidates); + } + Some(LangItem::Tuple) => { + self.assemble_candidate_for_tuple(obligation, &mut candidates); + } + Some(LangItem::FnPtrTrait) => { + self.assemble_candidates_for_fn_ptr_trait(obligation, &mut candidates); + } + Some(LangItem::BikeshedGuaranteedNoDrop) => { + self.assemble_candidates_for_bikeshed_guaranteed_no_drop_trait( + obligation, + &mut candidates, + ); + } + _ => { + // We re-match here for traits that can have both builtin impls and user written impls. + // After the builtin impls we need to also add user written impls, which we do not want to + // do in general because just checking if there are any is expensive. + match lang_item { + Some(LangItem::Coroutine) => { + self.assemble_coroutine_candidates(obligation, &mut candidates); + } + Some(LangItem::Future) => { + self.assemble_future_candidates(obligation, &mut candidates); + } + Some(LangItem::Iterator) => { + self.assemble_iterator_candidates(obligation, &mut candidates); + } + Some(LangItem::FusedIterator) => { + self.assemble_fused_iterator_candidates(obligation, &mut candidates); + } + Some(LangItem::AsyncIterator) => { + self.assemble_async_iterator_candidates(obligation, &mut candidates); + } + Some(LangItem::AsyncFnKindHelper) => { + self.assemble_async_fn_kind_helper_candidates( + obligation, + &mut candidates, + ); + } + _ => { + // FIXME: Put these into match arms above, since they're built-in. + self.assemble_closure_candidates(obligation, &mut candidates); + self.assemble_async_closure_candidates(obligation, &mut candidates); + self.assemble_fn_pointer_candidates(obligation, &mut candidates); + } + } - // FIXME: Put these into `else if` blocks above, since they're built-in. - self.assemble_closure_candidates(obligation, &mut candidates); - self.assemble_async_closure_candidates(obligation, &mut candidates); - self.assemble_fn_pointer_candidates(obligation, &mut candidates); - - self.assemble_candidates_from_impls(obligation, &mut candidates); - self.assemble_candidates_from_object_ty(obligation, &mut candidates); + self.assemble_candidates_from_impls(obligation, &mut candidates); + self.assemble_candidates_from_object_ty(obligation, &mut candidates); + } } self.assemble_candidates_from_projected_tys(obligation, &mut candidates); From dd714276e5bc65960816fcdcd2859f4c34ccb680 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 28 Apr 2025 09:02:06 +0000 Subject: [PATCH 2/3] Always check the lang item first --- .../src/traits/select/candidate_assembly.rs | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 99d0a383109f2..d643ba7f17d14 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -139,12 +139,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { &mut candidates, ); } - _ => { - // FIXME: Put these into match arms above, since they're built-in. - self.assemble_closure_candidates(obligation, &mut candidates); + Some(LangItem::AsyncFn | LangItem::AsyncFnMut | LangItem::AsyncFnOnce) => { self.assemble_async_closure_candidates(obligation, &mut candidates); + } + Some(LangItem::Fn | LangItem::FnMut | LangItem::FnOnce) => { + self.assemble_closure_candidates(obligation, &mut candidates); self.assemble_fn_pointer_candidates(obligation, &mut candidates); } + _ => {} } self.assemble_candidates_from_impls(obligation, &mut candidates); @@ -380,9 +382,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &PolyTraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) { - let Some(kind) = self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()) else { - return; - }; + let kind = self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()).unwrap(); // Okay to skip binder because the args on closure types never // touch bound regions, they just capture the in-scope @@ -444,11 +444,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &PolyTraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) { - let Some(goal_kind) = - self.tcx().async_fn_trait_kind_from_def_id(obligation.predicate.def_id()) - else { - return; - }; + let goal_kind = + self.tcx().async_fn_trait_kind_from_def_id(obligation.predicate.def_id()).unwrap(); match *obligation.self_ty().skip_binder().kind() { ty::CoroutineClosure(_, args) => { @@ -521,11 +518,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &PolyTraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) { - // We provide impl of all fn traits for fn pointers. - if !self.tcx().is_fn_trait(obligation.predicate.def_id()) { - return; - } - // Keep this function in sync with extract_tupled_inputs_and_output_from_callable // until the old solver (and thus this function) is removed. From 03c05c9a9568894f8fd7bd8abbfe602d5e593da8 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 28 Apr 2025 13:25:21 +0000 Subject: [PATCH 3/3] Also match on the lang item in confirmation --- .../src/traits/select/confirmation.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index d71d1e9ae0faf..4d184b0181a3c 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -252,16 +252,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let tcx = self.tcx(); let obligations = if has_nested { let trait_def = obligation.predicate.def_id(); - let conditions = if tcx.is_lang_item(trait_def, LangItem::Sized) { - self.sized_conditions(obligation) - } else if tcx.is_lang_item(trait_def, LangItem::Copy) { - self.copy_clone_conditions(obligation) - } else if tcx.is_lang_item(trait_def, LangItem::Clone) { - self.copy_clone_conditions(obligation) - } else if tcx.is_lang_item(trait_def, LangItem::FusedIterator) { - self.fused_iterator_conditions(obligation) - } else { - bug!("unexpected builtin trait {:?}", trait_def) + let conditions = match tcx.as_lang_item(trait_def) { + Some(LangItem::Sized) => self.sized_conditions(obligation), + Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(obligation), + Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation), + other => bug!("unexpected builtin trait {trait_def:?} ({other:?})"), }; let BuiltinImplConditions::Where(types) = conditions else { bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);