Skip to content

Commit dcb4e81

Browse files
committed
Suggest correct order for args and constraints
1 parent 33d793c commit dcb4e81

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/librustc_ast_passes/ast_validation.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,20 @@ impl<'a> AstValidator<'a> {
663663
_ => None,
664664
})
665665
.collect::<Vec<_>>();
666+
let snippet_span = match &constraint_spans[..] {
667+
[single] => *single,
668+
[first, .., last] => first.to(*last),
669+
[] => unreachable!(),
670+
};
671+
let removal_span = match &arg_spans[..] {
672+
[first, ..] => snippet_span.until(*first),
673+
[] => unreachable!(),
674+
};
675+
let sugg_span = match &arg_spans[..] {
676+
[.., last] => last.shrink_to_hi(),
677+
[] => unreachable!(),
678+
};
679+
let snippet = self.session.source_map().span_to_snippet(snippet_span).unwrap();
666680
let constraint_len = constraint_spans.len();
667681
// ...and then error:
668682
self.err_handler()
@@ -679,6 +693,15 @@ impl<'a> AstValidator<'a> {
679693
),
680694
)
681695
.span_labels(arg_spans, "generic argument")
696+
.multipart_suggestion(
697+
"move the constraints after the generic arguments",
698+
vec![
699+
(removal_span, String::new()),
700+
(sugg_span.shrink_to_lo(), ", ".to_string()),
701+
(sugg_span, snippet),
702+
],
703+
Applicability::MachineApplicable,
704+
)
682705
.emit();
683706
}
684707
}

src/test/ui/parser/issue-32214.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | pub fn test<W, I: Trait<Item=(), W> >() {}
55
| ------- ^ generic argument
66
| |
77
| the constraint is provided here
8+
|
9+
help: move the constraints after the generic arguments
10+
|
11+
LL | pub fn test<W, I: Trait<W, Item=()> >() {}
12+
| --^^^^^^^
813

914
error: aborting due to previous error
1015

src/test/ui/suggestions/suggest-move-types.stderr

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | struct A<T, M: One<A=(), T>> {
55
| ---- ^ generic argument
66
| |
77
| the constraint is provided here
8+
|
9+
help: move the constraints after the generic arguments
10+
|
11+
LL | struct A<T, M: One<T, A=()>> {
12+
| --^^^^
813

914
error: generic arguments must come before the first constraint
1015
--> $DIR/suggest-move-types.rs:33:43
@@ -14,6 +19,11 @@ LL | struct Al<'a, T, M: OneWithLifetime<A=(), T, 'a>> {
1419
| | |
1520
| | generic argument
1621
| the constraint is provided here
22+
|
23+
help: move the constraints after the generic arguments
24+
|
25+
LL | struct Al<'a, T, M: OneWithLifetime<T, 'a, A=()>> {
26+
| -- ^^^^
1727

1828
error: generic arguments must come before the first constraint
1929
--> $DIR/suggest-move-types.rs:40:46
@@ -26,6 +36,11 @@ LL | struct B<T, U, V, M: Three<A=(), B=(), C=(), T, U, V>> {
2636
| | | the constraints are provided here
2737
| | the constraints are provided here
2838
| the constraints are provided here
39+
|
40+
help: move the constraints after the generic arguments
41+
|
42+
LL | struct B<T, U, V, M: Three<T, U, V, A=(), B=(), C=()>> {
43+
| -- ^^^^^^^^^^^^^^^^
2944

3045
error: generic arguments must come before the first constraint
3146
--> $DIR/suggest-move-types.rs:48:71
@@ -41,6 +56,11 @@ LL | struct Bl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<A=(), B=(), C=(), T, U,
4156
| | | the constraints are provided here
4257
| | the constraints are provided here
4358
| the constraints are provided here
59+
|
60+
help: move the constraints after the generic arguments
61+
|
62+
LL | struct Bl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, U, V, 'a, 'b, 'c, A=(), B=(), C=()>> {
63+
| -- ^^^^^^^^^^^^^^^^
4464

4565
error: generic arguments must come before the first constraint
4666
--> $DIR/suggest-move-types.rs:57:28
@@ -53,6 +73,11 @@ LL | struct C<T, U, V, M: Three<T, A=(), B=(), C=(), U, V>> {
5373
| | | the constraints are provided here
5474
| | the constraints are provided here
5575
| generic argument
76+
|
77+
help: move the constraints after the generic arguments
78+
|
79+
LL | struct C<T, U, V, M: Three<A=(), B=(), C=(), U, V, A=(), B=(), C=()>> {
80+
| -- ^^^^^^^^^^^^^^^^
5681

5782
error: generic arguments must come before the first constraint
5883
--> $DIR/suggest-move-types.rs:65:53
@@ -68,6 +93,11 @@ LL | struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, 'a, A=(), B=(), C=()
6893
| | | the constraints are provided here
6994
| | generic argument
7095
| generic argument
96+
|
97+
help: move the constraints after the generic arguments
98+
|
99+
LL | struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<A=(), B=(), C=(), U, 'b, V, 'c, A=(), B=(), C=()>> {
100+
| -- ^^^^^^^^^^^^^^^^
71101

72102
error: generic arguments must come before the first constraint
73103
--> $DIR/suggest-move-types.rs:74:28
@@ -80,6 +110,11 @@ LL | struct D<T, U, V, M: Three<T, A=(), B=(), U, C=(), V>> {
80110
| | | the constraints are provided here
81111
| | the constraints are provided here
82112
| generic argument
113+
|
114+
help: move the constraints after the generic arguments
115+
|
116+
LL | struct D<T, U, V, M: Three<A=(), B=(), U, C=(), V, A=(), B=(), U, C=()>> {
117+
| -- ^^^^^^^^^^^^^^^^^^^
83118

84119
error: generic arguments must come before the first constraint
85120
--> $DIR/suggest-move-types.rs:82:53
@@ -95,6 +130,11 @@ LL | struct Dl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<T, 'a, A=(), B=(), U, '
95130
| | | the constraints are provided here
96131
| | generic argument
97132
| generic argument
133+
|
134+
help: move the constraints after the generic arguments
135+
|
136+
LL | struct Dl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime<A=(), B=(), U, 'b, C=(), V, 'c, A=(), B=(), U, 'b, C=()>> {
137+
| -- ^^^^^^^^^^^^^^^^^^^^^^^
98138

99139
error[E0747]: type provided when a lifetime was expected
100140
--> $DIR/suggest-move-types.rs:33:43

0 commit comments

Comments
 (0)