Skip to content

Commit eefc44b

Browse files
Rollup merge of #106497 - chenyukang:yukang/fix-106443-sugg-clone, r=estebank
Suggest using clone when we have &T and T implemented Clone Fixes #106443
2 parents 334426b + 0e570e5 commit eefc44b

File tree

8 files changed

+200
-1
lines changed

8 files changed

+200
-1
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5757
|| self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty)
5858
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
5959
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
60+
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
6061
|| self.suggest_into(err, expr, expr_ty, expected)
6162
|| self.suggest_floating_point_literal(err, expr, expected);
6263
if !suggested {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10141014
}
10151015
}
10161016

1017+
pub(crate) fn suggest_clone_for_ref(
1018+
&self,
1019+
diag: &mut Diagnostic,
1020+
expr: &hir::Expr<'_>,
1021+
expr_ty: Ty<'tcx>,
1022+
expected_ty: Ty<'tcx>,
1023+
) -> bool {
1024+
if let ty::Ref(_, inner_ty, hir::Mutability::Not) = expr_ty.kind()
1025+
&& let Some(clone_trait_def) = self.tcx.lang_items().clone_trait()
1026+
&& expected_ty == *inner_ty
1027+
&& self
1028+
.infcx
1029+
.type_implements_trait(
1030+
clone_trait_def,
1031+
[self.tcx.erase_regions(expected_ty)],
1032+
self.param_env
1033+
)
1034+
.must_apply_modulo_regions()
1035+
{
1036+
diag.span_suggestion_verbose(
1037+
expr.span.shrink_to_hi(),
1038+
"consider using clone here",
1039+
".clone()",
1040+
Applicability::MachineApplicable,
1041+
);
1042+
return true;
1043+
}
1044+
false
1045+
}
1046+
10171047
pub(crate) fn suggest_copied_or_cloned(
10181048
&self,
10191049
diag: &mut Diagnostic,

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

+5
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
873873
);
874874
}
875875

876+
if self.suggest_add_clone_to_arg(&obligation, &mut err, trait_predicate) {
877+
err.emit();
878+
return;
879+
}
880+
876881
if self.suggest_impl_trait(&mut err, span, &obligation, trait_predicate) {
877882
err.emit();
878883
return;

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

+57-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::infer::InferCtxt;
1010
use crate::traits::{NormalizeExt, ObligationCtxt};
1111

1212
use hir::def::CtorOf;
13-
use hir::HirId;
13+
use hir::{Expr, HirId};
1414
use rustc_data_structures::fx::FxHashSet;
1515
use rustc_data_structures::stack::ensure_sufficient_stack;
1616
use rustc_errors::{
@@ -206,6 +206,13 @@ pub trait TypeErrCtxtExt<'tcx> {
206206
trait_pred: ty::PolyTraitPredicate<'tcx>,
207207
);
208208

209+
fn suggest_add_clone_to_arg(
210+
&self,
211+
obligation: &PredicateObligation<'tcx>,
212+
err: &mut Diagnostic,
213+
trait_pred: ty::PolyTraitPredicate<'tcx>,
214+
) -> bool;
215+
209216
fn suggest_add_reference_to_arg(
210217
&self,
211218
obligation: &PredicateObligation<'tcx>,
@@ -1102,6 +1109,55 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11021109
}
11031110
}
11041111

