about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-06-12 15:23:28 +0000
committerbors <bors@rust-lang.org>2018-06-12 15:23:28 +0000
commitef8cb40c9c03fe60aac47c8736045231633dbdac (patch)
tree372aa750a7d6b5351c8824c762ac7b68b8640825 /src/libsyntax
parent3f3ba6c3cb1707eaf63640fc6681ae389253fe18 (diff)
parentd13bfd294c55a47bb33e5846ce184c80cdf547dd (diff)
downloadrust-ef8cb40c9c03fe60aac47c8736045231633dbdac.tar.gz
rust-ef8cb40c9c03fe60aac47c8736045231633dbdac.zip
Auto merge of #51519 - ExpHP:issue-51331-b, r=petrochenkov
Fix for $crate var normalization in proc macro for externally defined macros

Fixes #51331, a bug that has existed in at least *some* form for a year and a half.

The PR includes the addition of a `fold_qpath` method to `syntax::fold::Folder`.  Overriding this method is useful for folds that modify paths in a way that invalidates indices (insertion or removal of a component), as it provides the opportunity to update `qself.position` in `<A as B>::C` paths.  I added it because the bugfix is messy without it.

(unfortunately, grepping around the codebase, I did not see anything else that could use it.)
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/fold.rs49
1 files changed, 24 insertions, 25 deletions
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 2f209b347a4..1056b3dad17 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -168,6 +168,10 @@ pub trait Folder : Sized {
         noop_fold_path(p, self)
     }
 
+    fn fold_qpath(&mut self, qs: Option<QSelf>, p: Path) -> (Option<QSelf>, Path) {
+        noop_fold_qpath(qs, p, self)
+    }
+
     fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
         noop_fold_path_parameters(p, self)
     }
@@ -370,14 +374,8 @@ pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
             TyKind::Tup(tys) => TyKind::Tup(tys.move_map(|ty| fld.fold_ty(ty))),
             TyKind::Paren(ty) => TyKind::Paren(fld.fold_ty(ty)),
             TyKind::Path(qself, path) => {
-                let qself = qself.map(|QSelf { ty, path_span, position }| {
-                    QSelf {
-                        ty: fld.fold_ty(ty),
-                        path_span: fld.new_span(path_span),
-                        position,
-                    }
-                });
-                TyKind::Path(qself, fld.fold_path(path))
+                let (qself, path) = fld.fold_qpath(qself, path);
+                TyKind::Path(qself, path)
             }
             TyKind::Array(ty, length) => {
                 TyKind::Array(fld.fold_ty(ty), fld.fold_anon_const(length))
@@ -442,6 +440,19 @@ pub fn noop_fold_path<T: Folder>(Path { segments, span }: Path, fld: &mut T) ->
     }
 }
 
+pub fn noop_fold_qpath<T: Folder>(qself: Option<QSelf>,
+                                  path: Path,
+                                  fld: &mut T) -> (Option<QSelf>, Path) {
+    let qself = qself.map(|QSelf { ty, path_span, position }| {
+        QSelf {
+            ty: fld.fold_ty(ty),
+            path_span: fld.new_span(path_span),
+            position,
+        }
+    });
+    (qself, fld.fold_path(path))
+}
+
 pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters, fld: &mut T)
                                             -> PathParameters
 {
@@ -1097,15 +1108,9 @@ pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
                 PatKind::TupleStruct(folder.fold_path(pth),
                         pats.move_map(|x| folder.fold_pat(x)), ddpos)
             }
-            PatKind::Path(opt_qself, pth) => {
-                let opt_qself = opt_qself.map(|qself| {
-                    QSelf {
-                        ty: folder.fold_ty(qself.ty),
-                        path_span: folder.new_span(qself.path_span),
-                        position: qself.position,
-                    }
-                });
-                PatKind::Path(opt_qself, folder.fold_path(pth))
+            PatKind::Path(qself, pth) => {
+                let (qself, pth) = folder.fold_qpath(qself, pth);
+                PatKind::Path(qself, pth)
             }
             PatKind::Struct(pth, fields, etc) => {
                 let pth = folder.fold_path(pth);
@@ -1267,14 +1272,8 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
                                 lim)
             }
             ExprKind::Path(qself, path) => {
-                let qself = qself.map(|QSelf { ty, path_span, position }| {
-                    QSelf {
-                        ty: folder.fold_ty(ty),
-                        path_span: folder.new_span(path_span),
-                        position,
-                    }
-                });
-                ExprKind::Path(qself, folder.fold_path(path))
+                let (qself, path) = folder.fold_qpath(qself, path);
+                ExprKind::Path(qself, path)
             }
             ExprKind::Break(opt_label, opt_expr) => {
                 ExprKind::Break(opt_label.map(|label| folder.fold_label(label)),