2019-06-08 18:47:18 +09:00
|
|
|
use std::borrow::Cow;
|
2017-03-21 16:00:33 -04:00
|
|
|
use std::collections::BTreeMap;
|
2017-12-24 00:28:58 +09:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-26 12:55:25 +02:00
|
|
|
|
2020-03-27 22:29:12 -05:00
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_ast::visit::Visitor;
|
2021-03-14 13:44:02 -05:00
|
|
|
use rustc_ast::AstLike;
|
2020-05-19 17:31:28 +09:00
|
|
|
use rustc_span::symbol::{self, sym, Symbol};
|
2021-03-28 16:25:30 -05:00
|
|
|
use rustc_span::Span;
|
2020-06-11 21:11:18 -05:00
|
|
|
use thiserror::Error;
|
2015-07-26 12:55:25 +02:00
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
use crate::attr::MetaVisitor;
|
2019-02-04 13:30:43 +03:00
|
|
|
use crate::config::FileName;
|
2019-03-17 12:25:59 +09:00
|
|
|
use crate::items::is_mod_decl;
|
2021-12-29 20:49:39 -06:00
|
|
|
use crate::parse::parser::{
|
2021-03-15 20:07:44 -05:00
|
|
|
Directory, DirectoryOwnership, ModError, ModulePathSuccess, Parser, ParserError,
|
2020-06-11 21:11:18 -05:00
|
|
|
};
|
2021-12-29 20:49:39 -06:00
|
|
|
use crate::parse::session::ParseSess;
|
2021-11-07 20:37:34 -06:00
|
|
|
use crate::utils::{contains_skip, mk_sp};
|
2017-07-13 18:42:14 +09:00
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
mod visitor;
|
|
|
|
|
2020-09-02 18:42:00 -05:00
|
|
|
type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
|
2019-03-17 12:25:59 +09:00
|
|
|
|
2020-09-02 18:42:00 -05:00
|
|
|
/// Represents module with its inner attributes.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub(crate) struct Module<'a> {
|
2021-03-28 16:25:30 -05:00
|
|
|
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
|
|
|
|
pub(crate) items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
|
2020-09-02 18:42:00 -05:00
|
|
|
inner_attr: Vec<ast::Attribute>,
|
2021-03-28 16:25:30 -05:00
|
|
|
pub(crate) span: Span,
|
2020-09-02 18:42:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Module<'a> {
|
2021-03-28 16:25:30 -05:00
|
|
|
pub(crate) fn new(
|
|
|
|
mod_span: Span,
|
|
|
|
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
|
|
|
|
mod_items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
|
|
|
|
mod_attrs: Cow<'a, Vec<ast::Attribute>>,
|
|
|
|
) -> Self {
|
|
|
|
let inner_attr = mod_attrs
|
2020-09-02 18:42:00 -05:00
|
|
|
.iter()
|
|
|
|
.filter(|attr| attr.style == ast::AttrStyle::Inner)
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
Module {
|
2021-03-28 16:25:30 -05:00
|
|
|
items: mod_items,
|
2020-09-02 18:42:00 -05:00
|
|
|
inner_attr,
|
2021-03-28 16:25:30 -05:00
|
|
|
span: mod_span,
|
|
|
|
ast_mod_kind,
|
2020-09-02 18:42:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 13:44:02 -05:00
|
|
|
impl<'a> AstLike for Module<'a> {
|
2021-05-02 10:03:25 -05:00
|
|
|
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = true;
|
2020-09-02 18:42:00 -05:00
|
|
|
fn attrs(&self) -> &[ast::Attribute] {
|
|
|
|
&self.inner_attr
|
|
|
|
}
|
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<ast::Attribute>)) {
|
|
|
|
f(&mut self.inner_attr)
|
|
|
|
}
|
2021-03-15 20:07:44 -05:00
|
|
|
fn tokens_mut(&mut self) -> Option<&mut Option<rustc_ast::tokenstream::LazyTokenStream>> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2020-09-02 18:42:00 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 12:25:59 +09:00
|
|
|
/// Maps each module to the corresponding file.
|
2019-06-08 18:47:18 +09:00
|
|
|
pub(crate) struct ModResolver<'ast, 'sess> {
|
|
|
|
parse_sess: &'sess ParseSess,
|
2019-03-17 12:25:59 +09:00
|
|
|
directory: Directory,
|
2019-06-08 18:47:18 +09:00
|
|
|
file_map: FileModMap<'ast>,
|
2019-06-07 14:55:41 +09:00
|
|
|
recursive: bool,
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:11:18 -05:00
|
|
|
/// Represents errors while trying to resolve modules.
|
|
|
|
#[derive(Debug, Error)]
|
2021-03-18 18:07:54 +09:00
|
|
|
#[error("failed to resolve mod `{module}`: {kind}")]
|
2020-06-11 21:11:18 -05:00
|
|
|
pub struct ModuleResolutionError {
|
2020-06-11 21:49:40 -05:00
|
|
|
pub(crate) module: String,
|
|
|
|
pub(crate) kind: ModuleResolutionErrorKind,
|
2020-06-11 21:11:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub(crate) enum ModuleResolutionErrorKind {
|
|
|
|
/// Find a file that cannot be parsed.
|
|
|
|
#[error("cannot parse {file}")]
|
|
|
|
ParseError { file: PathBuf },
|
|
|
|
/// File cannot be found.
|
|
|
|
#[error("{file} does not exist")]
|
|
|
|
NotFound { file: PathBuf },
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum SubModKind<'a, 'ast> {
|
2019-06-08 18:47:18 +09:00
|
|
|
/// `mod foo;`
|
2020-09-02 18:42:00 -05:00
|
|
|
External(PathBuf, DirectoryOwnership, Module<'ast>),
|
2019-06-09 09:20:39 +09:00
|
|
|
/// `mod foo;` with multiple sources.
|
2020-09-02 18:42:00 -05:00
|
|
|
MultiExternal(Vec<(PathBuf, DirectoryOwnership, Module<'ast>)>),
|
2019-06-08 18:47:18 +09:00
|
|
|
/// `mod foo {}`
|
2019-06-09 09:20:39 +09:00
|
|
|
Internal(&'a ast::Item),
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
|
2019-03-17 12:25:59 +09:00
|
|
|
/// Creates a new `ModResolver`.
|
2019-05-09 20:37:51 +02:00
|
|
|
pub(crate) fn new(
|
2019-06-08 18:47:18 +09:00
|
|
|
parse_sess: &'sess ParseSess,
|
2019-03-17 12:25:59 +09:00
|
|
|
directory_ownership: DirectoryOwnership,
|
2019-06-07 14:55:41 +09:00
|
|
|
recursive: bool,
|
2019-03-17 12:25:59 +09:00
|
|
|
) -> Self {
|
|
|
|
ModResolver {
|
|
|
|
directory: Directory {
|
|
|
|
path: PathBuf::new(),
|
|
|
|
ownership: directory_ownership,
|
|
|
|
},
|
|
|
|
file_map: BTreeMap::new(),
|
2019-06-08 18:47:18 +09:00
|
|
|
parse_sess,
|
2019-06-07 14:55:41 +09:00
|
|
|
recursive,
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a map that maps a file name to the module in AST.
|
2019-06-08 18:47:18 +09:00
|
|
|
pub(crate) fn visit_crate(
|
|
|
|
mut self,
|
|
|
|
krate: &'ast ast::Crate,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<FileModMap<'ast>, ModuleResolutionError> {
|
2020-03-26 21:25:34 -05:00
|
|
|
let root_filename = self.parse_sess.span_to_filename(krate.span);
|
2019-03-17 12:25:59 +09:00
|
|
|
self.directory.path = match root_filename {
|
2020-03-26 21:25:34 -05:00
|
|
|
FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(),
|
2019-03-17 12:25:59 +09:00
|
|
|
_ => PathBuf::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Skip visiting sub modules when the input is from stdin.
|
2019-06-07 14:55:41 +09:00
|
|
|
if self.recursive {
|
2021-03-28 16:25:30 -05:00
|
|
|
self.visit_mod_from_ast(&krate.items)?;
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
|
2021-11-07 20:37:34 -06:00
|
|
|
let snippet_provider = self.parse_sess.snippet_provider(krate.span);
|
|
|
|
|
2020-09-02 18:42:00 -05:00
|
|
|
self.file_map.insert(
|
|
|
|
root_filename,
|
2021-03-28 16:25:30 -05:00
|
|
|
Module::new(
|
2021-11-07 20:37:34 -06:00
|
|
|
mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()),
|
2021-03-28 16:25:30 -05:00
|
|
|
None,
|
|
|
|
Cow::Borrowed(&krate.items),
|
|
|
|
Cow::Borrowed(&krate.attrs),
|
|
|
|
),
|
2020-09-02 18:42:00 -05:00
|
|
|
);
|
2019-03-17 12:25:59 +09:00
|
|
|
Ok(self.file_map)
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
/// Visit `cfg_if` macro and look for module declarations.
|
2020-06-11 21:11:18 -05:00
|
|
|
fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), ModuleResolutionError> {
|
2020-03-26 23:18:16 -05:00
|
|
|
let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess);
|
2019-06-08 18:47:18 +09:00
|
|
|
visitor.visit_item(&item);
|
|
|
|
for module_item in visitor.mods() {
|
2021-03-28 16:25:30 -05:00
|
|
|
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind {
|
2020-09-02 18:42:00 -05:00
|
|
|
self.visit_sub_mod(
|
|
|
|
&module_item.item,
|
2021-03-28 16:25:30 -05:00
|
|
|
Module::new(
|
|
|
|
module_item.item.span,
|
|
|
|
Some(Cow::Owned(sub_mod_kind.clone())),
|
|
|
|
Cow::Owned(vec![]),
|
|
|
|
Cow::Owned(vec![]),
|
|
|
|
),
|
2020-09-02 18:42:00 -05:00
|
|
|
)?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit modules defined inside macro calls.
|
2021-03-28 16:25:30 -05:00
|
|
|
fn visit_mod_outside_ast(
|
|
|
|
&mut self,
|
|
|
|
items: Vec<rustc_ast::ptr::P<ast::Item>>,
|
|
|
|
) -> Result<(), ModuleResolutionError> {
|
|
|
|
for item in items {
|
2019-06-09 09:20:39 +09:00
|
|
|
if is_cfg_if(&item) {
|
|
|
|
self.visit_cfg_if(Cow::Owned(item.into_inner()))?;
|
|
|
|
continue;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
|
2021-03-28 16:25:30 -05:00
|
|
|
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
|
|
|
|
let span = item.span;
|
|
|
|
self.visit_sub_mod(
|
|
|
|
&item,
|
|
|
|
Module::new(
|
|
|
|
span,
|
|
|
|
Some(Cow::Owned(sub_mod_kind.clone())),
|
|
|
|
Cow::Owned(vec![]),
|
|
|
|
Cow::Owned(vec![]),
|
|
|
|
),
|
|
|
|
)?;
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
/// Visit modules from AST.
|
2021-03-28 16:25:30 -05:00
|
|
|
fn visit_mod_from_ast(
|
|
|
|
&mut self,
|
2021-11-07 20:37:34 -06:00
|
|
|
items: &'ast [rustc_ast::ptr::P<ast::Item>],
|
2021-03-28 16:25:30 -05:00
|
|
|
) -> Result<(), ModuleResolutionError> {
|
|
|
|
for item in items {
|
2019-06-09 09:20:39 +09:00
|
|
|
if is_cfg_if(item) {
|
|
|
|
self.visit_cfg_if(Cow::Borrowed(item))?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
|
2021-03-28 16:25:30 -05:00
|
|
|
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
|
|
|
|
let span = item.span;
|
|
|
|
self.visit_sub_mod(
|
|
|
|
item,
|
|
|
|
Module::new(
|
|
|
|
span,
|
|
|
|
Some(Cow::Borrowed(sub_mod_kind)),
|
|
|
|
Cow::Owned(vec![]),
|
|
|
|
Cow::Borrowed(&item.attrs),
|
|
|
|
),
|
|
|
|
)?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_sub_mod(
|
|
|
|
&mut self,
|
|
|
|
item: &'c ast::Item,
|
2020-09-02 18:42:00 -05:00
|
|
|
sub_mod: Module<'ast>,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<(), ModuleResolutionError> {
|
2019-06-09 09:20:39 +09:00
|
|
|
let old_directory = self.directory.clone();
|
|
|
|
let sub_mod_kind = self.peek_sub_mod(item, &sub_mod)?;
|
|
|
|
if let Some(sub_mod_kind) = sub_mod_kind {
|
2020-09-02 18:42:00 -05:00
|
|
|
self.insert_sub_mod(sub_mod_kind.clone())?;
|
2019-06-09 09:20:39 +09:00
|
|
|
self.visit_sub_mod_inner(sub_mod, sub_mod_kind)?;
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
self.directory = old_directory;
|
2019-06-08 18:47:18 +09:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inspect the given sub-module which we are about to visit and returns its kind.
|
2019-06-09 09:20:39 +09:00
|
|
|
fn peek_sub_mod(
|
|
|
|
&self,
|
|
|
|
item: &'c ast::Item,
|
2020-09-02 18:42:00 -05:00
|
|
|
sub_mod: &Module<'ast>,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<Option<SubModKind<'c, 'ast>>, ModuleResolutionError> {
|
2019-06-08 18:47:18 +09:00
|
|
|
if contains_skip(&item.attrs) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_mod_decl(item) {
|
|
|
|
// mod foo;
|
|
|
|
// Look for an extern file.
|
2019-06-09 09:20:39 +09:00
|
|
|
self.find_external_module(item.ident, &item.attrs, sub_mod)
|
2019-06-08 18:47:18 +09:00
|
|
|
} else {
|
|
|
|
// An internal module (`mod foo { /* ... */ }`);
|
2020-05-22 11:55:34 +02:00
|
|
|
Ok(Some(SubModKind::Internal(item)))
|
2019-06-08 18:47:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
fn insert_sub_mod(
|
|
|
|
&mut self,
|
|
|
|
sub_mod_kind: SubModKind<'c, 'ast>,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<(), ModuleResolutionError> {
|
2019-06-09 09:20:39 +09:00
|
|
|
match sub_mod_kind {
|
2020-03-26 23:18:16 -05:00
|
|
|
SubModKind::External(mod_path, _, sub_mod) => {
|
|
|
|
self.file_map
|
|
|
|
.entry(FileName::Real(mod_path))
|
|
|
|
.or_insert(sub_mod);
|
2019-06-09 09:20:39 +09:00
|
|
|
}
|
|
|
|
SubModKind::MultiExternal(mods) => {
|
|
|
|
for (mod_path, _, sub_mod) in mods {
|
2020-03-26 23:18:16 -05:00
|
|
|
self.file_map
|
|
|
|
.entry(FileName::Real(mod_path))
|
|
|
|
.or_insert(sub_mod);
|
2019-06-09 09:20:39 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_sub_mod_inner(
|
|
|
|
&mut self,
|
2020-09-02 18:42:00 -05:00
|
|
|
sub_mod: Module<'ast>,
|
2019-06-09 09:20:39 +09:00
|
|
|
sub_mod_kind: SubModKind<'c, 'ast>,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<(), ModuleResolutionError> {
|
2019-06-09 09:20:39 +09:00
|
|
|
match sub_mod_kind {
|
2020-03-26 23:18:16 -05:00
|
|
|
SubModKind::External(mod_path, directory_ownership, sub_mod) => {
|
2019-06-09 09:20:39 +09:00
|
|
|
let directory = Directory {
|
|
|
|
path: mod_path.parent().unwrap().to_path_buf(),
|
|
|
|
ownership: directory_ownership,
|
|
|
|
};
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
|
|
|
|
}
|
2021-11-07 20:37:34 -06:00
|
|
|
SubModKind::Internal(item) => {
|
2019-06-09 09:20:39 +09:00
|
|
|
self.push_inline_mod_directory(item.ident, &item.attrs);
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, None)
|
|
|
|
}
|
|
|
|
SubModKind::MultiExternal(mods) => {
|
|
|
|
for (mod_path, directory_ownership, sub_mod) in mods {
|
|
|
|
let directory = Directory {
|
|
|
|
path: mod_path.parent().unwrap().to_path_buf(),
|
|
|
|
ownership: directory_ownership,
|
|
|
|
};
|
|
|
|
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_sub_mod_after_directory_update(
|
|
|
|
&mut self,
|
2020-09-02 18:42:00 -05:00
|
|
|
sub_mod: Module<'ast>,
|
2019-06-09 09:20:39 +09:00
|
|
|
directory: Option<Directory>,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<(), ModuleResolutionError> {
|
2019-06-09 09:20:39 +09:00
|
|
|
if let Some(directory) = directory {
|
|
|
|
self.directory = directory;
|
|
|
|
}
|
2021-03-28 16:25:30 -05:00
|
|
|
match (sub_mod.ast_mod_kind, sub_mod.items) {
|
2021-06-17 22:35:19 -05:00
|
|
|
(Some(Cow::Borrowed(ast::ModKind::Loaded(items, _, _))), _) => {
|
2021-11-07 20:37:34 -06:00
|
|
|
self.visit_mod_from_ast(items)
|
|
|
|
}
|
|
|
|
(Some(Cow::Owned(ast::ModKind::Loaded(items, _, _))), _) | (_, Cow::Owned(items)) => {
|
|
|
|
self.visit_mod_outside_ast(items)
|
2021-03-28 16:25:30 -05:00
|
|
|
}
|
|
|
|
(_, _) => Ok(()),
|
2019-06-09 09:20:39 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:47:18 +09:00
|
|
|
/// Find a file path in the filesystem which corresponds to the given module.
|
2019-03-17 12:25:59 +09:00
|
|
|
fn find_external_module(
|
|
|
|
&self,
|
2020-05-19 17:31:28 +09:00
|
|
|
mod_name: symbol::Ident,
|
2019-03-17 12:25:59 +09:00
|
|
|
attrs: &[ast::Attribute],
|
2020-09-02 18:42:00 -05:00
|
|
|
sub_mod: &Module<'ast>,
|
2020-06-11 21:11:18 -05:00
|
|
|
) -> Result<Option<SubModKind<'c, 'ast>>, ModuleResolutionError> {
|
2020-03-26 23:18:16 -05:00
|
|
|
let relative = match self.directory.ownership {
|
|
|
|
DirectoryOwnership::Owned { relative } => relative,
|
2021-03-15 20:07:44 -05:00
|
|
|
DirectoryOwnership::UnownedViaBlock => None,
|
2020-03-26 23:18:16 -05:00
|
|
|
};
|
2020-03-26 21:25:34 -05:00
|
|
|
if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) {
|
2020-03-26 23:18:16 -05:00
|
|
|
if self.parse_sess.is_file_parsed(&path) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2021-03-28 16:25:30 -05:00
|
|
|
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.span) {
|
|
|
|
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
|
|
|
|
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
|
2020-03-26 23:18:16 -05:00
|
|
|
path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
2021-03-28 16:25:30 -05:00
|
|
|
Module::new(
|
|
|
|
span,
|
|
|
|
Some(Cow::Owned(ast::ModKind::Unloaded)),
|
|
|
|
Cow::Owned(items),
|
|
|
|
Cow::Owned(attrs),
|
|
|
|
),
|
2020-03-26 23:18:16 -05:00
|
|
|
))),
|
2020-06-11 21:11:18 -05:00
|
|
|
Err(ParserError::ParseError) => Err(ModuleResolutionError {
|
|
|
|
module: mod_name.to_string(),
|
|
|
|
kind: ModuleResolutionErrorKind::ParseError { file: path },
|
|
|
|
}),
|
|
|
|
Err(..) => Err(ModuleResolutionError {
|
|
|
|
module: mod_name.to_string(),
|
|
|
|
kind: ModuleResolutionErrorKind::NotFound { file: path },
|
|
|
|
}),
|
2020-03-26 23:18:16 -05:00
|
|
|
};
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
// Look for nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
|
2020-03-26 21:25:34 -05:00
|
|
|
let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod);
|
2019-06-09 09:20:39 +09:00
|
|
|
|
2020-03-26 21:25:34 -05:00
|
|
|
match self
|
|
|
|
.parse_sess
|
|
|
|
.default_submod_path(mod_name, relative, &self.directory.path)
|
2019-03-17 12:25:59 +09:00
|
|
|
{
|
2020-03-26 21:25:34 -05:00
|
|
|
Ok(ModulePathSuccess {
|
2021-03-15 20:07:44 -05:00
|
|
|
file_path,
|
|
|
|
dir_ownership,
|
|
|
|
..
|
2020-03-26 23:18:16 -05:00
|
|
|
}) => {
|
|
|
|
let outside_mods_empty = mods_outside_ast.is_empty();
|
|
|
|
let should_insert = !mods_outside_ast
|
|
|
|
.iter()
|
2021-03-15 20:07:44 -05:00
|
|
|
.any(|(outside_path, _, _)| outside_path == &file_path);
|
|
|
|
if self.parse_sess.is_file_parsed(&file_path) {
|
2020-03-26 23:18:16 -05:00
|
|
|
if outside_mods_empty {
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
|
|
|
if should_insert {
|
2021-03-15 20:07:44 -05:00
|
|
|
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
|
2020-03-26 23:18:16 -05:00
|
|
|
}
|
|
|
|
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 20:07:44 -05:00
|
|
|
match Parser::parse_file_as_module(self.parse_sess, &file_path, sub_mod.span) {
|
2021-03-28 16:25:30 -05:00
|
|
|
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
|
|
|
|
Ok((attrs, items, span)) if outside_mods_empty => {
|
|
|
|
Ok(Some(SubModKind::External(
|
2021-03-15 20:07:44 -05:00
|
|
|
file_path,
|
|
|
|
dir_ownership,
|
2021-03-28 16:25:30 -05:00
|
|
|
Module::new(
|
|
|
|
span,
|
|
|
|
Some(Cow::Owned(ast::ModKind::Unloaded)),
|
|
|
|
Cow::Owned(items),
|
|
|
|
Cow::Owned(attrs),
|
|
|
|
),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok((attrs, items, span)) => {
|
2020-09-02 18:42:00 -05:00
|
|
|
mods_outside_ast.push((
|
2021-03-15 20:07:44 -05:00
|
|
|
file_path.clone(),
|
|
|
|
dir_ownership,
|
2021-03-28 16:25:30 -05:00
|
|
|
Module::new(
|
|
|
|
span,
|
|
|
|
Some(Cow::Owned(ast::ModKind::Unloaded)),
|
|
|
|
Cow::Owned(items),
|
|
|
|
Cow::Owned(attrs),
|
|
|
|
),
|
2020-09-02 18:42:00 -05:00
|
|
|
));
|
2020-03-26 23:18:16 -05:00
|
|
|
if should_insert {
|
2021-03-15 20:07:44 -05:00
|
|
|
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
|
2020-03-26 23:18:16 -05:00
|
|
|
}
|
|
|
|
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
|
|
|
|
}
|
2020-06-11 21:11:18 -05:00
|
|
|
Err(ParserError::ParseError) => Err(ModuleResolutionError {
|
|
|
|
module: mod_name.to_string(),
|
2021-03-15 20:07:44 -05:00
|
|
|
kind: ModuleResolutionErrorKind::ParseError { file: file_path },
|
2020-06-11 21:11:18 -05:00
|
|
|
}),
|
|
|
|
Err(..) if outside_mods_empty => Err(ModuleResolutionError {
|
|
|
|
module: mod_name.to_string(),
|
2021-03-15 20:07:44 -05:00
|
|
|
kind: ModuleResolutionErrorKind::NotFound { file: file_path },
|
2020-06-11 21:11:18 -05:00
|
|
|
}),
|
|
|
|
Err(..) => {
|
2020-03-26 23:18:16 -05:00
|
|
|
if should_insert {
|
2021-03-15 20:07:44 -05:00
|
|
|
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
|
2020-03-26 23:18:16 -05:00
|
|
|
}
|
|
|
|
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
|
2020-03-27 21:47:03 -05:00
|
|
|
}
|
2020-03-26 23:18:16 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 20:07:44 -05:00
|
|
|
Err(mod_err) if !mods_outside_ast.is_empty() => {
|
|
|
|
if let ModError::ParserError(mut e) = mod_err {
|
|
|
|
e.cancel();
|
|
|
|
}
|
2020-03-26 23:18:16 -05:00
|
|
|
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
|
|
|
|
}
|
2021-03-15 20:07:44 -05:00
|
|
|
Err(_) => Err(ModuleResolutionError {
|
|
|
|
module: mod_name.to_string(),
|
|
|
|
kind: ModuleResolutionErrorKind::NotFound {
|
|
|
|
file: self.directory.path.clone(),
|
|
|
|
},
|
|
|
|
}),
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:31:28 +09:00
|
|
|
fn push_inline_mod_directory(&mut self, id: symbol::Ident, attrs: &[ast::Attribute]) {
|
2019-03-17 12:25:59 +09:00
|
|
|
if let Some(path) = find_path_value(attrs) {
|
2021-12-15 14:39:23 +11:00
|
|
|
self.directory.path.push(path.as_str());
|
2019-03-17 12:25:59 +09:00
|
|
|
self.directory.ownership = DirectoryOwnership::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
|
|
|
|
// `mod x { ... }` come after the relative extension.
|
|
|
|
//
|
|
|
|
// 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 self.directory.ownership {
|
|
|
|
if let Some(ident) = relative.take() {
|
|
|
|
// remove the relative offset
|
2021-12-15 16:13:11 +11:00
|
|
|
self.directory.path.push(ident.as_str());
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 16:13:11 +11:00
|
|
|
self.directory.path.push(id.as_str());
|
2019-03-17 12:25:59 +09:00
|
|
|
}
|
2017-12-08 14:16:47 +01:00
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
|
2020-03-26 21:25:34 -05:00
|
|
|
fn find_mods_outside_of_ast(
|
2019-06-09 09:20:39 +09:00
|
|
|
&self,
|
|
|
|
attrs: &[ast::Attribute],
|
2020-09-02 18:42:00 -05:00
|
|
|
sub_mod: &Module<'ast>,
|
|
|
|
) -> Vec<(PathBuf, DirectoryOwnership, Module<'ast>)> {
|
2019-06-09 09:20:39 +09:00
|
|
|
// Filter nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
|
|
|
|
let mut path_visitor = visitor::PathVisitor::default();
|
|
|
|
for attr in attrs.iter() {
|
|
|
|
if let Some(meta) = attr.meta() {
|
|
|
|
path_visitor.visit_meta_item(&meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut result = vec![];
|
|
|
|
for path in path_visitor.paths() {
|
|
|
|
let mut actual_path = self.directory.path.clone();
|
|
|
|
actual_path.push(&path);
|
|
|
|
if !actual_path.exists() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-26 21:25:34 -05:00
|
|
|
if self.parse_sess.is_file_parsed(&actual_path) {
|
|
|
|
// If the specified file is already parsed, then we just use that.
|
2019-06-09 09:20:39 +09:00
|
|
|
result.push((
|
|
|
|
actual_path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
|
|
|
sub_mod.clone(),
|
|
|
|
));
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-28 16:25:30 -05:00
|
|
|
let (attrs, items, span) =
|
|
|
|
match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.span) {
|
|
|
|
Ok((ref attrs, _, _)) if contains_skip(attrs) => continue,
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(..) => continue,
|
|
|
|
};
|
2020-03-26 21:25:34 -05:00
|
|
|
|
2019-06-09 09:20:39 +09:00
|
|
|
result.push((
|
|
|
|
actual_path,
|
|
|
|
DirectoryOwnership::Owned { relative: None },
|
2021-03-28 16:25:30 -05:00
|
|
|
Module::new(
|
|
|
|
span,
|
|
|
|
Some(Cow::Owned(ast::ModKind::Unloaded)),
|
|
|
|
Cow::Owned(items),
|
|
|
|
Cow::Owned(attrs),
|
|
|
|
),
|
2019-06-09 09:20:39 +09:00
|
|
|
))
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2015-07-26 12:55:25 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 22:28:05 +09:00
|
|
|
fn path_value(attr: &ast::Attribute) -> Option<Symbol> {
|
2020-09-02 12:36:50 -05:00
|
|
|
if attr.has_name(sym::path) {
|
2018-08-06 22:28:05 +09:00
|
|
|
attr.value_str()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 02:56:42 +00:00
|
|
|
// N.B., even when there are multiple `#[path = ...]` attributes, we just need to
|
2018-08-06 22:34:58 +09:00
|
|
|
// examine the first one, since rustc ignores the second and the subsequent ones
|
|
|
|
// as unused attributes.
|
2018-08-06 22:28:05 +09:00
|
|
|
fn find_path_value(attrs: &[ast::Attribute]) -> Option<Symbol> {
|
|
|
|
attrs.iter().flat_map(path_value).next()
|
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
|
|
|
|
fn is_cfg_if(item: &ast::Item) -> bool {
|
2019-10-05 23:40:24 +09:00
|
|
|
match item.kind {
|
2020-03-26 23:18:16 -05:00
|
|
|
ast::ItemKind::MacCall(ref mac) => {
|
2019-09-06 22:41:03 +09:00
|
|
|
if let Some(first_segment) = mac.path.segments.first() {
|
2021-01-19 13:40:58 -05:00
|
|
|
if first_segment.ident.name == Symbol::intern("cfg_if") {
|
2019-09-02 04:36:51 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2019-06-09 09:20:39 +09:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|