1
Fork 0

Use resizable Vec instead of P<[T]> in AST

This commit is contained in:
Vadim Petrochenkov 2017-01-17 01:54:59 +03:00
parent 2efe865d22
commit 03620dba25
9 changed files with 38 additions and 57 deletions

View file

@ -596,7 +596,7 @@ impl<'a> LoweringContext<'a> {
} }
} }
fn lower_ty_params(&mut self, tps: &P<[TyParam]>, add_bounds: &NodeMap<Vec<TyParamBound>>) fn lower_ty_params(&mut self, tps: &Vec<TyParam>, add_bounds: &NodeMap<Vec<TyParamBound>>)
-> hir::HirVec<hir::TyParam> { -> hir::HirVec<hir::TyParam> {
tps.iter().map(|tp| { tps.iter().map(|tp| {
self.lower_ty_param(tp, add_bounds.get(&tp.id).map_or(&[][..], |x| &x)) self.lower_ty_param(tp, add_bounds.get(&tp.id).map_or(&[][..], |x| &x))

View file

@ -204,11 +204,11 @@ pub struct AngleBracketedParameterData {
/// The lifetime parameters for this path segment. /// The lifetime parameters for this path segment.
pub lifetimes: Vec<Lifetime>, pub lifetimes: Vec<Lifetime>,
/// The type parameters for this path segment, if present. /// The type parameters for this path segment, if present.
pub types: P<[P<Ty>]>, pub types: Vec<P<Ty>>,
/// Bindings (equality constraints) on associated types, if present. /// Bindings (equality constraints) on associated types, if present.
/// ///
/// E.g., `Foo<A=Bar>`. /// E.g., `Foo<A=Bar>`.
pub bindings: P<[TypeBinding]>, pub bindings: Vec<TypeBinding>,
} }
impl Into<Option<P<PathParameters>>> for AngleBracketedParameterData { impl Into<Option<P<PathParameters>>> for AngleBracketedParameterData {
@ -297,7 +297,7 @@ pub enum TraitBoundModifier {
Maybe, Maybe,
} }
pub type TyParamBounds = P<[TyParamBound]>; pub type TyParamBounds = Vec<TyParamBound>;
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct TyParam { pub struct TyParam {
@ -314,7 +314,7 @@ pub struct TyParam {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Generics { pub struct Generics {
pub lifetimes: Vec<LifetimeDef>, pub lifetimes: Vec<LifetimeDef>,
pub ty_params: P<[TyParam]>, pub ty_params: Vec<TyParam>,
pub where_clause: WhereClause, pub where_clause: WhereClause,
pub span: Span, pub span: Span,
} }
@ -344,7 +344,7 @@ impl Default for Generics {
fn default() -> Generics { fn default() -> Generics {
Generics { Generics {
lifetimes: Vec::new(), lifetimes: Vec::new(),
ty_params: P::new(), ty_params: Vec::new(),
where_clause: WhereClause { where_clause: WhereClause {
id: DUMMY_NODE_ID, id: DUMMY_NODE_ID,
predicates: Vec::new(), predicates: Vec::new(),

View file

@ -67,9 +67,6 @@ pub trait AstBuilder {
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>; fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
fn ty_infer(&self, sp: Span) -> P<ast::Ty>; fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
fn ty_vars(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> ;
fn typaram(&self, fn typaram(&self,
span: Span, span: Span,
id: ast::Ident, id: ast::Ident,
@ -333,8 +330,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
} else { } else {
Some(P(ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData { Some(P(ast::PathParameters::AngleBracketed(ast::AngleBracketedParameterData {
lifetimes: lifetimes, lifetimes: lifetimes,
types: P::from_vec(types), types: types,
bindings: P::from_vec(bindings), bindings: bindings,
}))) })))
}; };
segments.push(ast::PathSegment { identifier: last_identifier, parameters: parameters }); 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 mut path = trait_path;
let parameters = ast::AngleBracketedParameterData { let parameters = ast::AngleBracketedParameterData {
lifetimes: lifetimes, lifetimes: lifetimes,
types: P::from_vec(types), types: types,
bindings: P::from_vec(bindings), bindings: bindings,
}; };
path.segments.push(ast::PathSegment { path.segments.push(ast::PathSegment {
identifier: ident, 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<P<ast::Ty>> {
ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
}
fn ty_vars_global(&self, ty_params: &P<[ast::TyParam]>) -> Vec<P<ast::Ty>> {
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 { fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
ast::TraitRef { ast::TraitRef {
path: path, path: path,

View file

@ -225,7 +225,7 @@ pub trait Folder : Sized {
noop_fold_ty_param(tp, self) 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<TyParam>) -> Vec<TyParam> {
noop_fold_ty_params(tps, self) noop_fold_ty_params(tps, self)
} }
@ -674,8 +674,7 @@ pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
} }
} }
pub fn noop_fold_ty_params<T: Folder>(tps: P<[TyParam]>, fld: &mut T) pub fn noop_fold_ty_params<T: Folder>(tps: Vec<TyParam>, fld: &mut T) -> Vec<TyParam> {
-> P<[TyParam]> {
tps.move_map(|tp| fld.fold_ty_param(tp)) tps.move_map(|tp| fld.fold_ty_param(tp))
} }

View file

@ -847,7 +847,7 @@ mod tests {
Abi::Rust, Abi::Rust,
ast::Generics{ // no idea on either of these: ast::Generics{ // no idea on either of these:
lifetimes: Vec::new(), lifetimes: Vec::new(),
ty_params: P::new(), ty_params: Vec::new(),
where_clause: ast::WhereClause { where_clause: ast::WhereClause {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
predicates: Vec::new(), predicates: Vec::new(),

View file

@ -685,7 +685,7 @@ impl<'a> Parser<'a> {
pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self, pub fn parse_seq_to_before_gt_or_return<T, F>(&mut self,
sep: Option<token::Token>, sep: Option<token::Token>,
mut f: F) mut f: F)
-> PResult<'a, (P<[T]>, bool)> -> PResult<'a, (Vec<T>, bool)>
where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>, where F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
{ {
let mut v = Vec::new(); let mut v = Vec::new();
@ -706,7 +706,7 @@ impl<'a> Parser<'a> {
if i % 2 == 0 { if i % 2 == 0 {
match f(self)? { match f(self)? {
Some(result) => v.push(result), Some(result) => v.push(result),
None => return Ok((P::from_vec(v), true)) None => return Ok((v, true))
} }
} else { } else {
if let Some(t) = sep.as_ref() { 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 /// Parse a sequence bracketed by '<' and '>', stopping
@ -723,7 +723,7 @@ impl<'a> Parser<'a> {
pub fn parse_seq_to_before_gt<T, F>(&mut self, pub fn parse_seq_to_before_gt<T, F>(&mut self,
sep: Option<token::Token>, sep: Option<token::Token>,
mut f: F) mut f: F)
-> PResult<'a, P<[T]>> where -> PResult<'a, Vec<T>> where
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
{ {
let (result, returned) = self.parse_seq_to_before_gt_or_return(sep, 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<T, F>(&mut self, pub fn parse_seq_to_gt<T, F>(&mut self,
sep: Option<token::Token>, sep: Option<token::Token>,
f: F) f: F)
-> PResult<'a, P<[T]>> where -> PResult<'a, Vec<T>> where
F: FnMut(&mut Parser<'a>) -> PResult<'a, T>, F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
{ {
let v = self.parse_seq_to_before_gt(sep, f)?; 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<T, F>(&mut self, pub fn parse_seq_to_gt_or_return<T, F>(&mut self,
sep: Option<token::Token>, sep: Option<token::Token>,
f: F) f: F)
-> PResult<'a, (P<[T]>, bool)> where -> PResult<'a, (Vec<T>, bool)> where
F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>, F: FnMut(&mut Parser<'a>) -> PResult<'a, Option<T>>,
{ {
let (v, returned) = self.parse_seq_to_before_gt_or_return(sep, f)?; 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)) { let other_bounds = if self.eat(&token::BinOp(token::Plus)) {
self.parse_ty_param_bounds()? self.parse_ty_param_bounds()?
} else { } else {
P::new() Vec::new()
}; };
let all_bounds = let all_bounds =
Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter() Some(TraitTyParamBound(poly_trait_ref, TraitBoundModifier::None)).into_iter()
.chain(other_bounds.into_vec()) .chain(other_bounds)
.collect(); .collect();
Ok(ast::TyKind::ObjectSum(all_bounds)) Ok(ast::TyKind::ObjectSum(all_bounds))
} }
@ -1267,7 +1267,7 @@ impl<'a> Parser<'a> {
return Ok(lhs); 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, // In type grammar, `+` is treated like a binary operator,
// and hence both L and R side are required. // 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 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})) 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()?; let (lifetimes, types, bindings) = self.parse_generic_values_after_lt()?;
ast::AngleBracketedParameterData { ast::AngleBracketedParameterData {
lifetimes: lifetimes, lifetimes: lifetimes,
types: P::from_vec(types), types: types,
bindings: P::from_vec(bindings), bindings: bindings,
}.into() }.into()
} else if self.eat(&token::OpenDelim(token::Paren)) { } else if self.eat(&token::OpenDelim(token::Paren)) {
let lo = self.prev_span.lo; let lo = self.prev_span.lo;
@ -1819,8 +1819,8 @@ impl<'a> Parser<'a> {
identifier: identifier, identifier: identifier,
parameters: ast::AngleBracketedParameterData { parameters: ast::AngleBracketedParameterData {
lifetimes: lifetimes, lifetimes: lifetimes,
types: P::from_vec(types), types: types,
bindings: P::from_vec(bindings), bindings: bindings,
}.into(), }.into(),
}); });
@ -4192,7 +4192,7 @@ impl<'a> Parser<'a> {
fn parse_colon_then_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds> fn parse_colon_then_ty_param_bounds(&mut self) -> PResult<'a, TyParamBounds>
{ {
if !self.eat(&token::Colon) { if !self.eat(&token::Colon) {
Ok(P::new()) Ok(Vec::new())
} else { } else {
self.parse_ty_param_bounds() 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 )? /// Matches typaram = IDENT (`?` unbound)? optbounds ( EQ ty )?
@ -4375,7 +4375,7 @@ impl<'a> Parser<'a> {
// If we found the `>`, don't continue. // If we found the `>`, don't continue.
if !returned { if !returned {
return Ok((lifetimes, types.into_vec(), Vec::new())); return Ok((lifetimes, types, Vec::new()));
} }
// Then parse type bindings. // 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, ()> { fn forbid_lifetime(&mut self) -> PResult<'a, ()> {

View file

@ -1009,7 +1009,7 @@ impl<'a> State<'a> {
ast::TyKind::BareFn(ref f) => { ast::TyKind::BareFn(ref f) => {
let generics = ast::Generics { let generics = ast::Generics {
lifetimes: f.lifetimes.clone(), lifetimes: f.lifetimes.clone(),
ty_params: P::new(), ty_params: Vec::new(),
where_clause: ast::WhereClause { where_clause: ast::WhereClause {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
predicates: Vec::new(), predicates: Vec::new(),
@ -2973,7 +2973,7 @@ impl<'a> State<'a> {
} }
let generics = ast::Generics { let generics = ast::Generics {
lifetimes: Vec::new(), lifetimes: Vec::new(),
ty_params: P::new(), ty_params: Vec::new(),
where_clause: ast::WhereClause { where_clause: ast::WhereClause {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
predicates: Vec::new(), predicates: Vec::new(),

View file

@ -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); .to_generics(cx, self.span, type_ident, generics);
let mut ty_params = ty_params.into_vec();
// Copy the lifetimes // Copy the lifetimes
lifetimes.extend(generics.lifetimes.iter().cloned()); lifetimes.extend(generics.lifetimes.iter().cloned());
@ -533,7 +532,7 @@ impl<'a> TraitDef<'a> {
bounds.push((*declared_bound).clone()); 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 // and similarly for where clauses
@ -596,7 +595,7 @@ impl<'a> TraitDef<'a> {
span: self.span, span: self.span,
bound_lifetimes: vec![], bound_lifetimes: vec![],
bounded_ty: ty, bounded_ty: ty,
bounds: P::from_vec(bounds), bounds: bounds,
}; };
let predicate = ast::WherePredicate::BoundPredicate(predicate); let predicate = ast::WherePredicate::BoundPredicate(predicate);
@ -607,7 +606,7 @@ impl<'a> TraitDef<'a> {
let trait_generics = Generics { let trait_generics = Generics {
lifetimes: lifetimes, lifetimes: lifetimes,
ty_params: P::from_vec(ty_params), ty_params: ty_params,
where_clause: where_clause, where_clause: where_clause,
span: span, span: span,
}; };

View file

@ -212,7 +212,7 @@ fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>, s
-> Generics { -> Generics {
Generics { Generics {
lifetimes: lifetimes, lifetimes: lifetimes,
ty_params: P::from_vec(ty_params), ty_params: ty_params,
where_clause: ast::WhereClause { where_clause: ast::WhereClause {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
predicates: Vec::new(), predicates: Vec::new(),