From f221b394def7e739c6c2b936afd12e032ee0f827 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 8 Dec 2019 12:29:05 +0100 Subject: [PATCH] extract error_negative_bounds --- src/librustc_parse/parser/ty.rs | 62 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index db9a0ada525..3877f3f9a62 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -371,33 +371,7 @@ fn parse_generic_bounds_common( } if !negative_bounds.is_empty() { - let negative_bounds_len = negative_bounds.len(); - let last_span = *negative_bounds.last().unwrap(); - let mut err = self.struct_span_err( - negative_bounds, - "negative bounds are not supported", - ); - err.span_label(last_span, "negative bounds are not supported"); - if let Some(bound_list) = colon_span { - let bound_list = bound_list.to(self.prev_span); - let mut new_bound_list = String::new(); - if !bounds.is_empty() { - let mut snippets = bounds.iter().map(|bound| bound.span()) - .map(|span| self.span_to_snippet(span)); - while let Some(Ok(snippet)) = snippets.next() { - new_bound_list.push_str(" + "); - new_bound_list.push_str(&snippet); - } - new_bound_list = new_bound_list.replacen(" +", ":", 1); - } - err.span_suggestion_hidden( - bound_list, - &format!("remove the bound{}", pluralize!(negative_bounds_len)), - new_bound_list, - Applicability::MachineApplicable, - ); - } - err.emit(); + self.error_negative_bounds(colon_span, &bounds, negative_bounds); } Ok(bounds) @@ -414,6 +388,40 @@ fn can_begin_bound(&mut self) -> bool { || self.check(&token::OpenDelim(token::Paren)) } + fn error_negative_bounds( + &self, + colon_span: Option, + bounds: &[GenericBound], + negative_bounds: Vec, + ) { + let negative_bounds_len = negative_bounds.len(); + let last_span = *negative_bounds.last().unwrap(); + let mut err = self.struct_span_err( + negative_bounds, + "negative bounds are not supported", + ); + err.span_label(last_span, "negative bounds are not supported"); + if let Some(bound_list) = colon_span { + let bound_list = bound_list.to(self.prev_span); + let mut new_bound_list = String::new(); + if !bounds.is_empty() { + let mut snippets = bounds.iter().map(|bound| self.span_to_snippet(bound.span())); + while let Some(Ok(snippet)) = snippets.next() { + new_bound_list.push_str(" + "); + new_bound_list.push_str(&snippet); + } + new_bound_list = new_bound_list.replacen(" +", ":", 1); + } + err.span_suggestion_hidden( + bound_list, + &format!("remove the bound{}", pluralize!(negative_bounds_len)), + new_bound_list, + Applicability::MachineApplicable, + ); + } + err.emit(); + } + /// Parses a bound according to the grammar: /// ``` /// BOUND = TY_BOUND | LT_BOUND -- 2.44.0