Skip to content

Commit 2f15dfa

Browse files
committed
Fix suggestion regression with incorrect syntactic combination of trait bounds
1 parent e5bb7d8 commit 2f15dfa

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
599599
if let Some(proj_pred) = proj_pred {
600600
let ProjectionPredicate { projection_ty, term } = proj_pred.skip_binder();
601601
let item = self.tcx.associated_item(projection_ty.item_def_id);
602-
constraint.push_str(&format!("<{}={}>", item.name, term.to_string()));
602+
603+
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
604+
// That should be extracted into a helper function.
605+
if constraint.ends_with('>') {
606+
constraint = format!(
607+
"{}, {}={}>",
608+
&constraint[..constraint.len() - 1],
609+
item.name,
610+
term.to_string()
611+
);
612+
} else {
613+
constraint.push_str(&format!("<{}={}>", item.name, term.to_string()));
614+
}
603615
}
604616

605617
if suggest_constraining_type_param(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
fn add_ten<N: std::ops::Add<i32, Output=N>>(n: N) -> N {
4+
n + 10
5+
//~^ ERROR cannot add `{integer}` to `N`
6+
}
7+
8+
fn main() { add_ten(0); }
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// run-rustfix
2+
13
fn add_ten<N>(n: N) -> N {
24
n + 10
35
//~^ ERROR cannot add `{integer}` to `N`
46
}
57

6-
fn main() {}
8+
fn main() { add_ten(0); }

src/test/ui/suggestions/issue-97677.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0369]: cannot add `{integer}` to `N`
2-
--> $DIR/issue-97677.rs:2:7
2+
--> $DIR/issue-97677.rs:4:7
33
|
44
LL | n + 10
55
| - ^ -- {integer}
@@ -8,7 +8,7 @@ LL | n + 10
88
|
99
help: consider restricting type parameter `N`
1010
|
11-
LL | fn add_ten<N: std::ops::Add<i32><Output=N>>(n: N) -> N {
11+
LL | fn add_ten<N: std::ops::Add<i32, Output=N>>(n: N) -> N {
1212
| ++++++++++++++++++++++++++++++
1313

1414
error: aborting due to previous error

0 commit comments

Comments
 (0)