expand: Some more consistent naming in module loading

This commit is contained in:
Vadim Petrochenkov 2021-02-22 19:06:36 +03:00
parent 3d0b622ab7
commit 46b67aa74d
4 changed files with 59 additions and 57 deletions

View file

@ -4,7 +4,7 @@ use rustc_ast::token;
use rustc_ast::tokenstream::TokenStream;
use rustc_ast_pretty::pprust;
use rustc_expand::base::{self, *};
use rustc_expand::module::DirectoryOwnership;
use rustc_expand::module::DirOwnership;
use rustc_parse::parser::{ForceCollect, Parser};
use rustc_parse::{self, new_parser_from_file};
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
@ -116,7 +116,7 @@ pub fn expand_include<'cx>(
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
let dir_path = file.parent().unwrap_or(&file).to_owned();
cx.current_expansion.module = Rc::new(cx.current_expansion.module.with_dir_path(dir_path));
cx.current_expansion.directory_ownership = DirectoryOwnership::Owned { relative: None };
cx.current_expansion.dir_ownership = DirOwnership::Owned { relative: None };
struct ExpandResult<'a> {
p: Parser<'a>,

View file

@ -1,5 +1,5 @@
use crate::expand::{self, AstFragment, Invocation};
use crate::module::DirectoryOwnership;
use crate::module::DirOwnership;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Nonterminal};
@ -921,7 +921,7 @@ pub struct ExpansionData {
pub id: ExpnId,
pub depth: usize,
pub module: Rc<ModuleData>,
pub directory_ownership: DirectoryOwnership,
pub dir_ownership: DirOwnership,
pub prior_type_ascription: Option<(Span, bool)>,
}
@ -963,7 +963,7 @@ impl<'a> ExtCtxt<'a> {
id: ExpnId::root(),
depth: 0,
module: Default::default(),
directory_ownership: DirectoryOwnership::Owned { relative: None },
dir_ownership: DirOwnership::Owned { relative: None },
prior_type_ascription: None,
},
force_mode: false,

View file

