diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2017-12-18 23:26:59 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2017-12-20 22:43:47 +0300 |
| commit | 9a68098e87bdc5a73c6714064eb1627590b4c0bc (patch) | |
| tree | 081ba5793c9982f37db30e8b080264fc61a1ca36 | |
| parent | 81622c6b02536bdcf56145beb317da0d336703c1 (diff) | |
| download | rust-9a68098e87bdc5a73c6714064eb1627590b4c0bc.tar.gz rust-9a68098e87bdc5a73c6714064eb1627590b4c0bc.zip | |
Move impls for qpath recovery trait from `ast.rs`
| -rw-r--r-- | src/libsyntax/ast.rs | 81 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 42 |
2 files changed, 65 insertions, 58 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 461cb0480d2..1d399f159c8 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -20,7 +20,6 @@ use syntax_pos::{Span, DUMMY_SP}; use codemap::{respan, Spanned}; use abi::Abi; use ext::hygiene::{Mark, SyntaxContext}; -use parse::parser::{RecoverQPath, PathStyle}; use print::pprust; use ptr::P; use rustc_data_structures::indexed_vec; @@ -485,6 +484,30 @@ impl fmt::Debug for Pat { } impl Pat { + pub(super) fn to_ty(&self) -> Option<P<Ty>> { + let node = match &self.node { + PatKind::Wild => TyKind::Infer, + PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) => + TyKind::Path(None, Path::from_ident(ident.span, ident.node)), + PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()), + PatKind::Mac(mac) => TyKind::Mac(mac.clone()), + PatKind::Ref(pat, mutbl) => + pat.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?, + PatKind::Slice(pats, None, _) if pats.len() == 1 => + pats[0].to_ty().map(TyKind::Slice)?, + PatKind::Tuple(pats, None) => { + let mut tys = Vec::new(); + for pat in pats { + tys.push(pat.to_ty()?); + } + TyKind::Tup(tys) + } + _ => return None, + }; + + Some(P(Ty { node, id: self.id, span: self.span })) + } + pub fn walk<F>(&self, it: &mut F) -> bool where F: FnMut(&Pat) -> bool { @@ -520,38 +543,6 @@ impl Pat { } } -impl RecoverQPath for Pat { - fn to_ty(&self) -> Option<P<Ty>> { - let node = match &self.node { - PatKind::Wild => TyKind::Infer, - PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) => - TyKind::Path(None, Path::from_ident(ident.span, ident.node)), - PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()), - PatKind::Mac(mac) => TyKind::Mac(mac.clone()), - PatKind::Ref(pat, mutbl) => - pat.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?, - PatKind::Slice(pats, None, _) if pats.len() == 1 => - pats[0].to_ty().map(TyKind::Slice)?, - PatKind::Tuple(pats, None) => { - let mut tys = Vec::new(); - for pat in pats { - tys.push(pat.to_ty()?); - } - TyKind::Tup(tys) - } - _ => return None, - }; - - Some(P(Ty { node, id: self.id, span: self.span })) - } - fn to_recovered(&self, qself: Option<QSelf>, path: Path) -> Self { - Self { span: path.span, node: PatKind::Path(qself, path), id: self.id } - } - fn to_string(&self) -> String { - pprust::pat_to_string(self) - } -} - /// A single field in a struct pattern /// /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` @@ -919,10 +910,8 @@ impl Expr { _ => None, } } -} -impl RecoverQPath for Expr { - fn to_ty(&self) -> Option<P<Ty>> { + pub(super) fn to_ty(&self) -> Option<P<Ty>> { let node = match &self.node { ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()), ExprKind::Mac(mac) => TyKind::Mac(mac.clone()), @@ -951,13 +940,6 @@ impl RecoverQPath for Expr { Some(P(Ty { node, id: self.id, span: self.span })) } - fn to_recovered(&self, qself: Option<QSelf>, path: Path) -> Self { - Self { span: path.span, node: ExprKind::Path(qself, path), - id: self.id, attrs: self.attrs.clone() } - } - fn to_string(&self) -> String { - pprust::expr_to_string(self) - } } impl fmt::Debug for Expr { @@ -1469,19 +1451,6 @@ pub struct Ty { pub span: Span, } -impl RecoverQPath for Ty { - fn to_ty(&self) -> Option<P<Ty>> { - Some(P(self.clone())) - } - fn to_recovered(&self, qself: Option<QSelf>, path: Path) -> Self { - Self { span: path.span, node: TyKind::Path(qself, path), id: self.id } - } - fn to_string(&self) -> String { - pprust::ty_to_string(self) - } - const PATH_STYLE: PathStyle = PathStyle::Type; -} - impl fmt::Debug for Ty { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "type({})", pprust::ty_to_string(self)) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 74ec11b83c7..010ab3c2781 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -169,11 +169,49 @@ enum PrevTokenKind { Other, } -pub(crate) trait RecoverQPath: Sized { +trait RecoverQPath: Sized { + const PATH_STYLE: PathStyle = PathStyle::Expr; fn to_ty(&self) -> Option<P<Ty>>; fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self; fn to_string(&self) -> String; - const PATH_STYLE: PathStyle = PathStyle::Expr; +} + +impl RecoverQPath for Ty { + const PATH_STYLE: PathStyle = PathStyle::Type; + fn to_ty(&self) -> Option<P<Ty>> { + Some(P(self.clone())) + } + fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self { + Self { span: path.span, node: TyKind::Path(qself, path), id: self.id } + } + fn to_string(&self) -> String { + pprust::ty_to_string(self) + } +} + +impl RecoverQPath for Pat { + fn to_ty(&self) -> Option<P<Ty>> { + self.to_ty() + } + fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self { + Self { span: path.span, node: PatKind::Path(qself, path), id: self.id } + } + fn to_string(&self) -> String { + pprust::pat_to_string(self) + } +} + +impl RecoverQPath for Expr { + fn to_ty(&self) -> Option<P<Ty>> { + self.to_ty() + } + fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self { + Self { span: path.span, node: ExprKind::Path(qself, path), + id: self.id, attrs: self.attrs.clone() } + } + fn to_string(&self) -> String { + pprust::expr_to_string(self) + } } /* ident is handled by common.rs */ |
