From 2ddc35997cd7147f007943e50ef5f942d20ad7c7 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 27 Mar 2020 07:39:10 +0100 Subject: [PATCH] address some review comments --- src/librustc_ast_lowering/path.rs | 6 +- src/librustc_ast_passes/ast_validation.rs | 6 +- src/librustc_parse/parser/path.rs | 14 +-- src/test/ui/parser/issue-32214.rs | 2 +- src/test/ui/parser/issue-32214.stderr | 9 +- .../recover-assoc-const-constraint.stderr | 8 +- .../parser/recover-assoc-eq-missing-term.rs | 2 +- .../recover-assoc-eq-missing-term.stderr | 12 +-- src/test/ui/suggestions/suggest-move-types.rs | 16 ++-- .../ui/suggestions/suggest-move-types.stderr | 88 +++++++------------ 10 files changed, 68 insertions(+), 95 deletions(-) diff --git a/src/librustc_ast_lowering/path.rs b/src/librustc_ast_lowering/path.rs index f15a4555e5f..dde73475651 100644 --- a/src/librustc_ast_lowering/path.rs +++ b/src/librustc_ast_lowering/path.rs @@ -367,9 +367,9 @@ fn lower_angle_bracketed_parameter_data( mut itctx: ImplTraitContext<'_, 'hir>, ) -> (GenericArgsCtor<'hir>, bool) { let has_non_lt_args = data.args.iter().any(|arg| match arg { - AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_)) => false, - AngleBracketedArg::Arg(ast::GenericArg::Type(_) | ast::GenericArg::Const(_)) - | AngleBracketedArg::Constraint(_) => true, + AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_)) + | AngleBracketedArg::Constraint(_) => false, + AngleBracketedArg::Arg(ast::GenericArg::Type(_) | ast::GenericArg::Const(_)) => true, }); let args = data .args diff --git a/src/librustc_ast_passes/ast_validation.rs b/src/librustc_ast_passes/ast_validation.rs index e6f09a3a6cb..592d3bf2e05 100644 --- a/src/librustc_ast_passes/ast_validation.rs +++ b/src/librustc_ast_passes/ast_validation.rs @@ -660,12 +660,8 @@ fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) { // ...and then error: self.err_handler() .struct_span_err( - data.span, - "constraints in a path segment must come after generic arguments", - ) - .span_labels( misplaced_args, - "this generic argument must come before the first constraint", + "generic arguments must come before the first constraint", ) .span_label(first.unwrap(), "the first constraint is provided here") .emit(); diff --git a/src/librustc_parse/parser/path.rs b/src/librustc_parse/parser/path.rs index d23adf4ffe3..9fa7bc027b8 100644 --- a/src/librustc_parse/parser/path.rs +++ b/src/librustc_parse/parser/path.rs @@ -439,8 +439,8 @@ fn parse_assoc_equality_term(&mut self, ident: Ident, eq: Span) -> PResult<'a, P Some(GenericArg::Type(ty)) => return Ok(ty), Some(GenericArg::Const(expr)) => { self.struct_span_err(span, "cannot constrain an associated constant to a value") - .span_label(ident.span, "the value constrains this associated constant") - .span_label(expr.value.span, "the value is given in this expression") + .span_label(ident.span, "this associated constant...") + .span_label(expr.value.span, "...cannot be constrained to this value") .emit(); } Some(GenericArg::Lifetime(lt)) => { @@ -450,15 +450,17 @@ fn parse_assoc_equality_term(&mut self, ident: Ident, eq: Span) -> PResult<'a, P .emit(); } None => { - self.struct_span_err(span, "missing type to the right of `=`") + let after_eq = eq.shrink_to_hi(); + let before_next = self.token.span.shrink_to_lo(); + self.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`") .span_suggestion( - span, + self.sess.source_map().next_point(eq).to(before_next), "to constrain the associated type, add a type after `=`", - format!("{} = TheType", ident), + " TheType".to_string(), Applicability::HasPlaceholders, ) .span_suggestion( - eq, + eq.to(before_next), &format!("remove the `=` if `{}` is a type", ident), String::new(), Applicability::MaybeIncorrect, diff --git a/src/test/ui/parser/issue-32214.rs b/src/test/ui/parser/issue-32214.rs index ca30f5f1329..1379eeb58e6 100644 --- a/src/test/ui/parser/issue-32214.rs +++ b/src/test/ui/parser/issue-32214.rs @@ -1,6 +1,6 @@ trait Trait { type Item; } pub fn test >() {} -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint fn main() { } diff --git a/src/test/ui/parser/issue-32214.stderr b/src/test/ui/parser/issue-32214.stderr index ee99fe70811..d25d3a09633 100644 --- a/src/test/ui/parser/issue-32214.stderr +++ b/src/test/ui/parser/issue-32214.stderr @@ -1,10 +1,9 @@ -error: constraints in a path segment must come after generic arguments - --> $DIR/issue-32214.rs:3:24 +error: generic arguments must come before the first constraint + --> $DIR/issue-32214.rs:3:34 | LL | pub fn test >() {} - | ^-------^^-^ - | | | - | | this generic argument must come before the first constraint + | ------- ^ + | | | the first constraint is provided here error: aborting due to previous error diff --git a/src/test/ui/parser/recover-assoc-const-constraint.stderr b/src/test/ui/parser/recover-assoc-const-constraint.stderr index bf617207936..c6733b33faa 100644 --- a/src/test/ui/parser/recover-assoc-const-constraint.stderr +++ b/src/test/ui/parser/recover-assoc-const-constraint.stderr @@ -4,8 +4,8 @@ error: cannot constrain an associated constant to a value LL | bar::(); | ----^^^-- | | | - | | the value is given in this expression - | the value constrains this associated constant + | | ...cannot be constrained to this value + | this associated constant... error: cannot constrain an associated constant to a value --> $DIR/recover-assoc-const-constraint.rs:4:11 @@ -13,8 +13,8 @@ error: cannot constrain an associated constant to a value LL | bar::(); | ----^^^------ | | | - | | the value is given in this expression - | the value constrains this associated constant + | | ...cannot be constrained to this value + | this associated constant... error: aborting due to 2 previous errors diff --git a/src/test/ui/parser/recover-assoc-eq-missing-term.rs b/src/test/ui/parser/recover-assoc-eq-missing-term.rs index d800df8236b..4b42c44dc64 100644 --- a/src/test/ui/parser/recover-assoc-eq-missing-term.rs +++ b/src/test/ui/parser/recover-assoc-eq-missing-term.rs @@ -1,6 +1,6 @@ #[cfg(FALSE)] fn syntax() { - bar::(); //~ ERROR missing type to the right of `=` + bar::(); //~ ERROR missing type to the right of `=` } fn main() {} diff --git a/src/test/ui/parser/recover-assoc-eq-missing-term.stderr b/src/test/ui/parser/recover-assoc-eq-missing-term.stderr index 5eb5d6879e9..6e41e139220 100644 --- a/src/test/ui/parser/recover-assoc-eq-missing-term.stderr +++ b/src/test/ui/parser/recover-assoc-eq-missing-term.stderr @@ -1,16 +1,16 @@ error: missing type to the right of `=` - --> $DIR/recover-assoc-eq-missing-term.rs:3:11 + --> $DIR/recover-assoc-eq-missing-term.rs:3:17 | -LL | bar::(); - | ^^^^^^ +LL | bar::(); + | ^^^ | help: to constrain the associated type, add a type after `=` | -LL | bar::(); - | ^^^^^^^^^^^^^^ +LL | bar::(); + | ^^^^^^^ help: remove the `=` if `Item` is a type | -LL | bar::(); +LL | bar::(); | -- error: aborting due to previous error diff --git a/src/test/ui/suggestions/suggest-move-types.rs b/src/test/ui/suggestions/suggest-move-types.rs index d9d5351b5c2..27930626a62 100644 --- a/src/test/ui/suggestions/suggest-move-types.rs +++ b/src/test/ui/suggestions/suggest-move-types.rs @@ -24,21 +24,21 @@ trait ThreeWithLifetime<'a, 'b, 'c, T, U, V> { } struct A> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint m: M, t: T, } struct Al<'a, T, M: OneWithLifetime> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint //~^^ ERROR type provided when a lifetime was expected m: M, t: &'a T, } struct B> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint m: M, t: T, u: U, @@ -46,7 +46,7 @@ struct B> { } struct Bl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint //~^^ ERROR type provided when a lifetime was expected m: M, t: &'a T, @@ -55,7 +55,7 @@ struct Bl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint m: M, t: T, u: U, @@ -63,7 +63,7 @@ struct C> { } struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint //~^^ ERROR lifetime provided when a type was expected m: M, t: &'a T, @@ -72,7 +72,7 @@ struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint m: M, t: T, u: U, @@ -80,7 +80,7 @@ struct D> { } struct Dl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { -//~^ ERROR constraints in a path segment must come after generic arguments +//~^ ERROR generic arguments must come before the first constraint //~^^ ERROR lifetime provided when a type was expected m: M, t: &'a T, diff --git a/src/test/ui/suggestions/suggest-move-types.stderr b/src/test/ui/suggestions/suggest-move-types.stderr index 0225546b9e3..a0a8c3fc3ba 100644 --- a/src/test/ui/suggestions/suggest-move-types.stderr +++ b/src/test/ui/suggestions/suggest-move-types.stderr @@ -1,89 +1,65 @@ -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:26:19 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:26:26 | LL | struct A> { - | ^----^^-^ - | | | - | | this generic argument must come before the first constraint + | ---- ^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:33:36 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:33:43 | LL | struct Al<'a, T, M: OneWithLifetime> { - | ^----^^-^^--^ - | | | | - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:40:27 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:40:46 | LL | struct B> { - | ^----^^^^^^^^^^^^^^-^^-^^-^ - | | | | | - | | | | this generic argument must come before the first constraint - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^ ^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:48:52 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:48:71 | LL | struct Bl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { - | ^----^^^^^^^^^^^^^^-^^-^^-^^--^^--^^--^ - | | | | | | | | - | | | | | | | this generic argument must come before the first constraint - | | | | | | this generic argument must come before the first constraint - | | | | | this generic argument must come before the first constraint - | | | | this generic argument must come before the first constraint - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^ ^ ^^ ^^ ^^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:57:27 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:57:49 | LL | struct C> { - | ^^^^----^^^^^^^^^^^^^^-^^-^ - | | | | - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:65:52 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:65:78 | LL | struct Cl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { - | ^^^^^^^^----^^^^^^^^^^^^^^-^^--^^-^^--^ - | | | | | | - | | | | | this generic argument must come before the first constraint - | | | | this generic argument must come before the first constraint - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^^ ^ ^^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:74:27 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:74:43 | LL | struct D> { - | ^^^^----^^^^^^^^-^^^^^^^^-^ - | | | | - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^ + | | | the first constraint is provided here -error: constraints in a path segment must come after generic arguments - --> $DIR/suggest-move-types.rs:82:52 +error: generic arguments must come before the first constraint + --> $DIR/suggest-move-types.rs:82:72 | LL | struct Dl<'a, 'b, 'c, T, U, V, M: ThreeWithLifetime> { - | ^^^^^^^^----^^^^^^^^-^^--^^^^^^^^-^^--^ - | | | | | | - | | | | | this generic argument must come before the first constraint - | | | | this generic argument must come before the first constraint - | | | this generic argument must come before the first constraint - | | this generic argument must come before the first constraint + | ---- ^ ^^ ^ ^^ + | | | the first constraint is provided here error[E0747]: type provided when a lifetime was expected