Skip to content

Commit e1e5c14

Browse files
committed
In Parser and ExtCtxt, replace fields filename and mod_path_stack
with a single field `directory: PathBuf`.
1 parent 86995dc commit e1e5c14

File tree

4 files changed

+38
-57
lines changed

4 files changed

+38
-57
lines changed

src/libsyntax/ext/base.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use fold::Folder;
3030
use feature_gate;
3131

3232
use std::collections::{HashMap, HashSet};
33+
use std::path::PathBuf;
3334
use std::rc::Rc;
3435
use tokenstream;
3536

@@ -602,8 +603,7 @@ pub struct ExtCtxt<'a> {
602603
pub derive_modes: HashMap<InternedString, Box<MultiItemModifier>>,
603604
pub recursion_count: usize,
604605

605-
pub filename: Option<String>,
606-
pub mod_path_stack: Vec<InternedString>,
606+
pub directory: PathBuf,
607607
pub in_block: bool,
608608
}
609609

@@ -626,8 +626,7 @@ impl<'a> ExtCtxt<'a> {
626626
derive_modes: HashMap::new(),
627627
recursion_count: 0,
628628

629-
filename: None,
630-
mod_path_stack: Vec::new(),
629+
directory: PathBuf::new(),
631630
in_block: false,
632631
}
633632
}

src/libsyntax/ext/expand.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use visit;
2828
use visit::Visitor;
2929
use std_inject;
3030

31+
use std::path::PathBuf;
32+
3133
// A trait for AST nodes and AST node lists into which macro invocations may expand.
3234
trait MacroGenerable: Sized {
3335
// Expand the given MacResult using its appropriate `make_*` method.
@@ -566,7 +568,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
566568

567569
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
568570
fn fold_crate(&mut self, c: Crate) -> Crate {
569-
self.cx.filename = Some(self.cx.parse_sess.codemap().span_to_filename(c.span));
571+
let mut directory = PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(c.span));
572+
directory.pop();
573+
self.cx.directory = directory;
570574
noop_fold_crate(c, self)
571575
}
572576

@@ -591,18 +595,22 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
591595
let result;
592596
if let ast::ItemKind::Mod(ast::Mod { inner, .. }) = item.node {
593597
if item.span.contains(inner) {
594-
self.push_mod_path(item.ident, &item.attrs);
598+
let directory = self.cx.directory.clone();
599+
self.cx.directory.push(&*{
600+
::attr::first_attr_value_str_by_name(&item.attrs, "path")
601+
.unwrap_or(item.ident.name.as_str())
602+
});
595603
result = expand_item(item, self);
596-
self.pop_mod_path();
604+
self.cx.directory = directory;
597605
} else {
598-
let filename = if inner != syntax_pos::DUMMY_SP {
599-
Some(self.cx.parse_sess.codemap().span_to_filename(inner))
600-
} else { None };
601-
let orig_filename = replace(&mut self.cx.filename, filename);
602-
let orig_mod_path_stack = replace(&mut self.cx.mod_path_stack, Vec::new());
606+
let mut directory = match inner {
607+
syntax_pos::DUMMY_SP => PathBuf::new(),
608+
_ => PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner)),
609+
};
610+
directory.pop();
611+
let directory = replace(&mut self.cx.directory, directory);
603612
result = expand_item(item, self);
604-
self.cx.filename = orig_filename;
605-
self.cx.mod_path_stack = orig_mod_path_stack;
613+
self.cx.directory = directory;
606614
}
607615
} else {
608616
result = expand_item(item, self);
@@ -636,21 +644,6 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
636644
}
637645
}
638646

