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.
This commit is contained in:
David Wood 2019-03-30 20:57:04 +01:00
parent 3829746ef9
commit 0270d565d9
No known key found for this signature in database
GPG Key ID: 01760B4F9F53F154
11 changed files with 83 additions and 34 deletions

View File

@ -360,6 +360,7 @@ enum GenericPosition {
}
fn validate_generics_order<'a>(
sess: &Session,
handler: &errors::Handler,
generics: impl Iterator<
Item = (
@ -426,7 +427,11 @@ fn validate_generics_order<'a>(
if let GenericPosition::Param = pos {
err.span_suggestion(
span,
&format!("reorder the {}s: lifetimes, then types, then consts", pos_str),
&format!(
"reorder the {}s: lifetimes, then types{}",
pos_str,
if sess.features_untracked().const_generics { ", then consts" } else { "" },
),
ordered_params.clone(),
Applicability::MachineApplicable,
);
@ -709,13 +714,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
match *generic_args {
GenericArgs::AngleBracketed(ref data) => {
walk_list!(self, visit_generic_arg, &data.args);
validate_generics_order(self.err_handler(), data.args.iter().map(|arg| {
(match arg {
GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
GenericArg::Type(..) => ParamKindOrd::Type,
GenericArg::Const(..) => ParamKindOrd::Const,
}, None, arg.span(), None)
}), GenericPosition::Arg, generic_args.span());
validate_generics_order(
self.session,
self.err_handler(),
data.args.iter().map(|arg| {
(match arg {
GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
GenericArg::Type(..) => ParamKindOrd::Type,
GenericArg::Const(..) => ParamKindOrd::Const,
}, None, arg.span(), None)
}),
GenericPosition::Arg,
generic_args.span(),
);
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
// are allowed to contain nested `impl Trait`.
@ -748,18 +759,24 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
}
validate_generics_order(self.err_handler(), generics.params.iter().map(|param| {
let ident = Some(param.ident.to_string());
let (kind, ident) = match &param.kind {
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
GenericParamKind::Const { ref ty } => {
let ty = pprust::ty_to_string(ty);
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
}
};
(kind, Some(&*param.bounds), param.ident.span, ident)
}), GenericPosition::Param, generics.span);
validate_generics_order(
self.session,
self.err_handler(),
generics.params.iter().map(|param| {
let ident = Some(param.ident.to_string());
let (kind, ident) = match &param.kind {
GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
GenericParamKind::Const { ref ty } => {
let ty = pprust::ty_to_string(ty);
(ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
}
};
(kind, Some(&*param.bounds), param.ident.span, ident)
}),
GenericPosition::Param,
generics.span,
);
for predicate in &generics.where_clause.predicates {
if let WherePredicate::EqPredicate(ref predicate) = *predicate {

View File

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

View File

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

View File

@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/issue-59508.rs:10:25
|
LL | pub fn do_things<T, 'a, 'b: 'a>() {
| ----^^--^^----- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b: 'a, T>`
| ----^^--^^----- help: reorder the parameters: lifetimes, then types: `<'a, 'b: 'a, T>`
error: aborting due to previous error

View File

@ -2,25 +2,25 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/lifetime-before-type-params.rs:2:13
|
LL | fn first<T, 'a, 'b>() {}
| ----^^--^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| ----^^--^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: lifetime parameters must be declared prior to type parameters
--> $DIR/lifetime-before-type-params.rs:4:18
|
LL | fn second<'a, T, 'b>() {}
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: lifetime parameters must be declared prior to type parameters
--> $DIR/lifetime-before-type-params.rs:6:16
|
LL | fn third<T, U, 'a>() {}
| -------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, U>`
| -------^^- help: reorder the parameters: lifetimes, then types: `<'a, T, U>`
error: lifetime parameters must be declared prior to type parameters
--> $DIR/lifetime-before-type-params.rs:8:18
|
LL | fn fourth<'a, T, 'b, U, 'c, V>() {}
| --------^^-----^^---- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, 'c, T, U, V>`
| --------^^-----^^---- help: reorder the parameters: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`
error[E0601]: `main` function not found in crate `lifetime_before_type_params`
|

View File

@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/issue-14303-enum.rs:1:15
|
LL | enum X<'a, T, 'b> {
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: aborting due to previous error

View File

@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/issue-14303-fn-def.rs:1:15
|
LL | fn foo<'a, T, 'b>(x: &'a T) {}
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: aborting due to previous error

View File

@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/issue-14303-impl.rs:3:13
|
LL | impl<'a, T, 'b> X<T> {}
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: aborting due to previous error

View File

@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/issue-14303-struct.rs:1:17
|
LL | struct X<'a, T, 'b> {
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: aborting due to previous error

View File

@ -2,7 +2,7 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/issue-14303-trait.rs:1:18
|
LL | trait Foo<'a, T, 'b> {}
| --------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, T>`
| --------^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, T>`
error: aborting due to previous error

View File

@ -2,25 +2,25 @@ error: lifetime parameters must be declared prior to type parameters
--> $DIR/suggest-move-lifetimes.rs:1:13
|
LL | struct A<T, 'a> {
| ----^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T>`
| ----^^- help: reorder the parameters: lifetimes, then types: `<'a, T>`
error: lifetime parameters must be declared prior to type parameters
--> $DIR/suggest-move-lifetimes.rs:5:13
|
LL | struct B<T, 'a, U> {
| ----^^---- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, U>`
| ----^^---- help: reorder the parameters: lifetimes, then types: `<'a, T, U>`
error: lifetime parameters must be declared prior to type parameters
--> $DIR/suggest-move-lifetimes.rs:10:16
|
LL | struct C<T, U, 'a> {
| -------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, U>`
| -------^^- help: reorder the parameters: lifetimes, then types: `<'a, T, U>`
error: lifetime parameters must be declared prior to type parameters
--> $DIR/suggest-move-lifetimes.rs:15:16
|
LL | struct D<T, U, 'a, 'b, V, 'c> {
| -------^^--^^-----^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, 'b, 'c, T, U, V>`
| -------^^--^^-----^^- help: reorder the parameters: lifetimes, then types: `<'a, 'b, 'c, T, U, V>`
error: aborting due to 4 previous errors