1
Fork 0

Fix suggestion regression with incorrect syntactic combination of trait bounds

This commit is contained in:
Will Crichton 2022-07-15 18:02:26 -07:00
parent e5bb7d80d6
commit 2f15dfab0b
4 changed files with 26 additions and 4 deletions

View file

@ -599,8 +599,20 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if let Some(proj_pred) = proj_pred { if let Some(proj_pred) = proj_pred {
let ProjectionPredicate { projection_ty, term } = proj_pred.skip_binder(); let ProjectionPredicate { projection_ty, term } = proj_pred.skip_binder();
let item = self.tcx.associated_item(projection_ty.item_def_id); let item = self.tcx.associated_item(projection_ty.item_def_id);
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
// That should be extracted into a helper function.
if constraint.ends_with('>') {
constraint = format!(
"{}, {}={}>",
&constraint[..constraint.len() - 1],
item.name,
term.to_string()
);
} else {
constraint.push_str(&format!("<{}={}>", item.name, term.to_string())); constraint.push_str(&format!("<{}={}>", item.name, term.to_string()));
} }
}
if suggest_constraining_type_param( if suggest_constraining_type_param(
self.tcx, self.tcx,

View file

@ -0,0 +1,8 @@
// run-rustfix
fn add_ten<N: std::ops::Add<i32, Output=N>>(n: N) -> N {
n + 10
//~^ ERROR cannot add `{integer}` to `N`
}
fn main() { add_ten(0); }

View file

@ -1,6 +1,8 @@
// run-rustfix
fn add_ten<N>(n: N) -> N { fn add_ten<N>(n: N) -> N {
n + 10 n + 10
//~^ ERROR cannot add `{integer}` to `N` //~^ ERROR cannot add `{integer}` to `N`
} }
fn main() {} fn main() { add_ten(0); }

View file

@ -1,5 +1,5 @@
error[E0369]: cannot add `{integer}` to `N` error[E0369]: cannot add `{integer}` to `N`
--> $DIR/issue-97677.rs:2:7 --> $DIR/issue-97677.rs:4:7
| |
LL | n + 10 LL | n + 10
| - ^ -- {integer} | - ^ -- {integer}
@ -8,7 +8,7 @@ LL | n + 10
| |
help: consider restricting type parameter `N` help: consider restricting type parameter `N`
| |
LL | fn add_ten<N: std::ops::Add<i32><Output=N>>(n: N) -> N { LL | fn add_ten<N: std::ops::Add<i32, Output=N>>(n: N) -> N {
| ++++++++++++++++++++++++++++++ | ++++++++++++++++++++++++++++++
error: aborting due to previous error error: aborting due to previous error