expand: Move module file path stack from global session to expansion data

Also don't push the paths on the stack directly in `fn parse_external_mod`, return them instead.
This commit is contained in:
Vadim Petrochenkov 2021-02-21 19:15:43 +03:00
parent bc18eb4717
commit 39052c55bb
7 changed files with 80 additions and 70 deletions

View file

@ -42,10 +42,10 @@ crate fn parse_external_mod(
sess: &Session,
id: Ident,
span: Span, // The span to blame on errors.
file_path_stack: &[PathBuf],
Directory { mut ownership, path }: Directory,
attrs: &mut Vec<Attribute>,
pop_mod_stack: &mut bool,
) -> (Vec<P<Item>>, Span, Directory) {
) -> (Vec<P<Item>>, Span, PathBuf, Directory) {
// We bail on the first error, but that error does not cause a fatal error... (1)
let result: PResult<'_, _> = try {
// Extract the file path and the new ownership.
@ -53,20 +53,16 @@ crate fn parse_external_mod(
ownership = mp.ownership;
// Ensure file paths are acyclic.
let mut included_mod_stack = sess.parse_sess.included_mod_stack.borrow_mut();
error_on_circular_module(&sess.parse_sess, span, &mp.path, &included_mod_stack)?;
included_mod_stack.push(mp.path.clone());
*pop_mod_stack = true; // We have pushed, so notify caller.
drop(included_mod_stack);
error_on_circular_module(&sess.parse_sess, span, &mp.path, file_path_stack)?;
// Actually parse the external file as a module.
let mut parser = new_parser_from_file(&sess.parse_sess, &mp.path, Some(span));
let (mut inner_attrs, items, inner_span) = parser.parse_mod(&token::Eof)?;
attrs.append(&mut inner_attrs);
(items, inner_span)
(items, inner_span, mp.path)
};
// (1) ...instead, we return a dummy module.
let (items, inner_span) = result.map_err(|mut err| err.emit()).unwrap_or_default();
let (items, inner_span, file_path) = result.map_err(|mut err| err.emit()).unwrap_or_default();
// Extract the directory path for submodules of the module.
let path = sess.source_map().span_to_unmapped_path(inner_span);
@ -76,18 +72,18 @@ crate fn parse_external_mod(
};
path.pop();
(items, inner_span, Directory { ownership, path })
(items, inner_span, file_path, Directory { ownership, path })
}
fn error_on_circular_module<'a>(
sess: &'a ParseSess,
span: Span,
path: &Path,
included_mod_stack: &[PathBuf],
file_path_stack: &[PathBuf],
) -> PResult<'a, ()> {
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
if let Some(i) = file_path_stack.iter().position(|p| *p == path) {
let mut err = String::from("circular modules: ");
for p in &included_mod_stack[i..] {
for p in &file_path_stack[i..] {
err.push_str(&p.to_string_lossy());
err.push_str(" -> ");
}