diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2015-01-31 21:20:24 +0200 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2015-02-24 14:14:16 +0200 |
| commit | ffb8092ccf8dd186a9a03e6808d04a7276206793 (patch) | |
| tree | f9fab075f87149cc4ebbac0fed1e4ac90657c60c /src/libsyntax | |
| parent | 326711e9bdee2e8f467ad716109b5a270b61478d (diff) | |
| download | rust-ffb8092ccf8dd186a9a03e6808d04a7276206793.tar.gz rust-ffb8092ccf8dd186a9a03e6808d04a7276206793.zip | |
syntax: use a single Path for Trait::Item in QPath.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 11 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 44 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 27 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 32 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 53 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 15 |
6 files changed, 78 insertions, 104 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5f9776425c3..80a5527cb94 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -757,7 +757,7 @@ pub enum Expr_ { /// type parameters, e.g. foo::bar::<baz> ExprPath(Path), /// A "qualified path", e.g. `<Vec<T> as SomeTrait>::SomeType` - ExprQPath(P<QPath>), + ExprQPath(QPath), ExprAddrOf(Mutability, P<Expr>), ExprBreak(Option<Ident>), @@ -781,13 +781,12 @@ pub enum Expr_ { /// A "qualified path": /// /// <Vec<T> as SomeTrait>::SomeAssociatedItem -/// ^~~~~ ^~~~~~~~~ ^~~~~~~~~~~~~~~~~~ -/// self_type trait_path item_path +/// ^~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +/// self_type path #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct QPath { pub self_type: P<Ty>, - pub trait_path: Path, - pub item_path: PathSegment, + pub path: Path, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] @@ -1259,7 +1258,7 @@ pub enum Ty_ { /// Type parameters are stored in the Path itself TyPath(Path), /// A "qualified path", e.g. `<Vec<T> as SomeTrait>::SomeType` - TyQPath(P<QPath>), + TyQPath(QPath), /// Something like `A+B`. Note that `B` must always be a path. TyObjectSum(P<Ty>, TyParamBounds), /// A type like `for<'a> Foo<&'a Bar>` diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index baa2fab044f..90842bbab47 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -41,16 +41,16 @@ pub trait AstBuilder { -> ast::Path; fn qpath(&self, self_type: P<ast::Ty>, - trait_ref: P<ast::TraitRef>, - ident: ast::Ident ) - -> P<ast::QPath>; + trait_path: ast::Path, + ident: ast::Ident) + -> ast::QPath; fn qpath_all(&self, self_type: P<ast::Ty>, - trait_ref: P<ast::TraitRef>, + trait_path: ast::Path, ident: ast::Ident, lifetimes: Vec<ast::Lifetime>, types: Vec<P<ast::Ty>>, - bindings: Vec<P<ast::TypeBinding>> ) - -> P<ast::QPath>; + bindings: Vec<P<ast::TypeBinding>>) + -> ast::QPath; // types fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy; @@ -114,7 +114,7 @@ pub trait AstBuilder { // expressions fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>; fn expr_path(&self, path: ast::Path) -> P<ast::Expr>; - fn expr_qpath(&self, span: Span, qpath: P<ast::QPath>) -> P<ast::Expr>; + fn expr_qpath(&self, span: Span, qpath: ast::QPath) -> P<ast::Expr>; fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>; fn expr_self(&self, span: Span) -> P<ast::Expr>; @@ -346,40 +346,40 @@ impl<'a> AstBuilder for ExtCtxt<'a> { /// Constructs a qualified path. /// - /// Constructs a path like `<self_type as trait_ref>::ident`. + /// Constructs a path like `<self_type as trait_path>::ident`. fn qpath(&self, self_type: P<ast::Ty>, - trait_ref: P<ast::TraitRef>, + trait_path: ast::Path, ident: ast::Ident) - -> P<ast::QPath> { - self.qpath_all(self_type, trait_ref, ident, Vec::new(), Vec::new(), Vec::new()) + -> ast::QPath { + self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![]) } /// Constructs a qualified path. /// - /// Constructs a path like `<self_type as trait_ref>::ident<a, T, A=Bar>`. + /// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`. fn qpath_all(&self, self_type: P<ast::Ty>, - trait_ref: P<ast::TraitRef>, + trait_path: ast::Path, ident: ast::Ident, lifetimes: Vec<ast::Lifetime>, types: Vec<P<ast::Ty>>, - bindings: Vec<P<ast::TypeBinding>> ) - -> P<ast::QPath> { - let segment = ast::PathSegment { + bindings: Vec<P<ast::TypeBinding>>) + -> ast::QPath { + let mut path = trait_path; + path.segments.push(ast::PathSegment { identifier: ident, parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData { lifetimes: lifetimes, types: OwnedSlice::from_vec(types), bindings: OwnedSlice::from_vec(bindings), }) - }; + }); - P(ast::QPath { + ast::QPath { self_type: self_type, - trait_ref: trait_ref, - item_path: segment, - }) + path: path + } } fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy { @@ -607,7 +607,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } /// Constructs a QPath expression. - fn expr_qpath(&self, span: Span, qpath: P<ast::QPath>) -> P<ast::Expr> { + fn expr_qpath(&self, span: Span, qpath: ast::QPath) -> P<ast::Expr> { self.expr(span, ast::ExprQPath(qpath)) } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index c0421fb6f1c..c706ce9065d 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -146,10 +146,6 @@ pub trait Folder : Sized { noop_fold_ty(t, self) } - fn fold_qpath(&mut self, t: P<QPath>) -> P<QPath> { - noop_fold_qpath(t, self) - } - fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> { noop_fold_ty_binding(t, self) } @@ -430,7 +426,10 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> { TyParen(ty) => TyParen(fld.fold_ty(ty)), TyPath(path) => TyPath(fld.fold_path(path)), TyQPath(qpath) => { - TyQPath(fld.fold_qpath(qpath)) + TyQPath(QPath { + self_type: fld.fold_ty(qpath.self_type), + path: fld.fold_path(qpath.path) + }) } TyObjectSum(ty, bounds) => { TyObjectSum(fld.fold_ty(ty), @@ -450,19 +449,6 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> { }) } -pub fn noop_fold_qpath<T: Folder>(qpath: P<QPath>, fld: &mut T) -> P<QPath> { - qpath.map(|qpath| { - QPath { - self_type: fld.fold_ty(qpath.self_type), - trait_path: fld.fold_path(qpath.trait_path), - item_path: PathSegment { - identifier: fld.fold_ident(qpath.item_path.identifier), - parameters: fld.fold_path_parameters(qpath.item_path.parameters), - } - } - }) -} - pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, items}: ForeignMod, fld: &mut T) -> ForeignMod { ForeignMod { @@ -1362,7 +1348,10 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) -> e2.map(|x| folder.fold_expr(x))) } ExprPath(pth) => ExprPath(folder.fold_path(pth)), - ExprQPath(qpath) => ExprQPath(folder.fold_qpath(qpath)), + ExprQPath(qpath) => ExprQPath(QPath { + self_type: folder.fold_ty(qpath.self_type), + path: folder.fold_path(qpath.path) + }), ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))), ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))), ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b2f59725855..ad290da7d0a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1525,18 +1525,14 @@ impl<'a> Parser<'a> { // QUALIFIED PATH `<TYPE as TRAIT_REF>::item` let self_type = self.parse_ty_sum(); self.expect_keyword(keywords::As); - let trait_path = self.parse_path(LifetimeAndTypesWithoutColons); + let mut path = self.parse_path(LifetimeAndTypesWithoutColons); self.expect(&token::Gt); self.expect(&token::ModSep); - let item_name = self.parse_ident(); - TyQPath(P(QPath { - self_type: self_type, - trait_path: trait_path, - item_path: ast::PathSegment { - identifier: item_name, - parameters: ast::PathParameters::none() - } - })) + path.segments.push(ast::PathSegment { + identifier: self.parse_ident(), + parameters: ast::PathParameters::none() + }); + TyQPath(QPath { self_type: self_type, path: path }) } else if self.check(&token::ModSep) || self.token.is_ident() || self.token.is_path() { @@ -2220,7 +2216,7 @@ impl<'a> Parser<'a> { // QUALIFIED PATH `<TYPE as TRAIT_REF>::item::<'a, T>` let self_type = self.parse_ty_sum(); self.expect_keyword(keywords::As); - let trait_path = self.parse_path(LifetimeAndTypesWithoutColons); + let mut path = self.parse_path(LifetimeAndTypesWithoutColons); self.expect(&token::Gt); self.expect(&token::ModSep); let item_name = self.parse_ident(); @@ -2237,15 +2233,13 @@ impl<'a> Parser<'a> { } else { ast::PathParameters::none() }; + path.segments.push(ast::PathSegment { + identifier: item_name, + parameters: parameters + }); let hi = self.span.hi; - return self.mk_expr(lo, hi, ExprQPath(P(QPath { - self_type: self_type, - trait_path: trait_path, - item_path: ast::PathSegment { - identifier: item_name, - parameters: parameters - } - }))); + return self.mk_expr(lo, hi, + ExprQPath(QPath { self_type: self_type, path: path })); } if self.eat_keyword(keywords::Move) { return self.parse_lambda_expr(CaptureByValue); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 3cfb90a3e68..11502c29ebb 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -373,7 +373,7 @@ pub fn fn_block_to_string(p: &ast::FnDecl) -> String { } pub fn path_to_string(p: &ast::Path) -> String { - $to_string(|s| s.print_path(p, false)) + $to_string(|s| s.print_path(p, false, 0)) } pub fn ident_to_string(id: &ast::Ident) -> String { @@ -730,7 +730,10 @@ impl<'a> State<'a> { None)); } ast::TyPath(ref path) => { - try!(self.print_path(path, false)); + try!(self.print_path(path, false, 0)); + } + ast::TyQPath(ref qpath) => { + try!(self.print_qpath(qpath, false)) } ast::TyObjectSum(ref ty, ref bounds) => { try!(self.print_type(&**ty)); @@ -739,9 +742,6 @@ impl<'a> State<'a> { ast::TyPolyTraitRef(ref bounds) => { try!(self.print_bounds("", &bounds[..])); } - ast::TyQPath(ref qpath) => { - try!(self.print_qpath(&**qpath, false)) - } ast::TyFixedLengthVec(ref ty, ref v) => { try!(word(&mut self.s, "[")); try!(self.print_type(&**ty)); @@ -1018,7 +1018,7 @@ impl<'a> State<'a> { ast::ItemMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), ..}) => { try!(self.print_visibility(item.vis)); - try!(self.print_path(pth, false)); + try!(self.print_path(pth, false, 0)); try!(word(&mut self.s, "! ")); try!(self.print_ident(item.ident)); try!(self.cbox(indent_unit)); @@ -1033,7 +1033,7 @@ impl<'a> State<'a> { } fn print_trait_ref(&mut self, t: &ast::TraitRef) -> IoResult<()> { - self.print_path(&t.path, false) + self.print_path(&t.path, false, 0) } fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> IoResult<()> { @@ -1297,7 +1297,7 @@ impl<'a> State<'a> { ast::MethMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), ..}) => { // code copied from ItemMac: - try!(self.print_path(pth, false)); + try!(self.print_path(pth, false, 0)); try!(word(&mut self.s, "! ")); try!(self.cbox(indent_unit)); try!(self.popen()); @@ -1514,7 +1514,7 @@ impl<'a> State<'a> { match m.node { // I think it's reasonable to hide the ctxt here: ast::MacInvocTT(ref pth, ref tts, _) => { - try!(self.print_path(pth, false)); + try!(self.print_path(pth, false, 0)); try!(word(&mut self.s, "!")); match delim { token::Paren => try!(self.popen()), @@ -1584,7 +1584,7 @@ impl<'a> State<'a> { path: &ast::Path, fields: &[ast::Field], wth: &Option<P<ast::Expr>>) -> IoResult<()> { - try!(self.print_path(path, true)); + try!(self.print_path(path, true, 0)); if !(fields.is_empty() && wth.is_none()) { try!(word(&mut self.s, "{")); try!(self.commasep_cmnt( @@ -1852,8 +1852,8 @@ impl<'a> State<'a> { try!(self.print_expr(&**e)); } } - ast::ExprPath(ref path) => try!(self.print_path(path, true)), - ast::ExprQPath(ref qpath) => try!(self.print_qpath(&**qpath, true)), + ast::ExprPath(ref path) => try!(self.print_path(path, true, 0)), + ast::ExprQPath(ref qpath) => try!(self.print_qpath(qpath, true)), ast::ExprBreak(opt_ident) => { try!(word(&mut self.s, "break")); try!(space(&mut self.s)); @@ -2014,16 +2014,14 @@ impl<'a> State<'a> { fn print_path(&mut self, path: &ast::Path, - colons_before_params: bool) + colons_before_params: bool, + depth: usize) -> IoResult<()> { try!(self.maybe_print_comment(path.span.lo)); - if path.global { - try!(word(&mut self.s, "::")); - } - let mut first = true; - for segment in &path.segments { + let mut first = !path.global; + for segment in &path.segments[..path.segments.len()-depth] { if first { first = false } else { @@ -2047,11 +2045,12 @@ impl<'a> State<'a> { try!(self.print_type(&*qpath.self_type)); try!(space(&mut self.s)); try!(self.word_space("as")); - try!(self.print_path(&qpath.trait_path, false)); + try!(self.print_path(&qpath.path, false, 1)); try!(word(&mut self.s, ">")); try!(word(&mut self.s, "::")); - try!(self.print_ident(qpath.item_path.identifier)); - self.print_path_parameters(&qpath.item_path.parameters, colons_before_params) + let item_segment = qpath.path.segments.last().unwrap(); + try!(self.print_ident(item_segment.identifier)); + self.print_path_parameters(&item_segment.parameters, colons_before_params) } fn print_path_parameters(&mut self, @@ -2156,7 +2155,7 @@ impl<'a> State<'a> { } } ast::PatEnum(ref path, ref args_) => { - try!(self.print_path(path, true)); + try!(self.print_path(path, true, 0)); match *args_ { None => try!(word(&mut self.s, "(..)")), Some(ref args) => { @@ -2170,7 +2169,7 @@ impl<'a> State<'a> { } } ast::PatStruct(ref path, ref fields, etc) => { - try!(self.print_path(path, true)); + try!(self.print_path(path, true, 0)); try!(self.nbsp()); try!(self.word_space("{")); try!(self.commasep_cmnt( @@ -2555,7 +2554,7 @@ impl<'a> State<'a> { } } &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => { - try!(self.print_path(path, false)); + try!(self.print_path(path, false, 0)); try!(space(&mut self.s)); try!(self.word_space("=")); try!(self.print_type(&**ty)); @@ -2592,7 +2591,7 @@ impl<'a> State<'a> { pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> { match vp.node { ast::ViewPathSimple(ident, ref path) => { - try!(self.print_path(path, false)); + try!(self.print_path(path, false, 0)); // FIXME(#6993) can't compare identifiers directly here if path.segments.last().unwrap().identifier.name != @@ -2606,7 +2605,7 @@ impl<'a> State<'a> { } ast::ViewPathGlob(ref path) => { - try!(self.print_path(path, false)); + try!(self.print_path(path, false, 0)); word(&mut self.s, "::*") } @@ -2614,7 +2613,7 @@ impl<'a> State<'a> { if path.segments.is_empty() { try!(word(&mut self.s, "{")); } else { - try!(self.print_path(path, false)); + try!(self.print_path(path, false, 0)); try!(word(&mut self.s, "::{")); } try!(self.commasep(Inconsistent, &idents[..], |s, w| { diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 4e90adea90c..55372585062 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -125,9 +125,6 @@ pub trait Visitor<'v> : Sized { fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) { walk_path(self, path) } - fn visit_qpath(&mut self, qpath: &'v QPath, _id: ast::NodeId) { - walk_qpath(self, qpath) - } fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) { walk_path_segment(self, path_span, path_segment) } @@ -403,7 +400,8 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) { visitor.visit_path(path, typ.id); } TyQPath(ref qpath) => { - visitor.visit_qpath(&**qpath, typ.id); + visitor.visit_ty(&*qpath.self_type); + visitor.visit_path(&qpath.path, typ.id); } TyObjectSum(ref ty, ref bounds) => { visitor.visit_ty(&**ty); @@ -436,12 +434,6 @@ pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) { } } -pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath) { - visitor.visit_ty(&*qpath.self_type); - walk_path(visitor, &qpath.trait_path); - visitor.visit_path_segment(qpath.trait_path.span, &qpath.item_path); -} - pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, path_span: Span, segment: &'v PathSegment) { @@ -871,7 +863,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { visitor.visit_path(path, expression.id) } ExprQPath(ref qpath) => { - visitor.visit_qpath(&**qpath, expression.id) + visitor.visit_ty(&*qpath.self_type); + visitor.visit_path(&qpath.path, expression.id); } ExprBreak(_) | ExprAgain(_) => {} ExprRet(ref optional_expression) => { |
