2021-02-22 09:58:45 -06:00
|
|
|
use crate::base::ModuleData;
|
2021-02-16 15:56:07 -06:00
|
|
|
use rustc_ast::ptr::P;
|
2021-02-21 07:32:38 -06:00
|
|
|
use rustc_ast::{token, Attribute, Inline, Item};
|
2021-02-22 12:22:40 -06:00
|
|
|
use rustc_errors::{struct_span_err, DiagnosticBuilder};
|
2020-03-21 17:10:10 -05:00
|
|
|
use rustc_parse::new_parser_from_file;
|
2020-03-08 03:28:46 -05:00
|
|
|
use rustc_session::parse::ParseSess;
|
2020-07-29 20:27:50 -05:00
|
|
|
use rustc_session::Session;
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::{sym, Ident};
|
2021-02-22 09:42:57 -06:00
|
|
|
use rustc_span::Span;
|
2019-10-11 06:06:36 -05:00
|
|
|
|
2019-08-11 11:34:42 -05:00
|
|
|
use std::path::{self, Path, PathBuf};
|
|
|
|
|
2020-03-08 16:10:37 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2021-02-22 10:06:36 -06:00
|
|
|
pub enum DirOwnership {
|
2020-03-08 16:10:37 -05:00
|
|
|
Owned {
|
|
|
|
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
|
2020-04-19 06:00:18 -05:00
|
|
|
relative: Option<Ident>,
|
2020-03-08 16:10:37 -05:00
|
|
|
},
|
|
|
|
UnownedViaBlock,
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:19:57 -06:00
|
|
|
// Public for rustfmt usage.
|
|
|
|
pub struct ModulePathSuccess {
|
2021-02-22 10:06:36 -06:00
|
|
|
pub file_path: PathBuf,
|
|
|
|
pub dir_ownership: DirOwnership,
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 09:58:45 -06:00
|
|
|
crate struct ParsedExternalMod {
|
|
|
|
pub items: Vec<P<Item>>,
|
|
|
|
pub inner_span: Span,
|
|
|
|
pub file_path: PathBuf,
|
|
|
|
pub dir_path: PathBuf,
|
2021-02-22 10:06:36 -06:00
|
|
|
pub dir_ownership: DirOwnership,
|
2021-02-22 09:58:45 -06:00
|
|
|
}
|
|
|
|
|
2021-02-22 12:22:40 -06:00
|
|
|
pub enum ModError<'a> {
|
|
|
|
CircularInclusion(Vec<PathBuf>),
|
|
|
|
ModInBlock(Option<Ident>),
|
2021-05-05 03:00:18 -05:00
|
|
|
FileNotFound(Ident, PathBuf, PathBuf),
|
2021-05-03 05:57:48 -05:00
|
|
|
MultipleCandidates(Ident, PathBuf, PathBuf),
|
2021-02-22 12:22:40 -06:00
|
|
|
ParserError(DiagnosticBuilder<'a>),
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:21:37 -05:00
|
|
|
crate fn parse_external_mod(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &Session,
|
2021-02-22 10:06:36 -06:00
|
|
|
ident: Ident,
|
2020-03-09 05:16:00 -05:00
|
|
|
span: Span, // The span to blame on errors.
|
2021-02-22 09:58:45 -06:00
|
|
|
module: &ModuleData,
|
2021-02-22 10:06:36 -06:00
|
|
|
mut dir_ownership: DirOwnership,
|
2020-03-08 07:36:20 -05:00
|
|
|
attrs: &mut Vec<Attribute>,
|
2021-02-22 09:58:45 -06:00
|
|
|
) -> ParsedExternalMod {
|
2020-03-08 07:36:20 -05:00
|
|
|
// We bail on the first error, but that error does not cause a fatal error... (1)
|
2021-02-22 12:22:40 -06:00
|
|
|
let result: Result<_, ModError<'_>> = try {
|
2020-03-08 07:36:20 -05:00
|
|
|
// Extract the file path and the new ownership.
|
2021-02-22 12:22:40 -06:00
|
|
|
let mp = mod_file_path(sess, ident, &attrs, &module.dir_path, dir_ownership)?;
|
2021-02-22 10:06:36 -06:00
|
|
|
dir_ownership = mp.dir_ownership;
|
2020-03-08 07:36:20 -05:00
|
|
|
|
|
|
|
// Ensure file paths are acyclic.
|
2021-02-22 12:22:40 -06:00
|
|
|
if let Some(pos) = module.file_path_stack.iter().position(|p| p == &mp.file_path) {
|
|
|
|
Err(ModError::CircularInclusion(module.file_path_stack[pos..].to_vec()))?;
|
|
|
|
}
|
2020-03-08 07:36:20 -05:00
|
|
|
|
2020-03-21 16:51:03 -05:00
|
|
|
// Actually parse the external file as a module.
|
2021-02-22 10:06:36 -06:00
|
|
|
let mut parser = new_parser_from_file(&sess.parse_sess, &mp.file_path, Some(span));
|
2021-02-22 12:22:40 -06:00
|
|
|
let (mut inner_attrs, items, inner_span) =
|
|
|
|
parser.parse_mod(&token::Eof).map_err(|err| ModError::ParserError(err))?;
|
2021-02-16 15:56:07 -06:00
|
|
|
attrs.append(&mut inner_attrs);
|
2021-02-22 10:06:36 -06:00
|
|
|
(items, inner_span, mp.file_path)
|
2020-03-08 07:36:20 -05:00
|
|
|
};
|
|
|
|
// (1) ...instead, we return a dummy module.
|
2021-02-22 12:22:40 -06:00
|
|
|
let (items, inner_span, file_path) =
|
|
|
|
result.map_err(|err| err.report(sess, span)).unwrap_or_default();
|
2020-03-08 07:36:20 -05:00
|
|
|
|
2021-02-22 09:42:57 -06:00
|
|
|
// Extract the directory path for submodules of the module.
|
2021-02-22 09:58:45 -06:00
|
|
|
let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
|
2020-03-08 07:36:20 -05:00
|
|
|
|
2021-02-22 09:58:45 -06:00
|
|
|
ParsedExternalMod { items, inner_span, file_path, dir_path, dir_ownership }
|
2020-03-08 03:54:19 -05:00
|
|
|
}
|
2020-03-07 12:53:25 -06:00
|
|
|
|
2021-02-22 10:06:36 -06:00
|
|
|
crate fn mod_dir_path(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &Session,
|
2021-02-22 10:06:36 -06:00
|
|
|
ident: Ident,
|
2020-03-08 03:54:19 -05:00
|
|
|
attrs: &[Attribute],
|
2021-02-22 09:58:45 -06:00
|
|
|
module: &ModuleData,
|
2021-02-22 10:06:36 -06:00
|
|
|
mut dir_ownership: DirOwnership,
|
2021-02-21 07:32:38 -06:00
|
|
|
inline: Inline,
|
2021-02-22 10:06:36 -06:00
|
|
|
) -> (PathBuf, DirOwnership) {
|
2021-02-21 07:32:38 -06:00
|
|
|
match inline {
|
2021-08-16 10:29:49 -05:00
|
|
|
Inline::Yes if let Some(file_path) = mod_file_path_from_attr(sess, attrs, &module.dir_path) => {
|
|
|
|
// For inline modules file path from `#[path]` is actually the directory path
|
|
|
|
// for historical reasons, so we don't pop the last segment here.
|
|
|
|
(file_path, DirOwnership::Owned { relative: None })
|
|
|
|
}
|
2021-02-21 07:32:38 -06:00
|
|
|
Inline::Yes => {
|
|
|
|
// 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`.
|
|
|
|
let mut dir_path = module.dir_path.clone();
|
|
|
|
if let DirOwnership::Owned { relative } = &mut dir_ownership {
|
|
|
|
if let Some(ident) = relative.take() {
|
|
|
|
// Remove the relative offset.
|
|
|
|
dir_path.push(&*ident.as_str());
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 11:56:49 -06:00
|
|
|
dir_path.push(&*ident.as_str());
|
2021-02-21 07:32:38 -06:00
|
|
|
|
|
|
|
(dir_path, dir_ownership)
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
2021-02-21 07:32:38 -06:00
|
|
|
Inline::No => {
|
|
|
|
// FIXME: This is a subset of `parse_external_mod` without actual parsing,
|
|
|
|
// check whether the logic for unloaded, loaded and inline modules can be unified.
|
|
|
|
let file_path = mod_file_path(sess, ident, &attrs, &module.dir_path, dir_ownership)
|
|
|
|
.map(|mp| {
|
|
|
|
dir_ownership = mp.dir_ownership;
|
|
|
|
mp.file_path
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2021-02-22 09:58:45 -06:00
|
|
|
|
2021-02-21 07:32:38 -06:00
|
|
|
// Extract the directory path for submodules of the module.
|
|
|
|
let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
|
|
|
|
|
|
|
|
(dir_path, dir_ownership)
|
|
|
|
}
|
|
|
|
}
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
2020-03-08 03:28:46 -05:00
|
|
|
|
2021-02-22 10:06:36 -06:00
|
|
|
fn mod_file_path<'a>(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &'a Session,
|
2021-02-22 10:06:36 -06:00
|
|
|
ident: Ident,
|
2020-03-08 06:19:27 -05:00
|
|
|
attrs: &[Attribute],
|
2020-03-08 03:28:46 -05:00
|
|
|
dir_path: &Path,
|
2021-02-22 10:06:36 -06:00
|
|
|
dir_ownership: DirOwnership,
|
2021-02-22 12:22:40 -06:00
|
|
|
) -> Result<ModulePathSuccess, ModError<'a>> {
|
2021-02-22 10:06:36 -06:00
|
|
|
if let Some(file_path) = mod_file_path_from_attr(sess, attrs, dir_path) {
|
2021-02-22 07:12:11 -06:00
|
|
|
// 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,
|
|
|
|
//
|
|
|
|
// 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
|
2021-02-22 10:06:36 -06:00
|
|
|
let dir_ownership = DirOwnership::Owned { relative: None };
|
|
|
|
return Ok(ModulePathSuccess { file_path, dir_ownership });
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:06:36 -06:00
|
|
|
let relative = match dir_ownership {
|
|
|
|
DirOwnership::Owned { relative } => relative,
|
|
|
|
DirOwnership::UnownedViaBlock => None,
|
2020-03-08 03:28:46 -05:00
|
|
|
};
|
2021-02-22 12:22:40 -06:00
|
|
|
let result = default_submod_path(&sess.parse_sess, ident, relative, dir_path);
|
2021-02-22 10:06:36 -06:00
|
|
|
match dir_ownership {
|
2021-02-22 12:22:40 -06:00
|
|
|
DirOwnership::Owned { .. } => result,
|
|
|
|
DirOwnership::UnownedViaBlock => Err(ModError::ModInBlock(match result {
|
|
|
|
Ok(_) | Err(ModError::MultipleCandidates(..)) => Some(ident),
|
|
|
|
_ => None,
|
|
|
|
})),
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Derive a submodule path from the first found `#[path = "path_string"]`.
|
|
|
|
/// The provided `dir_path` is joined with the `path_string`.
|
2021-02-22 10:06:36 -06:00
|
|
|
fn mod_file_path_from_attr(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &Session,
|
|
|
|
attrs: &[Attribute],
|
|
|
|
dir_path: &Path,
|
|
|
|
) -> Option<PathBuf> {
|
2020-03-08 03:28:46 -05:00
|
|
|
// Extract path string from first `#[path = "path_string"]` attribute.
|
2021-02-22 11:56:49 -06:00
|
|
|
let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?.as_str();
|
2020-03-08 03:28:46 -05:00
|
|
|
|
|
|
|
// On windows, the base path might have the form
|
|
|
|
// `\\?\foo\bar` in which case it does not tolerate
|
|
|
|
// mixed `/` and `\` separators, so canonicalize
|
|
|
|
// `/` to `\`.
|
|
|
|
#[cfg(windows)]
|
|
|
|
let path_string = path_string.replace("/", "\\");
|
|
|
|
|
|
|
|
Some(dir_path.join(&*path_string))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a path to a module.
|
|
|
|
// Public for rustfmt usage.
|
|
|
|
pub fn default_submod_path<'a>(
|
|
|
|
sess: &'a ParseSess,
|
2021-02-22 10:06:36 -06:00
|
|
|
ident: Ident,
|
2020-04-19 06:00:18 -05:00
|
|
|
relative: Option<Ident>,
|
2020-03-08 03:28:46 -05:00
|
|
|
dir_path: &Path,
|
2021-02-22 12:22:40 -06:00
|
|
|
) -> Result<ModulePathSuccess, ModError<'a>> {
|
2020-03-08 03:28:46 -05:00
|
|
|
// If we're in a foo.rs file instead of a mod.rs file,
|
|
|
|
// we need to look for submodules in
|
2021-02-22 10:06:36 -06:00
|
|
|
// `./foo/<ident>.rs` and `./foo/<ident>/mod.rs` rather than
|
|
|
|
// `./<ident>.rs` and `./<ident>/mod.rs`.
|
2020-03-08 03:28:46 -05:00
|
|
|
let relative_prefix_string;
|
|
|
|
let relative_prefix = if let Some(ident) = relative {
|
|
|
|
relative_prefix_string = format!("{}{}", ident.name, path::MAIN_SEPARATOR);
|
|
|
|
&relative_prefix_string
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
2021-02-22 10:06:36 -06:00
|
|
|
let mod_name = ident.name.to_string();
|
2020-03-08 03:28:46 -05:00
|
|
|
let default_path_str = format!("{}{}.rs", relative_prefix, mod_name);
|
|
|
|
let secondary_path_str =
|
|
|
|
format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);
|
|
|
|
let default_path = dir_path.join(&default_path_str);
|
|
|
|
let secondary_path = dir_path.join(&secondary_path_str);
|
|
|
|
let default_exists = sess.source_map().file_exists(&default_path);
|
|
|
|
let secondary_exists = sess.source_map().file_exists(&secondary_path);
|
|
|
|
|
2021-02-22 12:22:40 -06:00
|
|
|
match (default_exists, secondary_exists) {
|
2020-03-08 03:28:46 -05:00
|
|
|
(true, false) => Ok(ModulePathSuccess {
|
2021-02-22 10:06:36 -06:00
|
|
|
file_path: default_path,
|
|
|
|
dir_ownership: DirOwnership::Owned { relative: Some(ident) },
|
2020-03-08 03:28:46 -05:00
|
|
|
}),
|
|
|
|
(false, true) => Ok(ModulePathSuccess {
|
2021-02-22 10:06:36 -06:00
|
|
|
file_path: secondary_path,
|
|
|
|
dir_ownership: DirOwnership::Owned { relative: None },
|
2020-03-08 03:28:46 -05:00
|
|
|
}),
|
2021-05-05 03:00:18 -05:00
|
|
|
(false, false) => Err(ModError::FileNotFound(ident, default_path, secondary_path)),
|
2021-05-03 05:57:48 -05:00
|
|
|
(true, true) => Err(ModError::MultipleCandidates(ident, default_path, secondary_path)),
|
2021-02-22 12:22:40 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 03:28:46 -05:00
|
|
|
|
2021-02-22 12:22:40 -06:00
|
|
|
impl ModError<'_> {
|
|
|
|
fn report(self, sess: &Session, span: Span) {
|
|
|
|
let diag = &sess.parse_sess.span_diagnostic;
|
|
|
|
match self {
|
|
|
|
ModError::CircularInclusion(file_paths) => {
|
|
|
|
let mut msg = String::from("circular modules: ");
|
|
|
|
for file_path in &file_paths {
|
|
|
|
msg.push_str(&file_path.display().to_string());
|
|
|
|
msg.push_str(" -> ");
|
|
|
|
}
|
|
|
|
msg.push_str(&file_paths[0].display().to_string());
|
|
|
|
diag.struct_span_err(span, &msg)
|
|
|
|
}
|
|
|
|
ModError::ModInBlock(ident) => {
|
|
|
|
let msg = "cannot declare a non-inline module inside a block unless it has a path attribute";
|
|
|
|
let mut err = diag.struct_span_err(span, msg);
|
|
|
|
if let Some(ident) = ident {
|
|
|
|
let note =
|
|
|
|
format!("maybe `use` the module `{}` instead of redeclaring it", ident);
|
|
|
|
err.span_note(span, ¬e);
|
|
|
|
}
|
|
|
|
err
|
|
|
|
}
|
2021-05-05 03:00:18 -05:00
|
|
|
ModError::FileNotFound(ident, default_path, secondary_path) => {
|
2021-02-22 12:22:40 -06:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
diag,
|
|
|
|
span,
|
|
|
|
E0583,
|
|
|
|
"file not found for module `{}`",
|
|
|
|
ident,
|
|
|
|
);
|
|
|
|
err.help(&format!(
|
2021-05-05 03:00:18 -05:00
|
|
|
"to create the module `{}`, create file \"{}\" or \"{}\"",
|
2021-02-22 12:22:40 -06:00
|
|
|
ident,
|
|
|
|
default_path.display(),
|
2021-05-05 03:00:18 -05:00
|
|
|
secondary_path.display(),
|
2021-02-22 12:22:40 -06:00
|
|
|
));
|
|
|
|
err
|
|
|
|
}
|
2021-05-03 05:57:48 -05:00
|
|
|
ModError::MultipleCandidates(ident, default_path, secondary_path) => {
|
2021-02-22 12:22:40 -06:00
|
|
|
let mut err = struct_span_err!(
|
|
|
|
diag,
|
|
|
|
span,
|
|
|
|
E0761,
|
2021-05-03 03:02:54 -05:00
|
|
|
"file for module `{}` found at both \"{}\" and \"{}\"",
|
2021-02-22 12:22:40 -06:00
|
|
|
ident,
|
2021-05-03 05:57:48 -05:00
|
|
|
default_path.display(),
|
|
|
|
secondary_path.display(),
|
2021-02-22 12:22:40 -06:00
|
|
|
);
|
|
|
|
err.help("delete or rename one of them to remove the ambiguity");
|
|
|
|
err
|
|
|
|
}
|
|
|
|
ModError::ParserError(err) => err,
|
|
|
|
}.emit()
|
|
|
|
}
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|