Skip to content

Commit c52b60f

Browse files
Rollup merge of #158430 - TaKO8Ki:fix-clone-suggestion-empty-obligations, r=Kivooeo
Guard clone suggestion against empty obligation errors Fixes #148631
2 parents 1b6b2e6 + f08762c commit c52b60f

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,15 +1461,19 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14611461
let cause = ObligationCause::misc(expr.span, self.mir_def_id());
14621462
ocx.register_bound(cause, self.infcx.param_env, ty, clone_trait);
14631463
let errors = ocx.evaluate_obligations_error_on_ambiguity();
1464-
if errors.iter().all(|error| {
1465-
match error.obligation.predicate.as_clause().and_then(|c| c.as_trait_clause()) {
1466-
Some(clause) => match clause.self_ty().skip_binder().kind() {
1467-
ty::Adt(def, _) => def.did().is_local() && clause.def_id() == clone_trait,
1468-
_ => false,
1469-
},
1470-
None => false,
1471-
}
1472-
}) {
1464+
if !errors.is_empty()
1465+
&& errors.iter().all(|error| {
1466+
match error.obligation.predicate.as_clause().and_then(|c| c.as_trait_clause()) {
1467+
Some(clause) => match clause.self_ty().skip_binder().kind() {
1468+
ty::Adt(def, _) => {
1469+
def.did().is_local() && clause.def_id() == clone_trait
1470+
}
1471+
_ => false,
1472+
},
1473+
None => false,
1474+
}
1475+
})
1476+
{
14731477
let mut type_spans = vec![];
14741478
let mut types = FxIndexSet::default();
14751479
for clause in errors
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/148631
2+
3+
struct C;
4+
5+
struct S<T>(T);
6+
7+
trait Tr {}
8+
9+
impl<T> Clone for S<C>
10+
//~^ ERROR the type parameter `T` is not constrained
11+
where
12+
S<T>: Tr,
13+
{
14+
fn clone(&self) -> Self {
15+
*self
16+
//~^ ERROR cannot move out of `*self`
17+
}
18+
}
19+
20+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/unconstrained-impl-param-clone-suggestion.rs:9:6
3+
|
4+
LL | impl<T> Clone for S<C>
5+
| -^-
6+
| ||
7+
| |unconstrained type parameter
8+
| help: remove the unused type parameter `T`
9+
10+
error[E0507]: cannot move out of `*self` which is behind a shared reference
11+
--> $DIR/unconstrained-impl-param-clone-suggestion.rs:15:9
12+
|
13+
LL | *self
14+
| ^^^^^ move occurs because `*self` has type `S<C>`, which does not implement the `Copy` trait
15+
16+
error: aborting due to 2 previous errors
17+
18+
Some errors have detailed explanations: E0207, E0507.
19+
For more information about an error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)