From bc9232183b90817ed41a369361265127a268f072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 13 Dec 2022 18:55:00 -0800 Subject: [PATCH 1/2] Suggest constraining type parameter with `Clone` Fix #34896. --- .../src/fn_ctxt/suggestions.rs | 15 ++++++++++- ...on-unconstrained-borrowed-type-param.fixed | 8 ++++++ ...ne-on-unconstrained-borrowed-type-param.rs | 8 ++++++ ...n-unconstrained-borrowed-type-param.stderr | 25 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed create mode 100644 src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs create mode 100644 src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 4f92477b5d87e..c98cf4f8a37a4 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -13,7 +13,9 @@ use rustc_hir_analysis::astconv::AstConv; use rustc_infer::infer; use rustc_infer::traits::{self, StatementAsExpression}; use rustc_middle::lint::in_external_macro; -use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty}; +use rustc_middle::ty::{ + self, suggest_constraining_type_params, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty, +}; use rustc_session::errors::ExprParenthesesNeeded; use rustc_span::symbol::sym; use rustc_span::Span; @@ -1295,6 +1297,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "`{expected_ty}` does not implement `Clone`, so `{found_ty}` was cloned instead" ), ); + let owner = self.tcx.hir().enclosing_body_owner(expr.hir_id); + if let ty::Param(param) = expected_ty.kind() + && let Some(generics) = self.tcx.hir().get_generics(owner) + { + suggest_constraining_type_params( + self.tcx, + generics, + diag, + vec![(param.name.as_str(), "Clone", Some(clone_trait_did))].into_iter(), + ); + } } } diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed new file mode 100644 index 0000000000000..3e5ded6738b31 --- /dev/null +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed @@ -0,0 +1,8 @@ +// run-rustfix +fn wat(t: &T) -> T { + t.clone() //~ ERROR E0308 +} + +fn main() { + wat(&42); +} diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs new file mode 100644 index 0000000000000..1a5a38369ec52 --- /dev/null +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs @@ -0,0 +1,8 @@ +// run-rustfix +fn wat(t: &T) -> T { + t.clone() //~ ERROR E0308 +} + +fn main() { + wat(&42); +} diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr new file mode 100644 index 0000000000000..01246955fae8a --- /dev/null +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr @@ -0,0 +1,25 @@ +error[E0308]: mismatched types + --> $DIR/clone-on-unconstrained-borrowed-type-param.rs:3:5 + | +LL | fn wat(t: &T) -> T { + | - - expected `T` because of return type + | | + | this type parameter +LL | t.clone() + | ^^^^^^^^^ expected type parameter `T`, found `&T` + | + = note: expected type parameter `T` + found reference `&T` +note: `T` does not implement `Clone`, so `&T` was cloned instead + --> $DIR/clone-on-unconstrained-borrowed-type-param.rs:3:5 + | +LL | t.clone() + | ^ +help: consider restricting type parameter `T` + | +LL | fn wat(t: &T) -> T { + | +++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From 0fb8d848f9d3b427fea754a5d944375e049f2eb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 13 Dec 2022 19:52:42 -0800 Subject: [PATCH 2/2] Suggest `#[derive(Clone)]` --- .../src/fn_ctxt/suggestions.rs | 15 ++++++++------ .../rustc_hir_typeck/src/method/suggest.rs | 2 +- ...on-unconstrained-borrowed-type-param.fixed | 8 ++++++++ ...ne-on-unconstrained-borrowed-type-param.rs | 7 +++++++ ...n-unconstrained-borrowed-type-param.stderr | 20 ++++++++++++++++++- .../ui/typeck/explain_clone_autoref.stderr | 4 ++++ 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index c98cf4f8a37a4..7ef2dabcce2a0 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1278,17 +1278,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { && !results.expr_adjustments(callee_expr).iter().any(|adj| matches!(adj.kind, ty::adjustment::Adjust::Deref(..))) // Check that we're in fact trying to clone into the expected type && self.can_coerce(*pointee_ty, expected_ty) + && let predicate = ty::Binder::dummy(ty::TraitRef { + def_id: clone_trait_did, + substs: self.tcx.mk_substs([expected_ty.into()].iter()), + }) + .without_const() + .to_predicate(self.tcx) // And the expected type doesn't implement `Clone` && !self.predicate_must_hold_considering_regions(&traits::Obligation { cause: traits::ObligationCause::dummy(), param_env: self.param_env, recursion_depth: 0, - predicate: ty::Binder::dummy(ty::TraitRef { - def_id: clone_trait_did, - substs: self.tcx.mk_substs([expected_ty.into()].iter()), - }) - .without_const() - .to_predicate(self.tcx), + predicate, }) { diag.span_note( @@ -1307,6 +1308,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { diag, vec![(param.name.as_str(), "Clone", Some(clone_trait_did))].into_iter(), ); + } else { + self.suggest_derive(diag, &[(predicate, None, None)]); } } } diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 2d3b4663f06ce..9fe132fc94645 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1843,7 +1843,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.suggest_derive(err, &preds); } - fn suggest_derive( + pub fn suggest_derive( &self, err: &mut Diagnostic, unsatisfied_predicates: &[( diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed index 3e5ded6738b31..4f9e93a47ed1c 100644 --- a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.fixed @@ -3,6 +3,14 @@ fn wat(t: &T) -> T { t.clone() //~ ERROR E0308 } +#[derive(Clone)] +struct Foo; + +fn wut(t: &Foo) -> Foo { + t.clone() //~ ERROR E0308 +} + fn main() { wat(&42); + wut(&Foo); } diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs index 1a5a38369ec52..89b077d671a51 100644 --- a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.rs @@ -3,6 +3,13 @@ fn wat(t: &T) -> T { t.clone() //~ ERROR E0308 } +struct Foo; + +fn wut(t: &Foo) -> Foo { + t.clone() //~ ERROR E0308 +} + fn main() { wat(&42); + wut(&Foo); } diff --git a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr index 01246955fae8a..26ab515d9b4b6 100644 --- a/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr +++ b/src/test/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr @@ -20,6 +20,24 @@ help: consider restricting type parameter `T` LL | fn wat(t: &T) -> T { | +++++++ -error: aborting due to previous error +error[E0308]: mismatched types + --> $DIR/clone-on-unconstrained-borrowed-type-param.rs:9:5 + | +LL | fn wut(t: &Foo) -> Foo { + | --- expected `Foo` because of return type +LL | t.clone() + | ^^^^^^^^^ expected struct `Foo`, found `&Foo` + | +note: `Foo` does not implement `Clone`, so `&Foo` was cloned instead + --> $DIR/clone-on-unconstrained-borrowed-type-param.rs:9:5 + | +LL | t.clone() + | ^ +help: consider annotating `Foo` with `#[derive(Clone)]` + | +LL | #[derive(Clone)] + | + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/typeck/explain_clone_autoref.stderr b/src/test/ui/typeck/explain_clone_autoref.stderr index faac680ea1931..ff36e18d28301 100644 --- a/src/test/ui/typeck/explain_clone_autoref.stderr +++ b/src/test/ui/typeck/explain_clone_autoref.stderr @@ -12,6 +12,10 @@ note: `NotClone` does not implement `Clone`, so `&NotClone` was cloned instead | LL | nc.clone() | ^^ +help: consider annotating `NotClone` with `#[derive(Clone)]` + | +LL | #[derive(Clone)] + | error: aborting due to previous error