From f19488064a4752703fde8a3596a9d90ca2095534 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] 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 719f44f9f665a..08ef2484776b2 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, Ty}; +use rustc_middle::ty::{ + self, suggest_constraining_type_params, Binder, DefIdTree, IsSuggestable, Ty, +}; use rustc_session::errors::ExprParenthesesNeeded; use rustc_span::symbol::sym; use rustc_span::Span; @@ -1293,6 +1295,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`.