From 03620dba25328ee8cc7316cf6d9bad2d0a118ba1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 17 Jan 2017 01:54:59 +0300 Subject: [PATCH] Use resizable Vec instead of P<[T]> in AST --- src/librustc/hir/lowering.rs | 2 +- src/libsyntax/ast.rs | 10 +++---- src/libsyntax/ext/build.rs | 25 +++------------- src/libsyntax/fold.rs | 5 ++-- src/libsyntax/parse/mod.rs | 2 +- src/libsyntax/parse/parser.rs | 36 +++++++++++------------ src/libsyntax/print/pprust.rs | 4 +-- src/libsyntax_ext/deriving/generic/mod.rs | 9 +++--- src/libsyntax_ext/deriving/generic/ty.rs | 2 +- 9 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 9a0ef6537ce3a..ee6f91bd41493 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -596,7 +596,7 @@ impl<'a> LoweringContext<'a> { } } - fn lower_ty_params(&mut self, tps: &P<[TyParam]>, add_bounds: &NodeMap>) + fn lower_ty_params(&mut self, tps: &Vec, add_bounds: &NodeMap>) -> hir::HirVec { tps.iter().map(|tp| { self.lower_ty_param(tp, add_bounds.get(&tp.id).map_or(&[][..], |x| &x)) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 6c69aa14bcf96..2d7dfe50415d3 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -204,11 +204,11 @@ pub struct AngleBracketedParameterData { /// The lifetime parameters for this path segment. pub lifetimes: Vec, /// The type parameters for this path segment, if present. - pub types: P<[P]>, + pub types: Vec>, /// Bindings (equality constraints) on associated types, if present. /// /// E.g., `Foo`. - pub bindings: P<[TypeBinding]>, + pub bindings: Vec, } impl Into>> for AngleBracketedParameterData { @@ -297,7 +297,7 @@ pub enum TraitBoundModifier { Maybe, } -pub type TyParamBounds = P<[TyParamBound]>; +pub type TyParamBounds = Vec; #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct TyParam { @@ -314,7 +314,7 @@ pub struct TyParam { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct Generics { pub lifetimes: Vec, - pub ty_params: P<[TyParam]>, + pub ty_params: Vec, pub where_clause: WhereClause, pub span: Span, } @@ -344,7 +344,7 @@ impl Default for Generics { fn default() -> Generics { Generics { lifetimes: Vec::new(), - ty_params: P::new(), + ty_params: Vec::new(), where_clause: WhereClause { id: DUMMY_NODE_ID, predicates: Vec::new(), diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index fc3cbf20fb955..b234677f54477 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -67,9 +67,6 @@ pub trait AstBuilder { fn ty_option(&self, ty: P) -> P; fn ty_infer(&self, sp: Span) -> P; - fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec> ; - fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec> ; - fn typaram(&self, span: Span, id: ast::Ident, @@ -333,8 +330,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } else { Some(P(ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData { lifetimes: lifetimes, - types: P::from_vec(types), - bindings: P::from_vec(bindings), + types: types, + bindings: bindings, }))) }; segments.push(ast::PathSegment { identifier: last_identifier, parameters: parameters }); @@ -369,8 +366,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> { let mut path = trait_path; let parameters = ast::AngleBracketedParameterData { lifetimes: lifetimes, - types: P::from_vec(types), - bindings: P::from_vec(bindings), + types: types, + bindings: bindings, }; path.segments.push(ast::PathSegment { identifier: ident, @@ -458,20 +455,6 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } } - // these are strange, and probably shouldn't be used outside of - // pipes. Specifically, the global version possible generates - // incorrect code. - fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec> { - ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect() - } - - fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec> { - ty_params - .iter() - .map(|p| self.ty_path(self.path_global(DUMMY_SP, vec![p.ident]))) - .collect() - } - fn trait_ref(&self, path: ast::Path) -> ast::TraitRef { ast::TraitRef { path: path, diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 8b045f1b53002..2acf1c8f8fb49 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -225,7 +225,7 @@ pub trait Folder : Sized { noop_fold_ty_param(tp, self) } - fn fold_ty_params(&mut self, tps: P<[TyParam]>) -> P<[TyParam]> { + fn fold_ty_params(&mut self, tps: Vec) -> Vec { noop_fold_ty_params(tps, self) } @@ -674,8 +674,7 @@ pub fn noop_fold_ty_param(tp: TyParam, fld: &mut T) -> TyParam { } } -pub fn noop_fold_ty_params(tps: P<[TyParam]>, fld: &mut T) - -> P<[TyParam]> { +pub fn noop_fold_ty_params(tps: Vec, fld: &mut T) -> Vec { tps.move_map(|tp| fld.fold_ty_param(tp)) } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 32b61a88ac17f..1a1950dc45c61 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -847,7 +847,7 @@ mod tests { Abi::Rust, ast::Generics{ // no idea on either of these: lifetimes: Vec::new(), - ty_params: P::new(), + ty_params: Vec::new(), where_clause: ast::WhereClause { id: ast::DUMMY_NODE_ID, predicates: Vec::new(), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index bae08da0a640c..b5063528d0332 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -685,7 +685,7 @@ impl<'a> Parser<'a> { pub fn parse_seq_to_before_gt_or_return(&mut self, sep: Option, mut f: F) - -> PResult<'a, (P<[T]>, bool)> + -> PResult<'a, (Vec, bool)> where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option>, { let mut v = Vec::new(); @@ -706,7 +706,7 @@ impl<'a> Parser<'a> { if i % 2 == 0 { match f(self)? { Some(result) => v.push(result), - None => return Ok((P::from_vec(v), true)) + None => return Ok((v, true)) } } else { if let Some(t) = sep.as_ref() { @@ -715,7 +715,7 @@ impl<'a> Parser<'a> { } } - return Ok((P::from_vec(v), false)); + return Ok((v, false)); } /// Parse a sequence bracketed by '<' and '>', stopping @@ -723,7 +723,7 @@ impl<'a> Parser<'a> { pub fn parse_seq_to_before_gt(&mut self, sep: Option, mut f: F) - -> PResult<'a, P<[T]>> where + -> PResult<'a, Vec> where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, { let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, @@ -735,7 +735,7 @@ impl<'a> Parser<'a> { pub fn parse_seq_to_gt(&mut self, sep: Option, f: F) - -> PResult<'a, P<[T]>> where + -> PResult<'a, Vec> where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, { let v = self.parse_seq_to_before_gt(sep, f)?; @@ -746,7 +746,7 @@ impl<'a> Parser<'a> { pub fn parse_seq_to_gt_or_return(&mut self, sep: Option, f: F) - -> PResult<'a, (P<[T]>, bool)> where + -> PResult<'a, (Vec, bool)> where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option>, { let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?; @@ -1039,11 +1039,11 @@ impl<'a> Parser<'a> { let other_bounds = if self.eat(&token::BinOp(token::Plus)) { self.parse_ty_param_bounds()? } else { - P::new() + Vec::new() }; let all_bounds = Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter() - .chain(other_bounds.into_vec()) + .chain(other_bounds) .collect(); Ok(ast::TyKind::ObjectSum(all_bounds)) } @@ -1267,7 +1267,7 @@ impl<'a> Parser<'a> { return Ok(lhs); } - let mut bounds = self.parse_ty_param_bounds()?.into_vec(); + let mut bounds = self.parse_ty_param_bounds()?; // In type grammar, `+` is treated like a binary operator, // and hence both L and R side are required. @@ -1327,7 +1327,7 @@ impl<'a> Parser<'a> { } let sp = mk_sp(lo, self.prev_span.hi); - let sum = TyKind::ObjectSum(bounds.into()); + let sum = TyKind::ObjectSum(bounds); Ok(P(Ty {id: ast::DUMMY_NODE_ID, node: sum, span: sp})) } @@ -1759,8 +1759,8 @@ impl<'a> Parser<'a> { let (lifetimes, types, bindings) = self.parse_generic_values_after_lt()?; ast::AngleBracketedParameterData { lifetimes: lifetimes, - types: P::from_vec(types), - bindings: P::from_vec(bindings), + types: types, + bindings: bindings, }.into() } else if self.eat(&token::OpenDelim(token::Paren)) { let lo = self.prev_span.lo; @@ -1819,8 +1819,8 @@ impl<'a> Parser<'a> { identifier: identifier, parameters: ast::AngleBracketedParameterData { lifetimes: lifetimes, - types: P::from_vec(types), - bindings: P::from_vec(bindings), + types: types, + bindings: bindings, }.into(), }); @@ -4192,7 +4192,7 @@ impl<'a> Parser<'a> { fn parse_colon_then_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds> { if !self.eat(&token::Colon) { - Ok(P::new()) + Ok(Vec::new()) } else { self.parse_ty_param_bounds() } @@ -4238,7 +4238,7 @@ impl<'a> Parser<'a> { } } - return Ok(P::from_vec(result)); + return Ok(result); } /// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )? @@ -4375,7 +4375,7 @@ impl<'a> Parser<'a> { // If we found the `>`, don't continue. if !returned { - return Ok((lifetimes, types.into_vec(), Vec::new())); + return Ok((lifetimes, types, Vec::new())); } // Then parse type bindings. @@ -4396,7 +4396,7 @@ impl<'a> Parser<'a> { }); } )?; - Ok((lifetimes, types.into_vec(), bindings.into_vec())) + Ok((lifetimes, types, bindings)) } fn forbid_lifetime(&mut self) -> PResult<'a, ()> { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 31e6f25559dec..c989aa59e8717 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1009,7 +1009,7 @@ impl<'a> State<'a> { ast::TyKind::BareFn(ref f) => { let generics = ast::Generics { lifetimes: f.lifetimes.clone(), - ty_params: P::new(), + ty_params: Vec::new(), where_clause: ast::WhereClause { id: ast::DUMMY_NODE_ID, predicates: Vec::new(), @@ -2973,7 +2973,7 @@ impl<'a> State<'a> { } let generics = ast::Generics { lifetimes: Vec::new(), - ty_params: P::new(), + ty_params: Vec::new(), where_clause: ast::WhereClause { id: ast::DUMMY_NODE_ID, predicates: Vec::new(), diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index b99b10193681f..ce64aef516f49 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -507,9 +507,8 @@ impl<'a> TraitDef<'a> { } }); - let Generics { mut lifetimes, ty_params, mut where_clause, span } = self.generics + let Generics { mut lifetimes, mut ty_params, mut where_clause, span } = self.generics .to_generics(cx, self.span, type_ident, generics); - let mut ty_params = ty_params.into_vec(); // Copy the lifetimes lifetimes.extend(generics.lifetimes.iter().cloned()); @@ -533,7 +532,7 @@ impl<'a> TraitDef<'a> { bounds.push((*declared_bound).clone()); } - cx.typaram(self.span, ty_param.ident, vec![], P::from_vec(bounds), None) + cx.typaram(self.span, ty_param.ident, vec![], bounds, None) })); // and similarly for where clauses @@ -596,7 +595,7 @@ impl<'a> TraitDef<'a> { span: self.span, bound_lifetimes: vec![], bounded_ty: ty, - bounds: P::from_vec(bounds), + bounds: bounds, }; let predicate = ast::WherePredicate::BoundPredicate(predicate); @@ -607,7 +606,7 @@ impl<'a> TraitDef<'a> { let trait_generics = Generics { lifetimes: lifetimes, - ty_params: P::from_vec(ty_params), + ty_params: ty_params, where_clause: where_clause, span: span, }; diff --git a/src/libsyntax_ext/deriving/generic/ty.rs b/src/libsyntax_ext/deriving/generic/ty.rs index 4749d082bc0ec..cfd52381538cb 100644 --- a/src/libsyntax_ext/deriving/generic/ty.rs +++ b/src/libsyntax_ext/deriving/generic/ty.rs @@ -212,7 +212,7 @@ fn mk_generics(lifetimes: Vec, ty_params: Vec, s -> Generics { Generics { lifetimes: lifetimes, - ty_params: P::from_vec(ty_params), + ty_params: ty_params, where_clause: ast::WhereClause { id: ast::DUMMY_NODE_ID, predicates: Vec::new(),