Skip to content

Commit e7a7778

Browse files
committed
Suggest possible clone when we have &T
1 parent 2afe585 commit e7a7778

File tree

8 files changed

+212
-2
lines changed

8 files changed

+212
-2
lines changed

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,35 @@ 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+
diag.span_suggestion_verbose(
1036+
expr.span.shrink_to_hi(),
1037+
"consider using clone here",
1038+
".clone()",
1039+
Applicability::MachineApplicable,
1040+
);
1041+
return true;
1042+
}
1043+
false
1044+
}
1045+
10171046
pub(crate) fn suggest_copied_or_cloned(
10181047
&self,
10191048
diag: &mut Diagnostic,

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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 70 additions & 2 deletions
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,11 +206,15 @@ pub trait TypeErrCtxtExt<'tcx> {
206206
trait_pred: ty::PolyTraitPredicate<'tcx>,
207207
);
208208

209-
fn suggest_add_reference_to_arg(
209+
fn suggest_add_clone_to_arg(
210210
&self,
211211
obligation: &PredicateObligation<'tcx>,
212212
err: &mut Diagnostic,
213213
trait_pred: ty::PolyTraitPredicate<'tcx>,
214+
);
215+
216+
fn suggest_add_reference_to_arg(
217+
err: &mut Diagnostic,
214218
has_custom_message: bool,
215219
) -> bool;
216220

@@ -1102,6 +1106,70 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11021106
}
11031107
}
11041108

1109+
fn suggest_add_clone_to_arg(
1110+
&self,
1111+
obligation: &PredicateObligation<'tcx>,
1112+
err: &mut Diagnostic,
1113+
trait_pred: ty::PolyTraitPredicate<'tcx>,
1114+
) -> bool {
1115+
let span = obligation.cause.span;
1116+
let body_id = obligation.cause.body_id;
1117+
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
1118+
let ty = self.tcx.erase_late_bound_regions(self_ty);
1119+
let owner = self.tcx.hir().get_parent_item(body_id);
1120+
if let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() &&
1121+
let arg_node = self.tcx.hir().get(*arg_hir_id) &&
1122+
let Node::Expr(Expr { kind: hir::ExprKind::Path(_), ..}) = arg_node &&
1123+
let Some(generics) = self.tcx.hir().get_generics(owner.def_id) &&
1124+
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() &&
1125+
let ty::Param(param) = inner_ty.kind() &&
1126+
let Some(generic_param) =
1127+
generics.params.iter().find(|p| p.name.ident().as_str() == param.name.as_str())
1128+
{
1129+
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();
1133+
1134+
let trait_pred_and_suggested_ty =
1135+
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty));
1136+
let new_obligation = self.mk_trait_obligation_with_new_self_ty(
1137+
obligation.param_env,
1138+
trait_pred_and_suggested_ty,
1139+
);
1140+
1141+
if has_clone && self.predicate_may_hold(&new_obligation) {
1142+
let clone_bound = generics.bounds_for_param(generic_param.def_id)
1143+
.flat_map(|bp| bp.bounds)
1144+
.any(|bound| {
1145+
if let hir::GenericBound::Trait( hir::PolyTraitRef { trait_ref, ..}, ..) = bound {
1146+
Some(clone_trait) == trait_ref.trait_def_id()
1147+
} else {
1148+
false
1149+
}
1150+
});
1151+
if !clone_bound {
1152+
suggest_constraining_type_param(
1153+
self.tcx,
1154+
generics,
1155+
err,
1156+
param.name.as_str(),
1157+
"Clone",
1158+
Some(clone_trait)
1159+
);
1160+
}
1161+
err.span_suggestion_verbose(
1162+
span.shrink_to_hi(),
1163+
"consider using clone here",
1164+
".clone()".to_string(),
1165+
Applicability::MaybeIncorrect,
1166+
);
1167+
return true;
1168+
}
1169+
}
1170+
false
1171+
}
1172+
11051173
fn suggest_add_reference_to_arg(
11061174
&self,
11071175
obligation: &PredicateObligation<'tcx>,
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 35 additions & 0 deletions
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`.
Lines changed: 20 additions & 0 deletions
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+
}
Lines changed: 29 additions & 0 deletions
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)