@ -3,7 +3,7 @@ use crate::config::StripUnconfigured;
use crate::configure;
use crate::hygiene::SyntaxContext;
use crate::mbe::macro_rules::annotate_err_with_kind;
use crate::module::{parse_external_mod, push_directory, DirectoryOwnership, ParsedExternalMod};
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
use crate::placeholders::{placeholder, PlaceholderExpander};
use rustc_ast as ast;
@ -1246,10 +1246,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}
fn visit_block(&mut self, block: &mut P<Block>) {
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
let orig_dir_ownership = mem::replace(
&mut self.cx.current_expansion.dir_ownership,
DirOwnership::UnownedViaBlock,
);
noop_visit_block(block, self);
self.cx.current_expansion.directory_ownership = old_directory_ownership;
self.cx.current_expansion.dir_ownership = orig_dir_ownership;
}
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
@ -1280,12 +1282,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
let (file_path, dir_path, dir_ownership) = match mod_kind {
ModKind::Loaded(_, Inline::Yes, _) => {
// Inline `mod foo { ... }`, but we still need to push directories.
let (dir_path, dir_ownership) = push_directory(
let (dir_path, dir_ownership) = mod_dir_path(
&self.cx.sess,
ident,
&attrs,
&self.cx.current_expansion.module,
self.cx.current_expansion.directory_ownership,
self.cx.current_expansion.dir_ownership,
);
item.attrs = attrs;
(None, dir_path, dir_ownership)
@ -1306,7 +1308,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
ident,
span,
&self.cx.current_expansion.module,
self.cx.current_expansion.directory_ownership,
self.cx.current_expansion.dir_ownership,
&mut attrs,
);
@ -1334,12 +1336,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
let orig_module =
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
let orig_dir_ownership =
mem::replace(&mut self.cx.current_expansion.directory_ownership, dir_ownership);
mem::replace(&mut self.cx.current_expansion.dir_ownership, dir_ownership);
let result = noop_flat_map_item(item, self);
// Restore the module info.
self.cx.current_expansion.directory_ownership = orig_dir_ownership;
self.cx.current_expansion.dir_ownership = orig_dir_ownership;
self.cx.current_expansion.module = orig_module;
result

View file

@ -11,7 +11,7 @@ use rustc_span::Span;
use std::path::{self, Path, PathBuf};
#[derive(Copy, Clone)]
pub enum DirectoryOwnership {
pub enum DirOwnership {
Owned {
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
relative: Option<Ident>,
@ -29,8 +29,8 @@ pub struct ModulePath<'a> {
// Public for rustfmt usage.
pub struct ModulePathSuccess {
pub path: PathBuf,
pub ownership: DirectoryOwnership,
pub file_path: PathBuf,
pub dir_ownership: DirOwnership,
}
crate struct ParsedExternalMod {
@ -38,31 +38,31 @@ crate struct ParsedExternalMod {
pub inner_span: Span,
pub file_path: PathBuf,
pub dir_path: PathBuf,
pub dir_ownership: DirectoryOwnership,
pub dir_ownership: DirOwnership,
}
crate fn parse_external_mod(
sess: &Session,
id: Ident,
ident: Ident,
span: Span, // The span to blame on errors.
module: &ModuleData,
mut dir_ownership: DirectoryOwnership,
mut dir_ownership: DirOwnership,
attrs: &mut Vec<Attribute>,
) -> ParsedExternalMod {
// 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.
let mp = submod_path(sess, id, span, &attrs, dir_ownership, &module.dir_path)?;
dir_ownership = mp.ownership;
let mp = mod_file_path(sess, ident, span, &attrs, &module.dir_path, dir_ownership)?;
dir_ownership = mp.dir_ownership;
// Ensure file paths are acyclic.
error_on_circular_module(&sess.parse_sess, span, &mp.path, &module.file_path_stack)?;
error_on_circular_module(&sess.parse_sess, span, &mp.file_path, &module.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 parser = new_parser_from_file(&sess.parse_sess, &mp.file_path, Some(span));
let (mut inner_attrs, items, inner_span) = parser.parse_mod(&token::Eof)?;
attrs.append(&mut inner_attrs);
(items, inner_span, mp.path)
(items, inner_span, mp.file_path)
};
// (1) ...instead, we return a dummy module.
let (items, inner_span, file_path) = result.map_err(|mut err| err.emit()).unwrap_or_default();
@ -76,32 +76,32 @@ crate fn parse_external_mod(
fn error_on_circular_module<'a>(
sess: &'a ParseSess,
span: Span,
path: &Path,
file_path: &Path,
file_path_stack: &[PathBuf],
) -> PResult<'a, ()> {
if let Some(i) = file_path_stack.iter().position(|p| *p == path) {
if let Some(i) = file_path_stack.iter().position(|p| *p == file_path) {
let mut err = String::from("circular modules: ");
for p in &file_path_stack[i..] {
err.push_str(&p.to_string_lossy());
err.push_str(" -> ");
}
err.push_str(&path.to_string_lossy());
err.push_str(&file_path.to_string_lossy());
return Err(sess.span_diagnostic.struct_span_err(span, &err[..]));
}
Ok(())
}
crate fn push_directory(
crate fn mod_dir_path(
sess: &Session,
id: Ident,
ident: Ident,
attrs: &[Attribute],
module: &ModuleData,
mut dir_ownership: DirectoryOwnership,
) -> (PathBuf, DirectoryOwnership) {
mut dir_ownership: DirOwnership,
) -> (PathBuf, DirOwnership) {
let mut dir_path = module.dir_path.clone();
if let Some(file_path) = sess.first_attr_value_str_by_name(attrs, sym::path) {
dir_path.push(&*file_path.as_str());
dir_ownership = DirectoryOwnership::Owned { relative: None };
dir_ownership = DirOwnership::Owned { relative: None };
} else {
// We have to push on the current module name in the case of relative
// paths in order to ensure that any additional module paths from inline
@ -109,27 +109,27 @@ crate fn push_directory(
//
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
// directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
if let DirectoryOwnership::Owned { relative } = &mut dir_ownership {
if let DirOwnership::Owned { relative } = &mut dir_ownership {
if let Some(ident) = relative.take() {
// Remove the relative offset.
dir_path.push(&*ident.as_str());
}
}
dir_path.push(&*id.as_str());
dir_path.push(&*ident.as_str());
}
(dir_path, dir_ownership)
}
fn submod_path<'a>(
fn mod_file_path<'a>(
sess: &'a Session,
id: Ident,
ident: Ident,
span: Span,
attrs: &[Attribute],
ownership: DirectoryOwnership,
dir_path: &Path,
dir_ownership: DirOwnership,
) -> PResult<'a, ModulePathSuccess> {
if let Some(path) = submod_path_from_attr(sess, attrs, dir_path) {
if let Some(file_path) = mod_file_path_from_attr(sess, attrs, dir_path) {
// All `#[path]` files are treated as though they are a `mod.rs` file.
// This means that `mod foo;` declarations inside `#[path]`-included
// files are siblings,
@ -137,19 +137,19 @@ fn submod_path<'a>(
// Note that this will produce weirdness when a file named `foo.rs` is
// `#[path]` included and contains a `mod foo;` declaration.
// If you encounter this, it's your own darn fault :P
let ownership = DirectoryOwnership::Owned { relative: None };
return Ok(ModulePathSuccess { ownership, path });
let dir_ownership = DirOwnership::Owned { relative: None };
return Ok(ModulePathSuccess { file_path, dir_ownership });
}
let relative = match ownership {
DirectoryOwnership::Owned { relative } => relative,
DirectoryOwnership::UnownedViaBlock => None,
let relative = match dir_ownership {
DirOwnership::Owned { relative } => relative,
DirOwnership::UnownedViaBlock => None,
};
let ModulePath { path_exists, name, result } =
default_submod_path(&sess.parse_sess, id, span, relative, dir_path);
match ownership {
DirectoryOwnership::Owned { .. } => Ok(result?),
DirectoryOwnership::UnownedViaBlock => {
default_submod_path(&sess.parse_sess, ident, span, relative, dir_path);
match dir_ownership {
DirOwnership::Owned { .. } => Ok(result?),
DirOwnership::UnownedViaBlock => {
let _ = result.map_err(|mut err| err.cancel());
error_decl_mod_in_block(&sess.parse_sess, span, path_exists, &name)
}
@ -173,7 +173,7 @@ fn error_decl_mod_in_block<'a, T>(
/// Derive a submodule path from the first found `#[path = "path_string"]`.
/// The provided `dir_path` is joined with the `path_string`.
pub(super) fn submod_path_from_attr(
fn mod_file_path_from_attr(
sess: &Session,
attrs: &[Attribute],
dir_path: &Path,
@ -196,15 +196,15 @@ pub(super) fn submod_path_from_attr(
// Public for rustfmt usage.
pub fn default_submod_path<'a>(
sess: &'a ParseSess,
id: Ident,
ident: Ident,
span: Span,
relative: Option<Ident>,
dir_path: &Path,
) -> ModulePath<'a> {
// If we're in a foo.rs file instead of a mod.rs file,
// we need to look for submodules in
// `./foo/<id>.rs` and `./foo/<id>/mod.rs` rather than
// `./<id>.rs` and `./<id>/mod.rs`.
// `./foo/<ident>.rs` and `./foo/<ident>/mod.rs` rather than
// `./<ident>.rs` and `./<ident>/mod.rs`.
let relative_prefix_string;
let relative_prefix = if let Some(ident) = relative {
relative_prefix_string = format!("{}{}", ident.name, path::MAIN_SEPARATOR);
@ -213,7 +213,7 @@ pub fn default_submod_path<'a>(
""
};
let mod_name = id.name.to_string();
let mod_name = ident.name.to_string();
let default_path_str = format!("{}{}.rs", relative_prefix, mod_name);
let secondary_path_str =
format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);
@ -224,12 +224,12 @@ pub fn default_submod_path<'a>(
let result = match (default_exists, secondary_exists) {
(true, false) => Ok(ModulePathSuccess {
path: default_path,
ownership: DirectoryOwnership::Owned { relative: Some(id) },
file_path: default_path,
dir_ownership: DirOwnership::Owned { relative: Some(ident) },
}),
(false, true) => Ok(ModulePathSuccess {
path: secondary_path,
ownership: DirectoryOwnership::Owned { relative: None },
file_path: secondary_path,
dir_ownership: DirOwnership::Owned { relative: None },
}),
(false, false) => {
let mut err = struct_span_err!(