2020-08-23 05:42:19 -05:00
|
|
|
use rustc_ast::{token, Attribute, Mod, Unsafe};
|
2020-03-08 03:28:46 -05:00
|
|
|
use rustc_errors::{struct_span_err, PResult};
|
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-03-08 06:19:27 -05:00
|
|
|
use rustc_span::source_map::{FileName, Span};
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::{sym, Ident};
|
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(Clone)]
|
|
|
|
pub struct Directory {
|
|
|
|
pub path: PathBuf,
|
|
|
|
pub ownership: DirectoryOwnership,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum DirectoryOwnership {
|
|
|
|
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,
|
|
|
|
UnownedViaMod,
|
|
|
|
}
|
|
|
|
|
2019-08-11 11:34:42 -05:00
|
|
|
/// Information about the path to a module.
|
2020-01-11 13:19:57 -06:00
|
|
|
// Public for rustfmt usage.
|
2020-03-08 03:28:46 -05:00
|
|
|
pub struct ModulePath<'a> {
|
2019-08-11 11:34:42 -05:00
|
|
|
name: String,
|
|
|
|
path_exists: bool,
|
2020-03-08 03:28:46 -05:00
|
|
|
pub result: PResult<'a, ModulePathSuccess>,
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
|
|
|
|
2020-01-11 13:19:57 -06:00
|
|
|
// Public for rustfmt usage.
|
|
|
|
pub struct ModulePathSuccess {
|
2019-08-11 11:34:42 -05:00
|
|
|
pub path: PathBuf,
|
2020-03-08 06:19:27 -05:00
|
|
|
pub ownership: DirectoryOwnership,
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 16:21:37 -05:00
|
|
|
crate fn parse_external_mod(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &Session,
|
2020-04-19 06:00:18 -05:00
|
|
|
id: Ident,
|
2020-03-09 05:16:00 -05:00
|
|
|
span: Span, // The span to blame on errors.
|
2020-08-23 05:42:19 -05:00
|
|
|
unsafety: Unsafe,
|
2020-03-08 07:36:20 -05:00
|
|
|
Directory { mut ownership, path }: Directory,
|
|
|
|
attrs: &mut Vec<Attribute>,
|
|
|
|
pop_mod_stack: &mut bool,
|
|
|
|
) -> (Mod, 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.
|
2020-03-09 05:16:00 -05:00
|
|
|
let mp = submod_path(sess, id, span, &attrs, ownership, &path)?;
|
2020-03-08 07:36:20 -05:00
|
|
|
ownership = mp.ownership;
|
|
|
|
|
|
|
|
// Ensure file paths are acyclic.
|
2020-07-29 20:27:50 -05:00
|
|
|
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)?;
|
2020-03-08 07:36:20 -05:00
|
|
|
included_mod_stack.push(mp.path.clone());
|
|
|
|
*pop_mod_stack = true; // We have pushed, so notify caller.
|
|
|
|
drop(included_mod_stack);
|
|
|
|
|
2020-03-21 16:51:03 -05:00
|
|
|
// Actually parse the external file as a module.
|
2020-08-23 05:42:19 -05:00
|
|
|
let mut parser = new_parser_from_file(&sess.parse_sess, &mp.path, Some(span));
|
|
|
|
let mut module = parser.parse_mod(&token::Eof, unsafety)?;
|
2020-03-08 07:36:20 -05:00
|
|
|
module.0.inline = false;
|
|
|
|
module
|
|
|
|
};
|
|
|
|
// (1) ...instead, we return a dummy module.
|
2020-08-23 05:42:19 -05:00
|
|
|
let (module, mut new_attrs) = result.map_err(|mut err| err.emit()).unwrap_or_else(|_| {
|
|
|
|
let module = Mod { inner: Span::default(), unsafety, items: Vec::new(), inline: false };
|
|
|
|
(module, Vec::new())
|
|
|
|
});
|
2020-03-08 07:36:20 -05:00
|
|
|
attrs.append(&mut new_attrs);
|
|
|
|
|
|
|
|
// Extract the directory path for submodules of `module`.
|
|
|
|
let path = sess.source_map().span_to_unmapped_path(module.inner);
|
|
|
|
let mut path = match path {
|
2020-05-29 10:31:55 -05:00
|
|
|
FileName::Real(name) => name.into_local_path(),
|
2020-03-08 07:36:20 -05:00
|
|
|
other => PathBuf::from(other.to_string()),
|
|
|
|
};
|
|
|
|
path.pop();
|
|
|
|
|
|
|
|
(module, Directory { ownership, path })
|
2020-03-08 03:54:19 -05:00
|
|
|
}
|
2020-03-07 12:53:25 -06:00
|
|
|
|
2020-03-08 07:29:37 -05:00
|
|
|
fn error_on_circular_module<'a>(
|
|
|
|
sess: &'a ParseSess,
|
|
|
|
span: Span,
|
|
|
|
path: &Path,
|
|
|
|
included_mod_stack: &[PathBuf],
|
|
|
|
) -> PResult<'a, ()> {
|
|
|
|
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
|
|
|
|
let mut err = String::from("circular modules: ");
|
|
|
|
for p in &included_mod_stack[i..] {
|
|
|
|
err.push_str(&p.to_string_lossy());
|
|
|
|
err.push_str(" -> ");
|
|
|
|
}
|
|
|
|
err.push_str(&path.to_string_lossy());
|
|
|
|
return Err(sess.span_diagnostic.struct_span_err(span, &err[..]));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:21:37 -05:00
|
|
|
crate fn push_directory(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &Session,
|
2020-03-08 03:54:19 -05:00
|
|
|
id: Ident,
|
|
|
|
attrs: &[Attribute],
|
2020-03-08 07:36:20 -05:00
|
|
|
Directory { mut ownership, mut path }: Directory,
|
|
|
|
) -> Directory {
|
2020-07-29 20:27:50 -05:00
|
|
|
if let Some(filename) = sess.first_attr_value_str_by_name(attrs, sym::path) {
|
2020-03-08 07:36:20 -05:00
|
|
|
path.push(&*filename.as_str());
|
|
|
|
ownership = DirectoryOwnership::Owned { relative: None };
|
2020-03-08 03:54:19 -05:00
|
|
|
} 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`.
|
2020-03-08 07:36:20 -05:00
|
|
|
if let DirectoryOwnership::Owned { relative } = &mut ownership {
|
2020-03-08 03:54:19 -05:00
|
|
|
if let Some(ident) = relative.take() {
|
|
|
|
// Remove the relative offset.
|
2020-03-08 07:36:20 -05:00
|
|
|
path.push(&*ident.as_str());
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 07:36:20 -05:00
|
|
|
path.push(&*id.as_str());
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
2020-03-08 07:36:20 -05:00
|
|
|
Directory { ownership, path }
|
2019-08-11 11:34:42 -05:00
|
|
|
}
|
2020-03-08 03:28:46 -05:00
|
|
|
|
|
|
|
fn submod_path<'a>(
|
2020-07-29 20:27:50 -05:00
|
|
|
sess: &'a Session,
|
2020-04-19 06:00:18 -05:00
|
|
|
id: Ident,
|
2020-03-09 05:16:00 -05:00
|
|
|
span: Span,
|
2020-03-08 06:19:27 -05:00
|
|
|
attrs: &[Attribute],
|
|
|
|
ownership: DirectoryOwnership,
|
2020-03-08 03:28:46 -05:00
|
|
|
dir_path: &Path,
|
|
|
|
) -> PResult<'a, ModulePathSuccess> {
|
2020-07-29 20:27:50 -05:00
|
|
|
if let Some(path) = submod_path_from_attr(sess, attrs, dir_path) {
|
2020-03-08 06:19:27 -05:00
|
|
|
let ownership = match path.file_name().and_then(|s| s.to_str()) {
|
2020-03-08 03:28:46 -05: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
|
|
|
|
Some(_) => DirectoryOwnership::Owned { relative: None },
|
|
|
|
_ => DirectoryOwnership::UnownedViaMod,
|
|
|
|
};
|
2020-03-08 06:19:27 -05:00
|
|
|
return Ok(ModulePathSuccess { ownership, path });
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 06:19:27 -05:00
|
|
|
let relative = match ownership {
|
2020-03-08 03:28:46 -05:00
|
|
|
DirectoryOwnership::Owned { relative } => relative,
|
|
|
|
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
|
|
|
|
};
|
|
|
|
let ModulePath { path_exists, name, result } =
|
2020-07-29 20:27:50 -05:00
|
|
|
default_submod_path(&sess.parse_sess, id, span, relative, dir_path);
|
2020-03-08 06:19:27 -05:00
|
|
|
match ownership {
|
2020-03-08 03:28:46 -05:00
|
|
|
DirectoryOwnership::Owned { .. } => Ok(result?),
|
|
|
|
DirectoryOwnership::UnownedViaBlock => {
|
|
|
|
let _ = result.map_err(|mut err| err.cancel());
|
2020-07-29 20:27:50 -05:00
|
|
|
error_decl_mod_in_block(&sess.parse_sess, span, path_exists, &name)
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|
|
|
|
DirectoryOwnership::UnownedViaMod => {
|
|
|
|
let _ = result.map_err(|mut err| err.cancel());
|
2020-07-29 20:27:50 -05:00
|
|
|
error_cannot_declare_mod_here(&sess.parse_sess, span, path_exists, &name)
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_decl_mod_in_block<'a, T>(
|
|
|
|
sess: &'a ParseSess,
|
2020-03-09 05:16:00 -05:00
|
|
|
span: Span,
|
2020-03-08 03:28:46 -05:00
|
|
|
path_exists: bool,
|
|
|
|
name: &str,
|
|
|
|
) -> PResult<'a, T> {
|
|
|
|
let msg = "Cannot declare a non-inline module inside a block unless it has a path attribute";
|
2020-03-09 05:16:00 -05:00
|
|
|
let mut err = sess.span_diagnostic.struct_span_err(span, msg);
|
2020-03-08 03:28:46 -05:00
|
|
|
if path_exists {
|
|
|
|
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it", name);
|
2020-03-09 05:16:00 -05:00
|
|
|
err.span_note(span, &msg);
|
2020-03-08 03:28:46 -05:00
|
|
|
}
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_cannot_declare_mod_here<'a, T>(
|
|
|
|
sess: &'a ParseSess,
|
2020-03-09 05:16:00 -05:00
|
|
|
span: Span,
|
2020-03-08 03:28:46 -05:00
|
|
|
path_exists: bool,
|
|
|
|
name: &str,
|
|
|
|
) -> PResult<'a, T> {
|
|
|
|
let mut err =
|
2020-03-09 05:16:00 -05:00
|
|
|
sess.span_diagnostic.struct_span_err(span, "cannot declare a new module at this location");
|
|
|
|
if !span.is_dummy() {
|
2020-05-29 10:31:55 -05:00
|
|
|
if let FileName::Real(src_name) = sess.source_map().span_to_filename(span) {
|
|
|
|
let src_path = src_name.into_local_path();
|
2020-03-08 03:28:46 -05:00
|
|
|
if let Some(stem) = src_path.file_stem() {
|
|
|
|
let mut dest_path = src_path.clone();
|
|
|
|
dest_path.set_file_name(stem);
|
|
|
|
dest_path.push("mod.rs");
|
|
|
|
err.span_note(
|
2020-03-09 05:16:00 -05:00
|
|
|
span,
|
2020-03-08 03:28:46 -05:00
|
|
|
&format!(
|
2020-03-09 05:16:00 -05:00
|
|
|
"maybe move this module `{}` to its own directory via `{}`",
|
2020-03-08 03:28:46 -05:00
|
|
|
src_path.display(),
|
|
|
|
dest_path.display()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if path_exists {
|
|
|
|
err.span_note(
|
2020-03-09 05:16:00 -05:00
|
|
|
span,
|
2020-03-08 03:28:46 -05:00
|
|
|
&format!("... or maybe `use` the module `{}` instead of possibly redeclaring it", name),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Derive a submodule path from the first found `#[path = "path_string"]`.
|
|
|
|
/// The provided `dir_path` is joined with the `path_string`.
|
2020-09-04 19:44:11 -05:00
|
|
|
pub(super) fn submod_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.
|
2020-07-29 20:27:50 -05:00
|
|
|
let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?;
|
2020-03-08 03:28:46 -05:00
|
|
|
let path_string = path_string.as_str();
|
|
|
|
|
|
|
|
// 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,
|
2020-04-19 06:00:18 -05:00
|
|
|
id: Ident,
|
2020-03-09 05:16:00 -05:00
|
|
|
span: Span,
|
2020-04-19 06:00:18 -05:00
|
|
|
relative: Option<Ident>,
|
2020-03-08 03:28:46 -05:00
|
|
|
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`.
|
|
|
|
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 {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
let mod_name = id.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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
let result = match (default_exists, secondary_exists) {
|
|
|
|
(true, false) => Ok(ModulePathSuccess {
|
|
|
|
path: default_path,
|
2020-03-08 06:19:27 -05:00
|
|
|
ownership: DirectoryOwnership::Owned { relative: Some(id) },
|
2020-03-08 03:28:46 -05:00
|
|
|
}),
|
|
|
|
(false, true) => Ok(ModulePathSuccess {
|
|
|
|
path: secondary_path,
|
2020-03-08 06:19:27 -05:00
|
|
|
ownership: DirectoryOwnership::Owned { relative: None },
|
2020-03-08 03:28:46 -05:00
|
|
|
}),
|
|
|
|
(false, false) => {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
sess.span_diagnostic,
|
2020-03-09 05:16:00 -05:00
|
|
|
span,
|
2020-03-08 03:28:46 -05:00
|
|
|
E0583,
|
|
|
|
"file not found for module `{}`",
|
|
|
|
mod_name,
|
|
|
|
);
|
|
|
|
err.help(&format!(
|
|
|
|
"to create the module `{}`, create file \"{}\"",
|
|
|
|
mod_name,
|
|
|
|
default_path.display(),
|
|
|
|
));
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
(true, true) => {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
sess.span_diagnostic,
|
2020-03-09 05:16:00 -05:00
|
|
|
span,
|
2020-06-08 05:57:56 -05:00
|
|
|
E0761,
|
2020-03-08 03:28:46 -05:00
|
|
|
"file for module `{}` found at both {} and {}",
|
|
|
|
mod_name,
|
|
|
|
default_path_str,
|
|
|
|
secondary_path_str,
|
|
|
|
);
|
|
|
|
err.help("delete or rename one of them to remove the ambiguity");
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ModulePath { name: mod_name, path_exists: default_exists || secondary_exists, result }
|
|
|
|
}
|