Skip to content

Commit

Permalink
Fix suggestion regression with incorrect syntactic combination of tra…
Browse files Browse the repository at this point in the history
…it bounds
  • Loading branch information
willcrichton committed Jul 16, 2022
1 parent e5bb7d8 commit 2f15dfa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if let Some(proj_pred) = proj_pred {
let ProjectionPredicate { projection_ty, term } = proj_pred.skip_binder();
let item = self.tcx.associated_item(projection_ty.item_def_id);
constraint.push_str(&format!("<{}={}>", item.name, term.to_string()));

// 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()));
}
}

if suggest_constraining_type_param(
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/suggestions/issue-97677.fixed
Original file line number Diff line number Diff line change
@@ -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); }
4 changes: 3 additions & 1 deletion src/test/ui/suggestions/issue-97677.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// run-rustfix

fn add_ten<N>(n: N) -> N {
n + 10
//~^ ERROR cannot add `{integer}` to `N`
}

fn main() {}
fn main() { add_ten(0); }
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/issue-97677.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0369]: cannot add `{integer}` to `N`
--> $DIR/issue-97677.rs:2:7
--> $DIR/issue-97677.rs:4:7
|
LL | n + 10
| - ^ -- {integer}
Expand All @@ -8,7 +8,7 @@ LL | n + 10
|
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
Expand Down

0 comments on commit 2f15dfa

Please sign in to comment.