Skip to content

Commit 9a68098

Browse files
committed
Move impls for qpath recovery trait from ast.rs
1 parent 81622c6 commit 9a68098

File tree

2 files changed

+65
-58
lines changed

2 files changed

+65
-58
lines changed

src/libsyntax/ast.rs

+25-56
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use syntax_pos::{Span, DUMMY_SP};
2020
use codemap::{respan, Spanned};
2121
use abi::Abi;
2222
use ext::hygiene::{Mark, SyntaxContext};
23-
use parse::parser::{RecoverQPath, PathStyle};
2423
use print::pprust;
2524
use ptr::P;
2625
use rustc_data_structures::indexed_vec;
@@ -485,6 +484,30 @@ impl fmt::Debug for Pat {
485484
}
486485

487486
impl Pat {
487+
pub(super) fn to_ty(&self) -> Option<P<Ty>> {
488+
let node = match &self.node {
489+
PatKind::Wild => TyKind::Infer,
490+
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) =>
491+
TyKind::Path(None, Path::from_ident(ident.span, ident.node)),
492+
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
493+
PatKind::Mac(mac) => TyKind::Mac(mac.clone()),
494+
PatKind::Ref(pat, mutbl) =>
495+
pat.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
496+
PatKind::Slice(pats, None, _) if pats.len() == 1 =>
497+
pats[0].to_ty().map(TyKind::Slice)?,
498+
PatKind::Tuple(pats, None) => {
499+
let mut tys = Vec::new();
500+
for pat in pats {
501+
tys.push(pat.to_ty()?);
502+
}
503+
TyKind::Tup(tys)
504+
}
505+
_ => return None,
506+
};
507+
508+
Some(P(Ty { node, id: self.id, span: self.span }))
509+
}
510+
488511
pub fn walk<F>(&self, it: &mut F) -> bool
489512
where F: FnMut(&Pat) -> bool
490513
{
@@ -520,38 +543,6 @@ impl Pat {
520543
}
521544
}
522545

523-
impl RecoverQPath for Pat {
524-
fn to_ty(&self) -> Option<P<Ty>> {
525-
let node = match &self.node {
526-
PatKind::Wild => TyKind::Infer,
527-
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None) =>
528-
TyKind::Path(None, Path::from_ident(ident.span, ident.node)),
529-
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
530-
PatKind::Mac(mac) => TyKind::Mac(mac.clone()),
531-
PatKind::Ref(pat, mutbl) =>
532-
pat.to_ty().map(|ty| TyKind::Rptr(None, MutTy { ty, mutbl: *mutbl }))?,
533-
PatKind::Slice(pats, None, _) if pats.len() == 1 =>
534-
pats[0].to_ty().map(TyKind::Slice)?,
535-
PatKind::Tuple(pats, None) => {
536-
let mut tys = Vec::new();
537-
for pat in pats {
538-
tys.push(pat.to_ty()?);
539-
}
540-
TyKind::Tup(tys)
541-
}
542-
_ => return None,
543-
};
544-
545-
Some(P(Ty { node, id: self.id, span: self.span }))
546-
}
547-
fn to_recovered(&self, qself: Option<QSelf>, path: Path) -> Self {
548-
Self { span: path.span, node: PatKind::Path(qself, path), id: self.id }
549-
}
550-
fn to_string(&self) -> String {
551-
pprust::pat_to_string(self)
552-
}
553-
}
554-
555546
/// A single field in a struct pattern
556547
///
557548
/// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
@@ -919,10 +910,8 @@ impl Expr {
919910
_ => None,
920911
}
921912
}
922-
}
923913

924-
impl RecoverQPath for Expr {
925-
fn to_ty(&self) -> Option<P<Ty>> {
914+
pub(super) fn to_ty(&self) -> Option<P<Ty>> {
926915
let node = match &self.node {
927916
ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
928917
ExprKind::Mac(mac) => TyKind::Mac(mac.clone()),
@@ -951,13 +940,6 @@ impl RecoverQPath for Expr {
951940

952941
Some(P(Ty { node, id: self.id, span: self.span }))
953942
}
954-
fn to_recovered(&self, qself: Option<QSelf>, path: Path) -> Self {
955-
Self { span: path.span, node: ExprKind::Path(qself, path),
956-
id: self.id, attrs: self.attrs.clone() }
957-
}
958-
fn to_string(&self) -> String {
959-
pprust::expr_to_string(self)
960-
}
961943
}
962944

963945
impl fmt::Debug for Expr {
@@ -1469,19 +1451,6 @@ pub struct Ty {
14691451
pub span: Span,
14701452
}
14711453

1472-
impl RecoverQPath for Ty {
1473-
fn to_ty(&self) -> Option<P<Ty>> {
1474-
Some(P(self.clone()))
1475-
}
1476-
fn to_recovered(&self, qself: Option<QSelf>, path: Path) -> Self {
1477-
Self { span: path.span, node: TyKind::Path(qself, path), id: self.id }
1478-
}
1479-
fn to_string(&self) -> String {
1480-
pprust::ty_to_string(self)
1481-
}
1482-
const PATH_STYLE: PathStyle = PathStyle::Type;
1483-
}
1484-
14851454
impl fmt::Debug for Ty {
14861455
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14871456
write!(f, "type({})", pprust::ty_to_string(self))

src/libsyntax/parse/parser.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,49 @@ enum PrevTokenKind {
169169
Other,
170170
}
171171

172-
pub(crate) trait RecoverQPath: Sized {
172+
trait RecoverQPath: Sized {
173+
const PATH_STYLE: PathStyle = PathStyle::Expr;
173174
fn to_ty(&self) -> Option<P<Ty>>;
174175
fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self;
175176
fn to_string(&self) -> String;
176-
const PATH_STYLE: PathStyle = PathStyle::Expr;
177+
}
178+
179+
impl RecoverQPath for Ty {
180+
const PATH_STYLE: PathStyle = PathStyle::Type;
181+
fn to_ty(&self) -> Option<P<Ty>> {
182+
Some(P(self.clone()))
183+
}
184+
fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self {
185+
Self { span: path.span, node: TyKind::Path(qself, path), id: self.id }
186+
}
187+
fn to_string(&self) -> String {
188+
pprust::ty_to_string(self)
189+
}
190+
}
191+
192+
impl RecoverQPath for Pat {
193+
fn to_ty(&self) -> Option<P<Ty>> {
194+
self.to_ty()
195+
}
196+
fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self {
197+
Self { span: path.span, node: PatKind::Path(qself, path), id: self.id }
198+
}
199+
fn to_string(&self) -> String {
200+
pprust::pat_to_string(self)
201+
}
202+
}
203+
204+
impl RecoverQPath for Expr {
205+
fn to_ty(&self) -> Option<P<Ty>> {
206+
self.to_ty()
207+
}
208+
fn to_recovered(&self, qself: Option<QSelf>, path: ast::Path) -> Self {
209+
Self { span: path.span, node: ExprKind::Path(qself, path),
210+
id: self.id, attrs: self.attrs.clone() }
211+
}
212+
fn to_string(&self) -> String {
213+
pprust::expr_to_string(self)
214+
}
177215
}
178216

179217
/* ident is handled by common.rs */

0 commit comments

Comments
 (0)