639-
impl<'a, 'b> MacroExpander<'a, 'b> {
640-
fn push_mod_path(&mut self, id: Ident, attrs: &[ast::Attribute]) {
641-
let default_path = id.name.as_str();
642-
let file_path = match ::attr::first_attr_value_str_by_name(attrs, "path") {
643-
Some(d) => d,
644-
None => default_path,
645-
};
646-
self.cx.mod_path_stack.push(file_path)
647-
}
648-
649-
fn pop_mod_path(&mut self) {
650-
self.cx.mod_path_stack.pop().unwrap();
651-
}
652-
}
653-
654647
pub struct ExpansionConfig<'feat> {
655648
pub crate_name: String,
656649
pub features: Option<&'feat Features>,

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
211211
imported_from,
212212
rhs);
213213
let mut p = Parser::new(cx.parse_sess(), cx.cfg(), Box::new(trncbr));
214-
p.filename = cx.filename.clone();
215-
p.mod_path_stack = cx.mod_path_stack.clone();
214+
p.directory = cx.directory.clone();
216215
p.restrictions = match cx.in_block {
217216
true => Restrictions::NO_NONINLINE_MOD,
218217
false => Restrictions::empty(),

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ pub struct Parser<'a> {
264264
/// extra detail when the same error is seen twice
265265
pub obsolete_set: HashSet<ObsoleteSyntax>,
266266
/// Used to determine the path to externally loaded source files
267-
pub filename: Option<String>,
268-
pub mod_path_stack: Vec<InternedString>,
267+
pub directory: PathBuf,
269268
/// Stack of open delimiters and their spans. Used for error message.
270269
pub open_braces: Vec<(token::DelimToken, Span)>,
271270
/// Flag if this parser "owns" the directory that it is currently parsing
@@ -346,9 +345,11 @@ impl<'a> Parser<'a> {
346345
{
347346
let tok0 = rdr.real_token();
348347
let span = tok0.sp;
349-
let filename = if span != syntax_pos::DUMMY_SP {
350-
Some(sess.codemap().span_to_filename(span))
351-
} else { None };
348+
let mut directory = match span {
349+
syntax_pos::DUMMY_SP => PathBuf::new(),
350+
_ => PathBuf::from(sess.codemap().span_to_filename(span)),
351+
};
352+
directory.pop();
352353
let placeholder = TokenAndSpan {
353354
tok: token::Underscore,
354355
sp: span,
@@ -377,8 +378,7 @@ impl<'a> Parser<'a> {
377378
quote_depth: 0,
378379
parsing_token_tree: false,
379380
obsolete_set: HashSet::new(),
380-
mod_path_stack: Vec::new(),
381-
filename: filename,
381+
directory: directory,
382382
open_braces: Vec::new(),
383383
owns_directory: true,
384384
root_module_name: None,
@@ -5306,27 +5306,24 @@ impl<'a> Parser<'a> {
53065306
let (m, attrs) = self.eval_src_mod(id, &outer_attrs, id_span)?;
53075307
Ok((id, m, Some(attrs)))
53085308
} else {
5309-
self.push_mod_path(id, &outer_attrs);
5309+
let directory = self.directory.clone();
5310+
self.push_directory(id, &outer_attrs);
53105311
self.expect(&token::OpenDelim(token::Brace))?;
53115312
let mod_inner_lo = self.span.lo;
53125313
let attrs = self.parse_inner_attributes()?;
53135314
let m = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
5314-
self.pop_mod_path();
5315+
self.directory = directory;
53155316
Ok((id, ItemKind::Mod(m), Some(attrs)))
53165317
}
53175318
}
53185319

5319-
fn push_mod_path(&mut self, id: Ident, attrs: &[Attribute]) {
5320+
fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
53205321
let default_path = self.id_to_interned_str(id);
53215322
let file_path = match ::attr::first_attr_value_str_by_name(attrs, "path") {
53225323
Some(d) => d,
53235324
None => default_path,
53245325
};
5325-
self.mod_path_stack.push(file_path)
5326-
}
5327-
5328-
fn pop_mod_path(&mut self) {
5329-
self.mod_path_stack.pop().unwrap();
5326+
self.directory.push(&*file_path)
53305327
}
53315328

53325329
pub fn submod_path_from_attr(attrs: &[ast::Attribute], dir_path: &Path) -> Option<PathBuf> {
@@ -5374,18 +5371,11 @@ impl<'a> Parser<'a> {
53745371
id: ast::Ident,
53755372
outer_attrs: &[ast::Attribute],
53765373
id_sp: Span) -> PResult<'a, ModulePathSuccess> {
5377-
let mut prefix = PathBuf::from(self.filename.as_ref().unwrap());
5378-
prefix.pop();
5379-
let mut dir_path = prefix;
5380-
for part in &self.mod_path_stack {
5381-
dir_path.push(&**part);
5382-
}
5383-
5384-
if let Some(p) = Parser::submod_path_from_attr(outer_attrs, &dir_path) {
5374+
if let Some(p) = Parser::submod_path_from_attr(outer_attrs, &self.directory) {
53855375
return Ok(ModulePathSuccess { path: p, owns_directory: true });
53865376
}
53875377

5388-
let paths = Parser::default_submod_path(id, &dir_path, self.sess.codemap());
5378+
let paths = Parser::default_submod_path(id, &self.directory, self.sess.codemap());
53895379

53905380
if self.restrictions.contains(Restrictions::NO_NONINLINE_MOD) {
53915381
let msg =
@@ -5400,8 +5390,8 @@ impl<'a> Parser<'a> {
54005390
} else if !self.owns_directory {
54015391
let mut err = self.diagnostic().struct_span_err(id_sp,
54025392
"cannot declare a new module at this location");
5403-
let this_module = match self.mod_path_stack.last() {
5404-
Some(name) => name.to_string(),
5393+
let this_module = match self.directory.file_name() {
5394+
Some(file_name) => file_name.to_str().unwrap().to_owned(),
54055395
None => self.root_module_name.as_ref().unwrap().clone(),
54065396
};
54075397
err.span_note(id_sp,

0 commit comments

Comments
 (0)