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

Rollup of 9 pull requests #105721

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bc92321
Suggest constraining type parameter with `Clone`
estebank Dec 14, 2022
0fb8d84
Suggest `#[derive(Clone)]`
estebank Dec 14, 2022
687b4d9
Add regression test for #104678
JohnTitor Dec 14, 2022
5f5ae17
Consider discriminant fields that are ordered before variant fields
compiler-errors Dec 12, 2022
bdc3c4b
Make print_type_sizes test not use feature(start)
compiler-errors Dec 12, 2022
10368c6
Point at method chains on `E0271` errors
estebank Dec 13, 2022
36b60e7
Use `with_forced_trimmed_paths` more
estebank Dec 13, 2022
50e269f
Shorten trimmed display of closures
estebank Dec 14, 2022
e9ef36a
Trim paths in E0599
estebank Dec 14, 2022
7f8fdf4
Consider lifetimes when comparing assoc types in method chain
estebank Dec 14, 2022
ae60015
Use impl's def id when calculating type to specify UFCS
compiler-errors Nov 12, 2022
1225a65
drive-by: Fix path spans
compiler-errors Nov 12, 2022
7bf36de
Make report_projection_error more term agnostic
compiler-errors Dec 12, 2022
cfa6a93
Auto traits in dyn are suggestable
compiler-errors Dec 12, 2022
34d194d
Highlight conflicting param-env candidates, again
compiler-errors Nov 15, 2022
d10f6b4
Add test
compiler-errors Dec 5, 2022
c99f1b7
Run `x test tidy` sooner in mingw-check
jyn514 Dec 14, 2022
fc6d59d
Make `RUN_CHECK_WITH_PARALLEL_QUERIES` the last thing to run
jyn514 Dec 14, 2022
0277c72
Rollup merge of #104334 - compiler-errors:ufcs-sugg-wrong-def-id, r=e…
matthiaskrgr Dec 14, 2022
52d6fbc
Rollup merge of #105285 - compiler-errors:conflicting-param-env-2, r=…
matthiaskrgr Dec 14, 2022
d9e36a4
Rollup merge of #105623 - compiler-errors:generator-type-size-fix, r=…
matthiaskrgr Dec 14, 2022
68badbd
Rollup merge of #105627 - compiler-errors:dyn-auto-suggestable, r=dav…
matthiaskrgr Dec 14, 2022
d2b716d
Rollup merge of #105633 - compiler-errors:term-agnostic, r=oli-obk
matthiaskrgr Dec 14, 2022
2a5d257
Rollup merge of #105674 - estebank:iterator-chains, r=oli-obk
matthiaskrgr Dec 14, 2022
1fcd973
Rollup merge of #105679 - estebank:suggest-clone, r=compiler-errors
matthiaskrgr Dec 14, 2022
e657bbc
Rollup merge of #105692 - JohnTitor:issue-104678, r=compiler-errors
matthiaskrgr Dec 14, 2022
98b80fc
Rollup merge of #105714 - jyn514:tidy-first, r=Mark-Simulacrum
matthiaskrgr Dec 14, 2022
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
Next Next commit
Suggest constraining type parameter with Clone
Fix #34896.
  • Loading branch information
estebank committed Dec 14, 2022
commit bc9232183b90817ed41a369361265127a268f072
15 changes: 14 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix
fn wat<T: Clone>(t: &T) -> T {
t.clone() //~ ERROR E0308
}

fn main() {
wat(&42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix
fn wat<T>(t: &T) -> T {
t.clone() //~ ERROR E0308
}

fn main() {
wat(&42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0308]: mismatched types
--> $DIR/clone-on-unconstrained-borrowed-type-param.rs:3:5
|
LL | fn wat<T>(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: Clone>(t: &T) -> T {
| +++++++

error: aborting due to previous error

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