Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest using clone when we have &T and T implemented Clone #106497

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty)
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
|| self.suggest_into(err, expr, expr_ty, expected)
|| self.suggest_floating_point_literal(err, expr, expected);
if !suggested {
Expand Down
30 changes: 30 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

pub(crate) fn suggest_clone_for_ref(
&self,
diag: &mut Diagnostic,
expr: &hir::Expr<'_>,
expr_ty: Ty<'tcx>,
expected_ty: Ty<'tcx>,
) -> bool {
if let ty::Ref(_, inner_ty, hir::Mutability::Not) = expr_ty.kind()
&& let Some(clone_trait_def) = self.tcx.lang_items().clone_trait()
&& expected_ty == *inner_ty
&& self
.infcx
.type_implements_trait(
clone_trait_def,
[self.tcx.erase_regions(expected_ty)],
self.param_env
)
.must_apply_modulo_regions()
{
Comment on lines +1027 to +1035
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this I'm thinking we should also check if the type implements Drop, and if so tweak the wording to be less confident, but this can be addressed in a later PR.

diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"consider using clone here",
".clone()",
Applicability::MachineApplicable,
);
return true;
}
false
}

pub(crate) fn suggest_copied_or_cloned(
&self,
diag: &mut Diagnostic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
);
}

if self.suggest_add_clone_to_arg(&obligation, &mut err, trait_predicate) {
err.emit();
return;
}

if self.suggest_impl_trait(&mut err, span, &obligation, trait_predicate) {
err.emit();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::infer::InferCtxt;
use crate::traits::{NormalizeExt, ObligationCtxt};

use hir::def::CtorOf;
use hir::HirId;
use hir::{Expr, HirId};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{
Expand Down Expand Up @@ -206,6 +206,13 @@ pub trait TypeErrCtxtExt<'tcx> {
trait_pred: ty::PolyTraitPredicate<'tcx>,
);

fn suggest_add_clone_to_arg(
&self,
obligation: &PredicateObligation<'tcx>,
err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool;

fn suggest_add_reference_to_arg(
&self,
obligation: &PredicateObligation<'tcx>,
Expand Down Expand Up @@ -1102,6 +1109,55 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}
}

fn suggest_add_clone_to_arg(
estebank marked this conversation as resolved.
Show resolved Hide resolved
&self,
obligation: &PredicateObligation<'tcx>,
err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool {
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
let ty = self.tcx.erase_late_bound_regions(self_ty);
let owner = self.tcx.hir().get_parent_item(obligation.cause.body_id);
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 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 = |ty| {
self.type_implements_trait(clone_trait, [ty], obligation.param_env)
.must_apply_modulo_regions()
};

let new_obligation = self.mk_trait_obligation_with_new_self_ty(
obligation.param_env,
trait_pred.map_bound(|trait_pred| (trait_pred, *inner_ty)),
);

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,
err,
param.name.as_str(),
"Clone",
Some(clone_trait),
);
}
err.span_suggestion_verbose(
obligation.cause.span.shrink_to_hi(),
"consider using clone here",
".clone()".to_string(),
Applicability::MaybeIncorrect,
);
return true;
}
false
}

fn suggest_add_reference_to_arg(
&self,
obligation: &PredicateObligation<'tcx>,
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/suggestions/issue-106443-sugg-clone-for-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[derive(Clone)]
struct S;

// without Clone
struct T;

fn foo(_: S) {}

fn test1() {
let s = &S;
foo(s); //~ ERROR mismatched types
}

fn bar(_: T) {}
fn test2() {
let t = &T;
bar(t); //~ ERROR mismatched types
}

fn main() {
test1();
test2();
}
35 changes: 35 additions & 0 deletions src/test/ui/suggestions/issue-106443-sugg-clone-for-arg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0308]: mismatched types
--> $DIR/issue-106443-sugg-clone-for-arg.rs:11:9
|
LL | foo(s);
| --- ^ expected struct `S`, found `&S`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/issue-106443-sugg-clone-for-arg.rs:7:4
|
LL | fn foo(_: S) {}
| ^^^ ----
help: consider using clone here
|
LL | foo(s.clone());
| ++++++++

error[E0308]: mismatched types
--> $DIR/issue-106443-sugg-clone-for-arg.rs:17:9
|
LL | bar(t);
| --- ^ expected struct `T`, found `&T`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/issue-106443-sugg-clone-for-arg.rs:14:4
|
LL | fn bar(_: T) {}
| ^^^ ----

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[derive(Clone)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can be marked as // run-rustfix, because after applying the suggestions the code will compile :)

struct S;

trait X {}

impl X for S {}

fn foo<T: X>(_: 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) {
foo(s); //~ ERROR the trait bound `&T: X` is not satisfied
}

fn main() {
let s = &S;
bar(s);
}
29 changes: 29 additions & 0 deletions src/test/ui/suggestions/issue-106443-sugg-clone-for-bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0277]: the trait bound `&T: X` is not satisfied
--> $DIR/issue-106443-sugg-clone-for-bound.rs:10:9
|
LL | foo(s);
| ^ the trait `X` is not implemented for `&T`
|
help: consider further restricting this bound
|
LL | fn bar<T: X + Clone>(s: &T) {
| +++++++
help: consider using clone here
|
LL | foo(s.clone());
| ++++++++

error[E0277]: the trait bound `&T: X` is not satisfied
--> $DIR/issue-106443-sugg-clone-for-bound.rs:14:9
|
LL | foo(s);
| ^ the trait `X` is not implemented for `&T`
|
help: consider using clone here
|
LL | foo(s.clone());
| ++++++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.