Skip to content

Commit 0270d56

Browse files
committed
Only mention const generics if enabled.
This commit updates the generic parameter re-ordering diagnostic to only mention const generics if the feature is enabled.
1 parent 3829746 commit 0270d56

11 files changed

+83
-34
lines changed

src/librustc_passes/ast_validation.rs

+37-20
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ enum GenericPosition {
360360
}
361361

362362
fn validate_generics_order<'a>(
363+
sess: &Session,
363364
handler: &errors::Handler,
364365
generics: impl Iterator<
365366
Item = (
@@ -426,7 +427,11 @@ fn validate_generics_order<'a>(
426427
if let GenericPosition::Param = pos {
427428
err.span_suggestion(
428429
span,
429-
&format!("reorder the {}s: lifetimes, then types, then consts", pos_str),
430+
&format!(
431+
"reorder the {}s: lifetimes, then types{}",
432+
pos_str,
433+
if sess.features_untracked().const_generics { ", then consts" } else { "" },
434+
),
430435
ordered_params.clone(),
431436
Applicability::MachineApplicable,
432437
);
@@ -709,13 +714,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
709714
match *generic_args {
710715
GenericArgs::AngleBracketed(ref data) => {
711716
walk_list!(self, visit_generic_arg, &data.args);
712-
validate_generics_order(self.err_handler(), data.args.iter().map(|arg| {
713-
(match arg {
714-
GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
715-
GenericArg::Type(..) => ParamKindOrd::Type,
716-
GenericArg::Const(..) => ParamKindOrd::Const,
717-
}, None, arg.span(), None)
718-
}), GenericPosition::Arg, generic_args.span());
717+
validate_generics_order(
718+
self.session,
719+
self.err_handler(),
720+
data.args.iter().map(|arg| {
721+
(match arg {
722+
GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
723+
GenericArg::Type(..) => ParamKindOrd::Type,
724+
GenericArg::Const(..) => ParamKindOrd::Const,
725+
}, None, arg.span(), None)
726+
}),
727+
GenericPosition::Arg,
728+
generic_args.span(),
729+
);
719730

720731
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
721732
// are allowed to contain nested `impl Trait`.
@@ -748,18 +759,24 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
748759
}
749760
}
750761

751-
validate_generics_order(self.err_handler(), generics.params.iter().map(|param| {
752-
let ident = Some(param.ident.to_string());
753-
let (kind, ident) = match &param.kind {
754-
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
755-
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
756-
GenericParamKind::Const { ref ty } => {
757-
let ty = pprust::ty_to_string(ty);
758-
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
759-
}
760-
};
761-
(kind, Some(&*param.bounds), param.ident.span, ident)
762-
}), GenericPosition::Param, generics.span);
762+
validate_generics_order(
763+
self.session,
764+
self.err_handler(),
765+
generics.params.iter().map(|param| {
766+
let ident = Some(param.ident.to_string());
767+
let (kind, ident) = match &param.kind {
768+
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
769+
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
770+
GenericParamKind::Const { ref ty } => {
771+
let ty = pprust::ty_to_string(ty);
772+
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
773+
}
774+
};
775+
(kind, Some(&*param.bounds), param.ident.span, ident)
776+
}),
777+
GenericPosition::Param,
778+
generics.span,
779+
);
763780

764781
for predicate in &generics.where_clause.predicates {
765782
if let WherePredicate::EqPredicate(ref predicate) = *predicate {

src/test/ui/issue-59508-1.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(dead_code)]
2+
#![feature(const_generics)]
3+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
4+
5+
// This test checks that generic parameter re-ordering diagnostic suggestions mention that
6+
// consts come after types and lifetimes when the `const_generics` feature is enabled.
7+
// We cannot run rustfix on this test because of the above const generics warning.
8+
9+
struct A;
10+
11+
impl A {
12+
pub fn do_things<T, 'a, 'b: 'a>() {
13+
//~^ ERROR lifetime parameters must be declared prior to type parameters
14+
println!("panic");
15+
}
16+
}
17+
18+
fn main() {}

src/test/ui/issue-59508-1.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/issue-59508-1.rs:2:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
7+
error: lifetime parameters must be declared prior to type parameters
8+
--> $DIR/issue-59508-1.rs:12:25
9+
|
10+
LL | pub fn do_things<T, 'a, 'b: 'a>() {
11+
| ----^^--^^----- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b: 'a, T>`
12+
13+
error: aborting due to previous error
14+

src/test/ui/issue-59508.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/issue-59508.rs:10:25
33
|
44
LL | pub fn do_things<T, 'a, 'b: 'a>() {
5-
| ----^^--^^----- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b: 'a, T>`
5+
| ----^^--^^----- help: reorder the parameters: lifetimes, then types: `<'a, 'b: 'a, T>`
66

77
error: aborting due to previous error
88

src/test/ui/lifetime-before-type-params.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/lifetime-before-type-params.rs:2:13
33
|
44
LL | fn first<T, 'a, 'b>() {}
5-
| ----^^--^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
5+
| ----^^--^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
66

77
error: lifetime parameters must be declared prior to type parameters
88
--> $DIR/lifetime-before-type-params.rs:4:18
99
|
1010
LL | fn second<'a, T, 'b>() {}
11-
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
11+
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
1212

1313
error: lifetime parameters must be declared prior to type parameters
1414
--> $DIR/lifetime-before-type-params.rs:6:16
1515
|
1616
LL | fn third<T, U, 'a>() {}
17-
| -------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, U>`
17+
| -------^^- help: reorder the parameters: lifetimes, then types: `<'a, T, U>`
1818

1919
error: lifetime parameters must be declared prior to type parameters
2020
--> $DIR/lifetime-before-type-params.rs:8:18
2121
|
2222
LL | fn fourth<'a, T, 'b, U, 'c, V>() {}
23-
| --------^^-----^^---- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, 'c, T, U, V>`
23+
| --------^^-----^^---- help: reorder the parameters: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`
2424

2525
error[E0601]: `main` function not found in crate `lifetime_before_type_params`
2626
|

src/test/ui/parser/issue-14303-enum.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/issue-14303-enum.rs:1:15
33
|
44
LL | enum X<'a, T, 'b> {
5-
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
5+
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
66

77
error: aborting due to previous error
88

src/test/ui/parser/issue-14303-fn-def.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/issue-14303-fn-def.rs:1:15
33
|
44
LL | fn foo<'a, T, 'b>(x: &'a T) {}
5-
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
5+
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
66

77
error: aborting due to previous error
88

src/test/ui/parser/issue-14303-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/issue-14303-impl.rs:3:13
33
|
44
LL | impl<'a, T, 'b> X<T> {}
5-
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
5+
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
66

77
error: aborting due to previous error
88

src/test/ui/parser/issue-14303-struct.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/issue-14303-struct.rs:1:17
33
|
44
LL | struct X<'a, T, 'b> {
5-
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
5+
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
66

77
error: aborting due to previous error
88

src/test/ui/parser/issue-14303-trait.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/issue-14303-trait.rs:1:18
33
|
44
LL | trait Foo<'a, T, 'b> {}
5-
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
5+
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
66

77
error: aborting due to previous error
88

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ error: lifetime parameters must be declared prior to type parameters
22
--> $DIR/suggest-move-lifetimes.rs:1:13
33
|
44
LL | struct A<T, 'a> {
5-
| ----^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T>`
5+
| ----^^- help: reorder the parameters: lifetimes, then types: `<'a, T>`
66

77
error: lifetime parameters must be declared prior to type parameters
88
--> $DIR/suggest-move-lifetimes.rs:5:13
99
|
1010
LL | struct B<T, 'a, U> {
11-
| ----^^---- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, U>`
11+
| ----^^---- help: reorder the parameters: lifetimes, then types: `<'a, T, U>`
1212

1313
error: lifetime parameters must be declared prior to type parameters
1414
--> $DIR/suggest-move-lifetimes.rs:10:16
1515
|
1616
LL | struct C<T, U, 'a> {
17-
| -------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, U>`
17+
| -------^^- help: reorder the parameters: lifetimes, then types: `<'a, T, U>`
1818

1919
error: lifetime parameters must be declared prior to type parameters
2020
--> $DIR/suggest-move-lifetimes.rs:15:16
2121
|
2222
LL | struct D<T, U, 'a, 'b, V, 'c> {
23-
| -------^^--^^-----^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, 'c, T, U, V>`
23+
| -------^^--^^-----^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`
2424

2525
error: aborting due to 4 previous errors
2626

0 commit comments

Comments
 (0)