diff --git a/Cargo.lock b/Cargo.lock index a592c82225e..14c26301807 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3537,6 +3537,7 @@ dependencies = [ name = "rustc_ast_passes" version = "0.0.0" dependencies = [ + "itertools 0.8.0", "log", "rustc_ast", "rustc_ast_pretty", diff --git a/src/librustc_ast_passes/Cargo.toml b/src/librustc_ast_passes/Cargo.toml index 5d096e4965d..e4d1d79abb2 100644 --- a/src/librustc_ast_passes/Cargo.toml +++ b/src/librustc_ast_passes/Cargo.toml @@ -9,6 +9,7 @@ name = "rustc_ast_passes" path = "lib.rs" [dependencies] +itertools = "0.8" log = "0.4" rustc_ast_pretty = { path = "../librustc_ast_pretty" } rustc_attr = { path = "../librustc_attr" } diff --git a/src/librustc_ast_passes/ast_validation.rs b/src/librustc_ast_passes/ast_validation.rs index aaf0212ca02..82ba52a3f14 100644 --- a/src/librustc_ast_passes/ast_validation.rs +++ b/src/librustc_ast_passes/ast_validation.rs @@ -6,6 +6,7 @@ // This pass is supposed to perform only simple checks not requiring name resolution // or type checking or some other kind of complex analysis. +use itertools::{Either, Itertools}; use rustc_ast::ast::*; use rustc_ast::attr; use rustc_ast::expand::is_proc_macro_attr; @@ -640,7 +641,7 @@ fn deny_items(&self, trait_items: &[P], ident_span: Span) { } } - fn suggest_correct_generic_order(&self, data: &AngleBracketedArgs) -> String { + fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String { // Lifetimes always come first. let lt_sugg = data.args.iter().filter_map(|arg| match arg { AngleBracketedArg::Arg(lt @ GenericArg::Lifetime(_)) => { @@ -649,11 +650,12 @@ fn suggest_correct_generic_order(&self, data: &AngleBracketedArgs) -> String { _ => None, }); let args_sugg = data.args.iter().filter_map(|a| match a { - AngleBracketedArg::Arg(GenericArg::Lifetime(_)) => None, + AngleBracketedArg::Arg(GenericArg::Lifetime(_)) | AngleBracketedArg::Constraint(_) => { + None + } AngleBracketedArg::Arg(arg) => Some(pprust::to_string(|s| s.print_generic_arg(arg))), - AngleBracketedArg::Constraint(_) => None, }); - // Cosntraints always come last. + // Constraints always come last. let constraint_sugg = data.args.iter().filter_map(|a| match a { AngleBracketedArg::Arg(_) => None, AngleBracketedArg::Constraint(c) => { @@ -673,22 +675,11 @@ fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) { return; } // Find all generic argument coming after the first constraint... - let constraint_spans = data - .args - .iter() - .filter_map(|arg| match arg { - AngleBracketedArg::Constraint(c) => Some(c.span), - _ => None, - }) - .collect::>(); - let arg_spans = data - .args - .iter() - .filter_map(|arg| match arg { - AngleBracketedArg::Arg(a) => Some(a.span()), - _ => None, - }) - .collect::>(); + let (constraint_spans, arg_spans): (Vec, Vec) = + data.args.iter().partition_map(|arg| match arg { + AngleBracketedArg::Constraint(c) => Either::Left(c.span), + AngleBracketedArg::Arg(a) => Either::Right(a.span()), + }); let args_len = arg_spans.len(); let constraint_len = constraint_spans.len(); // ...and then error: @@ -706,7 +697,7 @@ fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) { pluralize!(constraint_len), pluralize!(args_len) ), - self.suggest_correct_generic_order(&data), + self.correct_generic_order_suggestion(&data), Applicability::MachineApplicable, ) .emit();