Skip to content

Commit 6082729

Browse files
committed
use type_implements_trait to check Param clone
1 parent ce4afed commit 6082729

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

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

+7-20
Original file line numberDiff line numberDiff line change
@@ -1121,36 +1121,23 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11211121
let Some(generics) = self.tcx.hir().get_generics(owner.def_id) else { return false };
11221122
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
11231123
let ty::Param(param) = inner_ty.kind() else { return false };
1124-
let Some(generic_param) = generics.get_named(param.name) else { return false };
11251124
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false };
11261125
let arg_node = self.tcx.hir().get(*arg_hir_id);
11271126
let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node else { return false };
11281127

11291128
let clone_trait = self.tcx.require_lang_item(LangItem::Clone, None);
1130-
let has_clone = self
1131-
.type_implements_trait(clone_trait, [ty], obligation.param_env)
1132-
.must_apply_modulo_regions();
1129+
let has_clone = |ty| {
1130+
self.type_implements_trait(clone_trait, [ty], obligation.param_env)
1131+
.must_apply_modulo_regions()
1132+
};
11331133

1134-
let trait_pred_and_suggested_ty =
1135-
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty));
11361134
let new_obligation = self.mk_trait_obligation_with_new_self_ty(
11371135
obligation.param_env,
1138-
trait_pred_and_suggested_ty,
1136+
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
11391137
);
11401138

1141-
if has_clone && self.predicate_may_hold(&new_obligation) {
1142-
let clone_bound = generics
1143-
.bounds_for_param(generic_param.def_id)
1144-
.flat_map(|bp| bp.bounds)
1145-
.any(|bound| {
1146-
if let hir::GenericBound::Trait(hir::PolyTraitRef { trait_ref, .. }, ..) = bound
1147-
{
1148-
Some(clone_trait) == trait_ref.trait_def_id()
1149-
} else {
1150-
false
1151-
}
1152-
});
1153-
if !clone_bound {
1139+
if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
1140+
if !has_clone(param.to_ty(self.tcx)) {
11541141
suggest_constraining_type_param(
11551142
self.tcx,
11561143
generics,

src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ trait X {}
66
impl X for S {}
77

88
fn foo<T: X>(_: T) {}
9-
fn bar<T: X>(s: &T) {
9+
fn bar<T: X>(s: &T) {
1010
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied
1111
}
1212

13-
fn bar_with_clone<T: X + Clone>(s: &T) {
13+
fn bar_with_clone<T: X + Clone>(s: &T) {
1414
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied
1515
}
1616

0 commit comments

Comments
 (0)