1112+
fn suggest_add_clone_to_arg(
1113+
&self,
1114+
obligation: &PredicateObligation<'tcx>,
1115+
err: &mut Diagnostic,
1116+
trait_pred: ty::PolyTraitPredicate<'tcx>,
1117+
) -> bool {
1118+
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
1119+
let ty = self.tcx.erase_late_bound_regions(self_ty);
1120+
let owner = self.tcx.hir().get_parent_item(obligation.cause.body_id);
1121+
let Some(generics) = self.tcx.hir().get_generics(owner.def_id) else { return false };
1122+
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
1123+
let ty::Param(param) = inner_ty.kind() else { return false };
1124+
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false };
1125+
let arg_node = self.tcx.hir().get(*arg_hir_id);
1126+
let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node else { return false };
1127+
1128+
let clone_trait = self.tcx.require_lang_item(LangItem::Clone, None);
1129+
let has_clone = |ty| {
1130+
self.type_implements_trait(clone_trait, [ty], obligation.param_env)
1131+
.must_apply_modulo_regions()
1132+
};
1133+
1134+
let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1135+
obligation.param_env,
1136+
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
1137+
);
1138+
1139+
if self.predicate_may_hold(&new_obligation) && has_clone(ty) {
1140+
if !has_clone(param.to_ty(self.tcx)) {
1141+
suggest_constraining_type_param(
1142+
self.tcx,
1143+
generics,
1144+
err,
1145+
param.name.as_str(),
1146+
"Clone",
1147+
Some(clone_trait),
1148+
);
1149+
}
1150+
err.span_suggestion_verbose(
1151+
obligation.cause.span.shrink_to_hi(),
1152+
"consider using clone here",
1153+
".clone()".to_string(),
1154+
Applicability::MaybeIncorrect,
1155+
);
1156+
return true;
1157+
}
1158+
false
1159+
}
1160+
11051161
fn suggest_add_reference_to_arg(
11061162
&self,
11071163
obligation: &PredicateObligation<'tcx>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[derive(Clone)]
2+
struct S;
3+
4+
// without Clone
5+
struct T;
6+
7+
fn foo(_: S) {}
8+
9+
fn test1() {
10+
let s = &S;
11+
foo(s); //~ ERROR mismatched types
12+
}
13+
14+
fn bar(_: T) {}
15+
fn test2() {
16+
let t = &T;
17+
bar(t); //~ ERROR mismatched types
18+
}
19+
20+
fn main() {
21+
test1();
22+
test2();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-106443-sugg-clone-for-arg.rs:11:9
3+
|
4+
LL | foo(s);
5+
| --- ^ expected struct `S`, found `&S`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
note: function defined here
10+
--> $DIR/issue-106443-sugg-clone-for-arg.rs:7:4
11+
|
12+
LL | fn foo(_: S) {}
13+
| ^^^ ----
14+
help: consider using clone here
15+
|
16+
LL | foo(s.clone());
17+
| ++++++++
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/issue-106443-sugg-clone-for-arg.rs:17:9
21+
|
22+
LL | bar(t);
23+
| --- ^ expected struct `T`, found `&T`
24+
| |
25+
| arguments to this function are incorrect
26+
|
27+
note: function defined here
28+
--> $DIR/issue-106443-sugg-clone-for-arg.rs:14:4
29+
|
30+
LL | fn bar(_: T) {}
31+
| ^^^ ----
32+
33+
error: aborting due to 2 previous errors
34+
35+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[derive(Clone)]
2+
struct S;
3+
4+
trait X {}
5+
6+
impl X for S {}
7+
8+
fn foo<T: X>(_: T) {}
9+
fn bar<T: X>(s: &T) {
10+
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied
11+
}
12+
13+
fn bar_with_clone<T: X + Clone>(s: &T) {
14+
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied
15+
}
16+
17+
fn main() {
18+
let s = &S;
19+
bar(s);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0277]: the trait bound `&T: X` is not satisfied
2+
--> $DIR/issue-106443-sugg-clone-for-bound.rs:10:9
3+
|
4+
LL | foo(s);
5+
| ^ the trait `X` is not implemented for `&T`
6+
|
7+
help: consider further restricting this bound
8+
|
9+
LL | fn bar<T: X + Clone>(s: &T) {
10+
| +++++++
11+
help: consider using clone here
12+
|
13+
LL | foo(s.clone());
14+
| ++++++++
15+
16+
error[E0277]: the trait bound `&T: X` is not satisfied
17+
--> $DIR/issue-106443-sugg-clone-for-bound.rs:14:9
18+
|
19+
LL | foo(s);
20+
| ^ the trait `X` is not implemented for `&T`
21+
|
22+
help: consider using clone here
23+
|
24+
LL | foo(s.clone());
25+
| ++++++++
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)