Skip to content

Commit

Permalink
Only mention const generics if enabled.
Browse files Browse the repository at this point in the history
This commit updates the generic parameter re-ordering diagnostic to only
mention const generics if the feature is enabled.
  • Loading branch information
davidtwco committed Mar 30, 2019
1 parent 3829746 commit 0270d56
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 34 deletions.
57 changes: 37 additions & 20 deletions src/librustc_passes/ast_validation.rs
Expand Up @@ -360,6 +360,7 @@ enum GenericPosition {
}

fn validate_generics_order<'a>(
sess: &Session,
handler: &errors::Handler,
generics: impl Iterator<
Item = (
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/issue-59508-1.rs
@@ -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() {}
14 changes: 14 additions & 0 deletions src/test/ui/issue-59508-1.stderr
@@ -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

2 changes: 1 addition & 1 deletion src/test/ui/issue-59508.stderr
Expand Up @@ -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

8 changes: 4 additions & 4 deletions src/test/ui/lifetime-before-type-params.stderr
Expand Up @@ -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`
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-14303-enum.stderr
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-14303-fn-def.stderr
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-14303-impl.stderr
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-14303-struct.stderr
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-14303-trait.stderr
Expand Up @@ -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

8 changes: 4 additions & 4 deletions src/test/ui/suggestions/suggest-move-lifetimes.stderr
Expand Up @@ -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

0 comments on commit 0270d56

Please sign in to comment.