expand: Some more consistent naming in module loading
This commit is contained in:
parent
3d0b622ab7
commit
46b67aa74d
@ -4,7 +4,7 @@
|
|||||||
use rustc_ast::tokenstream::TokenStream;
|
use rustc_ast::tokenstream::TokenStream;
|
||||||
use rustc_ast_pretty::pprust;
|
use rustc_ast_pretty::pprust;
|
||||||
use rustc_expand::base::{self, *};
|
use rustc_expand::base::{self, *};
|
||||||
use rustc_expand::module::DirectoryOwnership;
|
use rustc_expand::module::DirOwnership;
|
||||||
use rustc_parse::parser::{ForceCollect, Parser};
|
use rustc_parse::parser::{ForceCollect, Parser};
|
||||||
use rustc_parse::{self, new_parser_from_file};
|
use rustc_parse::{self, new_parser_from_file};
|
||||||
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
|
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.
|
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
|
||||||
let dir_path = file.parent().unwrap_or(&file).to_owned();
|
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.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> {
|
struct ExpandResult<'a> {
|
||||||
p: Parser<'a>,
|
p: Parser<'a>,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::expand::{self, AstFragment, Invocation};
|
use crate::expand::{self, AstFragment, Invocation};
|
||||||
use crate::module::DirectoryOwnership;
|
use crate::module::DirOwnership;
|
||||||
|
|
||||||
use rustc_ast::ptr::P;
|
use rustc_ast::ptr::P;
|
||||||
use rustc_ast::token::{self, Nonterminal};
|
use rustc_ast::token::{self, Nonterminal};
|
||||||
@ -921,7 +921,7 @@ pub struct ExpansionData {
|
|||||||
pub id: ExpnId,
|
pub id: ExpnId,
|
||||||
pub depth: usize,
|
pub depth: usize,
|
||||||
pub module: Rc<ModuleData>,
|
pub module: Rc<ModuleData>,
|
||||||
pub directory_ownership: DirectoryOwnership,
|
pub dir_ownership: DirOwnership,
|
||||||
pub prior_type_ascription: Option<(Span, bool)>,
|
pub prior_type_ascription: Option<(Span, bool)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -963,7 +963,7 @@ pub fn new(
|
|||||||
id: ExpnId::root(),
|
id: ExpnId::root(),
|
||||||
depth: 0,
|
depth: 0,
|
||||||
module: Default::default(),
|
module: Default::default(),
|
||||||
directory_ownership: DirectoryOwnership::Owned { relative: None },
|
dir_ownership: DirOwnership::Owned { relative: None },
|
||||||
prior_type_ascription: None,
|
prior_type_ascription: None,
|
||||||
},
|
},
|
||||||
force_mode: false,
|
force_mode: false,
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use crate::configure;
|
use crate::configure;
|
||||||
use crate::hygiene::SyntaxContext;
|
use crate::hygiene::SyntaxContext;
|
||||||
use crate::mbe::macro_rules::annotate_err_with_kind;
|
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 crate::placeholders::{placeholder, PlaceholderExpander};
|
||||||
|
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
@ -1246,10 +1246,12 @@ fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn visit_block(&mut self, block: &mut P<Block>) {
|
fn visit_block(&mut self, block: &mut P<Block>) {
|
||||||
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
|
let orig_dir_ownership = mem::replace(
|
||||||
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
|
&mut self.cx.current_expansion.dir_ownership,
|
||||||
|
DirOwnership::UnownedViaBlock,
|
||||||
|
);
|
||||||
noop_visit_block(block, self);
|
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]> {
|
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
||||||
@ -1280,12 +1282,12 @@ fn visit_block(&mut self, block: &mut P<Block>) {
|
|||||||
let (file_path, dir_path, dir_ownership) = match mod_kind {
|
let (file_path, dir_path, dir_ownership) = match mod_kind {
|
||||||
ModKind::Loaded(_, Inline::Yes, _) => {
|
ModKind::Loaded(_, Inline::Yes, _) => {
|
||||||
// Inline `mod foo { ... }`, but we still need to push directories.
|
// 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,
|
&self.cx.sess,
|
||||||
ident,
|
ident,
|
||||||
&attrs,
|
&attrs,
|
||||||
&self.cx.current_expansion.module,
|
&self.cx.current_expansion.module,
|
||||||
self.cx.current_expansion.directory_ownership,
|
self.cx.current_expansion.dir_ownership,
|
||||||
);
|
);
|
||||||
item.attrs = attrs;
|
item.attrs = attrs;
|
||||||
(None, dir_path, dir_ownership)
|
(None, dir_path, dir_ownership)
|
||||||
@ -1306,7 +1308,7 @@ fn visit_block(&mut self, block: &mut P<Block>) {
|
|||||||
ident,
|
ident,
|
||||||
span,
|
span,
|
||||||
&self.cx.current_expansion.module,
|
&self.cx.current_expansion.module,
|
||||||
self.cx.current_expansion.directory_ownership,
|
self.cx.current_expansion.dir_ownership,
|
||||||
&mut attrs,
|
&mut attrs,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1334,12 +1336,12 @@ fn visit_block(&mut self, block: &mut P<Block>) {
|
|||||||
let orig_module =
|
let orig_module =
|
||||||
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
|
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
|
||||||
let orig_dir_ownership =
|
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);
|
let result = noop_flat_map_item(item, self);
|
||||||
|
|
||||||
// Restore the module info.
|
// 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;
|
self.cx.current_expansion.module = orig_module;
|
||||||
|
|
||||||
result
|
result
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
use std::path::{self, Path, PathBuf};
|
use std::path::{self, Path, PathBuf};
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub enum DirectoryOwnership {
|
pub enum DirOwnership {
|
||||||
Owned {
|
Owned {
|
||||||
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
|
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
|
||||||
relative: Option<Ident>,
|
relative: Option<Ident>,
|
||||||
@ -29,8 +29,8 @@ pub struct ModulePath<'a> {
|
|||||||
|
|
||||||
// Public for rustfmt usage.
|
// Public for rustfmt usage.
|
||||||
pub struct ModulePathSuccess {
|
pub struct ModulePathSuccess {
|
||||||
pub path: PathBuf,
|
pub file_path: PathBuf,
|
||||||
pub ownership: DirectoryOwnership,
|
pub dir_ownership: DirOwnership,
|
||||||
}
|
}
|
||||||
|
|
||||||
crate struct ParsedExternalMod {
|
crate struct ParsedExternalMod {
|
||||||
@ -38,31 +38,31 @@ pub struct ModulePathSuccess {
|
|||||||
pub inner_span: Span,
|
pub inner_span: Span,
|
||||||
pub file_path: PathBuf,
|
pub file_path: PathBuf,
|
||||||
pub dir_path: PathBuf,
|
pub dir_path: PathBuf,
|
||||||
pub dir_ownership: DirectoryOwnership,
|
pub dir_ownership: DirOwnership,
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn parse_external_mod(
|
crate fn parse_external_mod(
|
||||||
sess: &Session,
|
sess: &Session,
|
||||||
id: Ident,
|
ident: Ident,
|
||||||
span: Span, // The span to blame on errors.
|
span: Span, // The span to blame on errors.
|
||||||
module: &ModuleData,
|
module: &ModuleData,
|
||||||
mut dir_ownership: DirectoryOwnership,
|
mut dir_ownership: DirOwnership,
|
||||||
attrs: &mut Vec<Attribute>,
|
attrs: &mut Vec<Attribute>,
|
||||||
) -> ParsedExternalMod {
|
) -> ParsedExternalMod {
|
||||||
// We bail on the first error, but that error does not cause a fatal error... (1)
|
// We bail on the first error, but that error does not cause a fatal error... (1)
|
||||||
let result: PResult<'_, _> = try {
|
let result: PResult<'_, _> = try {
|
||||||
// Extract the file path and the new ownership.
|
// Extract the file path and the new ownership.
|
||||||
let mp = submod_path(sess, id, span, &attrs, dir_ownership, &module.dir_path)?;
|
let mp = mod_file_path(sess, ident, span, &attrs, &module.dir_path, dir_ownership)?;
|
||||||
dir_ownership = mp.ownership;
|
dir_ownership = mp.dir_ownership;
|
||||||
|
|
||||||
// Ensure file paths are acyclic.
|
// 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.
|
// 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)?;
|
let (mut inner_attrs, items, inner_span) = parser.parse_mod(&token::Eof)?;
|
||||||
attrs.append(&mut inner_attrs);
|
attrs.append(&mut inner_attrs);
|
||||||
(items, inner_span, mp.path)
|
(items, inner_span, mp.file_path)
|
||||||
};
|
};
|
||||||
// (1) ...instead, we return a dummy module.
|
// (1) ...instead, we return a dummy module.
|
||||||
let (items, inner_span, file_path) = 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();
|
||||||
@ -76,32 +76,32 @@ pub struct ModulePathSuccess {
|
|||||||
fn error_on_circular_module<'a>(
|
fn error_on_circular_module<'a>(
|
||||||
sess: &'a ParseSess,
|
sess: &'a ParseSess,
|
||||||
span: Span,
|
span: Span,
|
||||||
path: &Path,
|
file_path: &Path,
|
||||||
file_path_stack: &[PathBuf],
|
file_path_stack: &[PathBuf],
|
||||||
) -> PResult<'a, ()> {
|
) -> 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: ");
|
let mut err = String::from("circular modules: ");
|
||||||
for p in &file_path_stack[i..] {
|
for p in &file_path_stack[i..] {
|
||||||
err.push_str(&p.to_string_lossy());
|
err.push_str(&p.to_string_lossy());
|
||||||
err.push_str(" -> ");
|
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[..]));
|
return Err(sess.span_diagnostic.struct_span_err(span, &err[..]));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn push_directory(
|
crate fn mod_dir_path(
|
||||||
sess: &Session,
|
sess: &Session,
|
||||||
id: Ident,
|
ident: Ident,
|
||||||
attrs: &[Attribute],
|
attrs: &[Attribute],
|
||||||
module: &ModuleData,
|
module: &ModuleData,
|
||||||
mut dir_ownership: DirectoryOwnership,
|
mut dir_ownership: DirOwnership,
|
||||||
) -> (PathBuf, DirectoryOwnership) {
|
) -> (PathBuf, DirOwnership) {
|
||||||
let mut dir_path = module.dir_path.clone();
|
let mut dir_path = module.dir_path.clone();
|
||||||
if let Some(file_path) = sess.first_attr_value_str_by_name(attrs, sym::path) {
|
if let Some(file_path) = sess.first_attr_value_str_by_name(attrs, sym::path) {
|
||||||
dir_path.push(&*file_path.as_str());
|
dir_path.push(&*file_path.as_str());
|
||||||
dir_ownership = DirectoryOwnership::Owned { relative: None };
|
dir_ownership = DirOwnership::Owned { relative: None };
|
||||||
} else {
|
} else {
|
||||||
// We have to push on the current module name in the case of relative
|
// 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
|
// paths in order to ensure that any additional module paths from inline
|
||||||
@ -109,27 +109,27 @@ fn error_on_circular_module<'a>(
|
|||||||
//
|
//
|
||||||
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
|
// 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`.
|
// 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() {
|
if let Some(ident) = relative.take() {
|
||||||
// Remove the relative offset.
|
// Remove the relative offset.
|
||||||
dir_path.push(&*ident.as_str());
|
dir_path.push(&*ident.as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dir_path.push(&*id.as_str());
|
dir_path.push(&*ident.as_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
(dir_path, dir_ownership)
|
(dir_path, dir_ownership)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submod_path<'a>(
|
fn mod_file_path<'a>(
|
||||||
sess: &'a Session,
|
sess: &'a Session,
|
||||||
id: Ident,
|
ident: Ident,
|
||||||
span: Span,
|
span: Span,
|
||||||
attrs: &[Attribute],
|
attrs: &[Attribute],
|
||||||
ownership: DirectoryOwnership,
|
|
||||||
dir_path: &Path,
|
dir_path: &Path,
|
||||||
|
dir_ownership: DirOwnership,
|
||||||
) -> PResult<'a, ModulePathSuccess> {
|
) -> 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.
|
// All `#[path]` files are treated as though they are a `mod.rs` file.
|
||||||
// This means that `mod foo;` declarations inside `#[path]`-included
|
// This means that `mod foo;` declarations inside `#[path]`-included
|
||||||
// files are siblings,
|
// files are siblings,
|
||||||
@ -137,19 +137,19 @@ fn submod_path<'a>(
|
|||||||
// Note that this will produce weirdness when a file named `foo.rs` is
|
// Note that this will produce weirdness when a file named `foo.rs` is
|
||||||
// `#[path]` included and contains a `mod foo;` declaration.
|
// `#[path]` included and contains a `mod foo;` declaration.
|
||||||
// If you encounter this, it's your own darn fault :P
|
// If you encounter this, it's your own darn fault :P
|
||||||
let ownership = DirectoryOwnership::Owned { relative: None };
|
let dir_ownership = DirOwnership::Owned { relative: None };
|
||||||
return Ok(ModulePathSuccess { ownership, path });
|
return Ok(ModulePathSuccess { file_path, dir_ownership });
|
||||||
}
|
}
|
||||||
|
|
||||||
let relative = match ownership {
|
let relative = match dir_ownership {
|
||||||
DirectoryOwnership::Owned { relative } => relative,
|
DirOwnership::Owned { relative } => relative,
|
||||||
DirectoryOwnership::UnownedViaBlock => None,
|
DirOwnership::UnownedViaBlock => None,
|
||||||
};
|
};
|
||||||
let ModulePath { path_exists, name, result } =
|
let ModulePath { path_exists, name, result } =
|
||||||
default_submod_path(&sess.parse_sess, id, span, relative, dir_path);
|
default_submod_path(&sess.parse_sess, ident, span, relative, dir_path);
|
||||||
match ownership {
|
match dir_ownership {
|
||||||
DirectoryOwnership::Owned { .. } => Ok(result?),
|
DirOwnership::Owned { .. } => Ok(result?),
|
||||||
DirectoryOwnership::UnownedViaBlock => {
|
DirOwnership::UnownedViaBlock => {
|
||||||
let _ = result.map_err(|mut err| err.cancel());
|
let _ = result.map_err(|mut err| err.cancel());
|
||||||
error_decl_mod_in_block(&sess.parse_sess, span, path_exists, &name)
|
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"]`.
|
/// Derive a submodule path from the first found `#[path = "path_string"]`.
|
||||||
/// The provided `dir_path` is joined with the `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,
|
sess: &Session,
|
||||||
attrs: &[Attribute],
|
attrs: &[Attribute],
|
||||||
dir_path: &Path,
|
dir_path: &Path,
|
||||||
@ -196,15 +196,15 @@ pub(super) fn submod_path_from_attr(
|
|||||||
// Public for rustfmt usage.
|
// Public for rustfmt usage.
|
||||||
pub fn default_submod_path<'a>(
|
pub fn default_submod_path<'a>(
|
||||||
sess: &'a ParseSess,
|
sess: &'a ParseSess,
|
||||||
id: Ident,
|
ident: Ident,
|
||||||
span: Span,
|
span: Span,
|
||||||
relative: Option<Ident>,
|
relative: Option<Ident>,
|
||||||
dir_path: &Path,
|
dir_path: &Path,
|
||||||
) -> ModulePath<'a> {
|
) -> ModulePath<'a> {
|
||||||
// If we're in a foo.rs file instead of a mod.rs file,
|
// If we're in a foo.rs file instead of a mod.rs file,
|
||||||
// we need to look for submodules in
|
// we need to look for submodules in
|
||||||
// `./foo/<id>.rs` and `./foo/<id>/mod.rs` rather than
|
// `./foo/<ident>.rs` and `./foo/<ident>/mod.rs` rather than
|
||||||
// `./<id>.rs` and `./<id>/mod.rs`.
|
// `./<ident>.rs` and `./<ident>/mod.rs`.
|
||||||
let relative_prefix_string;
|
let relative_prefix_string;
|
||||||
let relative_prefix = if let Some(ident) = relative {
|
let relative_prefix = if let Some(ident) = relative {
|
||||||
relative_prefix_string = format!("{}{}", ident.name, path::MAIN_SEPARATOR);
|
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 default_path_str = format!("{}{}.rs", relative_prefix, mod_name);
|
||||||
let secondary_path_str =
|
let secondary_path_str =
|
||||||
format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);
|
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) {
|
let result = match (default_exists, secondary_exists) {
|
||||||
(true, false) => Ok(ModulePathSuccess {
|
(true, false) => Ok(ModulePathSuccess {
|
||||||
path: default_path,
|
file_path: default_path,
|
||||||
ownership: DirectoryOwnership::Owned { relative: Some(id) },
|
dir_ownership: DirOwnership::Owned { relative: Some(ident) },
|
||||||
}),
|
}),
|
||||||
(false, true) => Ok(ModulePathSuccess {
|
(false, true) => Ok(ModulePathSuccess {
|
||||||
path: secondary_path,
|
file_path: secondary_path,
|
||||||
ownership: DirectoryOwnership::Owned { relative: None },
|
dir_ownership: DirOwnership::Owned { relative: None },
|
||||||
}),
|
}),
|
||||||
(false, false) => {
|
(false, false) => {
|
||||||
let mut err = struct_span_err!(
|
let mut err = struct_span_err!(
|
||||||
|
Loading…
Reference in New Issue
Block a user