diff options
| author | yukang <moorekang@gmail.com> | 2023-01-07 07:16:48 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2023-01-07 09:20:05 +0800 |
| commit | 6082729646e3530ecdd522921837806db83b27e3 (patch) | |
| tree | 8ac74bb13cc5ba934743e0defff49c4c30df8963 | |
| parent | ce4afed2efdaaceb2624021df3a8d8fff892415a (diff) | |
| download | rust-6082729646e3530ecdd522921837806db83b27e3.tar.gz rust-6082729646e3530ecdd522921837806db83b27e3.zip | |
use type_implements_trait to check Param clone
| -rw-r--r-- | compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs | 27 | ||||
| -rw-r--r-- | src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs | 4 |
2 files changed, 9 insertions, 22 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 727bdc391bb..c52365ae3b7 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1121,36 +1121,23 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let Some(generics) = self.tcx.hir().get_generics(owner.def_id) else { return false }; let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false }; let ty::Param(param) = inner_ty.kind() else { return false }; - let Some(generic_param) = generics.get_named(param.name) else { return false }; let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false }; let arg_node = self.tcx.hir().get(*arg_hir_id); let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node else { return false }; let clone_trait = self.tcx.require_lang_item(LangItem::Clone, None); - let has_clone = self - .type_implements_trait(clone_trait, [ty], obligation.param_env) - .must_apply_modulo_regions(); + let has_clone = |ty| { + self.type_implements_trait(clone_trait, [ty], obligation.param_env) + .must_apply_modulo_regions() + }; - let trait_pred_and_suggested_ty = - trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)); let new_obligation = self.mk_trait_obligation_with_new_self_ty( obligation.param_env, - trait_pred_and_suggested_ty, + trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)), ); - if has_clone && self.predicate_may_hold(&new_obligation) { - let clone_bound = generics - .bounds_for_param(generic_param.def_id) - .flat_map(|bp| bp.bounds) - .any(|bound| { - if let hir::GenericBound::Trait(hir::PolyTraitRef { trait_ref, .. }, ..) = bound - { - Some(clone_trait) == trait_ref.trait_def_id() - } else { - false - } - }); - if !clone_bound { + if self.predicate_may_hold(&new_obligation) && has_clone(ty) { + if !has_clone(param.to_ty(self.tcx)) { suggest_constraining_type_param( self.tcx, generics, diff --git a/src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs b/src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs index 4e4d4c044c9..3b2e316b296 100644 --- a/src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs +++ b/src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs @@ -6,11 +6,11 @@ trait X {} impl X for S {} fn foo<T: X>(_: T) {} -fn bar<T: X>(s: &T) { +fn bar<T: X>(s: &T) { foo(s); //~ ERROR the trait bound `&T: X` is not satisfied } -fn bar_with_clone<T: X + Clone>(s: &T) { +fn bar_with_clone<T: X + Clone>(s: &T) { foo(s); //~ ERROR the trait bound `&T: X` is not satisfied } |
