rust/compiler/rustc_expand/src/module.rs

266 lines
9.1 KiB
Rust
Raw Normal View History

use crate::base::ModuleData;
use rustc_ast::ptr::P;
use rustc_ast::{token, Attribute, Item};
2020-03-08 08:28:46 +00:00
use rustc_errors::{struct_span_err, PResult};
2020-03-21 22:10:10 +00:00
use rustc_parse::new_parser_from_file;
2020-03-08 08:28:46 +00:00
use rustc_session::parse::ParseSess;
use rustc_session::Session;
2020-04-19 11:00:18 +00:00
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
2019-10-11 11:06:36 +00:00
2019-08-11 16:34:42 +00:00
use std::path::{self, Path, PathBuf};
2020-03-08 21:10:37 +00:00
#[derive(Copy, Clone)]
pub enum DirOwnership {
2020-03-08 21:10:37 +00:00
Owned {
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
2020-04-19 11:00:18 +00:00
relative: Option<Ident>,
2020-03-08 21:10:37 +00:00
},
UnownedViaBlock,
}
2019-08-11 16:34:42 +00:00
/// Information about the path to a module.
2020-01-11 19:19:57 +00:00
// Public for rustfmt usage.
2020-03-08 08:28:46 +00:00
pub struct ModulePath<'a> {
2019-08-11 16:34:42 +00:00
name: String,
path_exists: bool,
2020-03-08 08:28:46 +00:00
pub result: PResult<'a, ModulePathSuccess>,
2019-08-11 16:34:42 +00:00
}
2020-01-11 19:19:57 +00:00
// Public for rustfmt usage.
pub struct ModulePathSuccess {
pub file_path: PathBuf,
pub dir_ownership: DirOwnership,
2019-08-11 16:34:42 +00:00
}
crate struct ParsedExternalMod {
pub items: Vec<P<Item>>,
pub inner_span: Span,
pub file_path: PathBuf,
pub dir_path: PathBuf,
pub dir_ownership: DirOwnership,
}
crate fn parse_external_mod(
sess: &Session,
ident: Ident,
2020-03-09 10:16:00 +00:00
span: Span, // The span to blame on errors.
module: &ModuleData,
mut dir_ownership: DirOwnership,
2020-03-08 12:36:20 +00:00
attrs: &mut Vec<Attribute>,
) -> ParsedExternalMod {
2020-03-08 12:36:20 +00:00
// 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 = mod_file_path(sess, ident, span, &attrs, &module.dir_path, dir_ownership)?;
dir_ownership = mp.dir_ownership;
2020-03-08 12:36:20 +00:00
// Ensure file paths are acyclic.
error_on_circular_module(&sess.parse_sess, span, &mp.file_path, &module.file_path_stack)?;
2020-03-08 12:36:20 +00:00
2020-03-21 21:51:03 +00:00
// Actually parse the external file as a module.
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.file_path)
2020-03-08 12:36:20 +00:00
};
// (1) ...instead, we return a dummy module.
let (items, inner_span, file_path) = result.map_err(|mut err| err.emit()).unwrap_or_default();
2020-03-08 12:36:20 +00:00
// Extract the directory path for submodules of the module.
let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
2020-03-08 12:36:20 +00:00
ParsedExternalMod { items, inner_span, file_path, dir_path, dir_ownership }
2020-03-08 08:54:19 +00:00
}
2020-03-07 18:53:25 +00:00
2020-03-08 12:29:37 +00:00
fn error_on_circular_module<'a>(
sess: &'a ParseSess,
span: Span,
file_path: &Path,
file_path_stack: &[PathBuf],
2020-03-08 12:29:37 +00:00
) -> PResult<'a, ()> {
if let Some(i) = file_path_stack.iter().position(|p| *p == file_path) {
2020-03-08 12:29:37 +00:00
let mut err = String::from("circular modules: ");
for p in &file_path_stack[i..] {
2020-03-08 12:29:37 +00:00
err.push_str(&p.to_string_lossy());
err.push_str(" -> ");
}
err.push_str(&file_path.to_string_lossy());
2020-03-08 12:29:37 +00:00
return Err(sess.span_diagnostic.struct_span_err(span, &err[..]));
}
Ok(())
}
crate fn mod_dir_path(
sess: &Session,
ident: Ident,
2020-03-08 08:54:19 +00:00
attrs: &[Attribute],
module: &ModuleData,
mut dir_ownership: DirOwnership,
) -> (PathBuf, DirOwnership) {
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.
return (file_path, DirOwnership::Owned { relative: None });
}
// 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());
2019-08-11 16:34:42 +00:00
}
}
dir_path.push(&*ident.as_str());
(dir_path, dir_ownership)
2019-08-11 16:34:42 +00:00
}
2020-03-08 08:28:46 +00:00
fn mod_file_path<'a>(
sess: &'a Session,
ident: Ident,
2020-03-09 10:16:00 +00:00
span: Span,
2020-03-08 11:19:27 +00:00
attrs: &[Attribute],
2020-03-08 08:28:46 +00:00
dir_path: &Path,
dir_ownership: DirOwnership,
2020-03-08 08:28:46 +00:00
) -> PResult<'a, ModulePathSuccess> {
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,
//
// 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 dir_ownership = DirOwnership::Owned { relative: None };
return Ok(ModulePathSuccess { file_path, dir_ownership });
2020-03-08 08:28:46 +00:00
}
let relative = match dir_ownership {
DirOwnership::Owned { relative } => relative,
DirOwnership::UnownedViaBlock => None,
2020-03-08 08:28:46 +00:00
};
let ModulePath { path_exists, name, result } =
default_submod_path(&sess.parse_sess, ident, span, relative, dir_path);
match dir_ownership {
DirOwnership::Owned { .. } => Ok(result?),
DirOwnership::UnownedViaBlock => {
2020-03-08 08:28:46 +00:00
let _ = result.map_err(|mut err| err.cancel());
error_decl_mod_in_block(&sess.parse_sess, span, path_exists, &name)
2020-03-08 08:28:46 +00:00
}
}
}
fn error_decl_mod_in_block<'a, T>(
sess: &'a ParseSess,
2020-03-09 10:16:00 +00:00
span: Span,
2020-03-08 08:28:46 +00: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 10:16:00 +00:00
let mut err = sess.span_diagnostic.struct_span_err(span, msg);
2020-03-08 08:28:46 +00:00
if path_exists {
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it", name);
2020-03-09 10:16:00 +00:00
err.span_note(span, &msg);
2020-03-08 08:28:46 +00:00
}
Err(err)
}
/// Derive a submodule path from the first found `#[path = "path_string"]`.
/// The provided `dir_path` is joined with the `path_string`.
fn mod_file_path_from_attr(
sess: &Session,
attrs: &[Attribute],
dir_path: &Path,
) -> Option<PathBuf> {
2020-03-08 08:28:46 +00:00
// Extract path string from first `#[path = "path_string"]` attribute.
let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?.as_str();
2020-03-08 08:28:46 +00: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,
ident: Ident,
2020-03-09 10:16:00 +00:00
span: Span,
2020-04-19 11:00:18 +00:00
relative: Option<Ident>,
2020-03-08 08:28:46 +00: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/<ident>.rs` and `./foo/<ident>/mod.rs` rather than
// `./<ident>.rs` and `./<ident>/mod.rs`.
2020-03-08 08:28:46 +00: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 {
""
};
let mod_name = ident.name.to_string();
2020-03-08 08:28:46 +00: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);
let result = match (default_exists, secondary_exists) {
(true, false) => Ok(ModulePathSuccess {
file_path: default_path,
dir_ownership: DirOwnership::Owned { relative: Some(ident) },
2020-03-08 08:28:46 +00:00
}),
(false, true) => Ok(ModulePathSuccess {
file_path: secondary_path,
dir_ownership: DirOwnership::Owned { relative: None },
2020-03-08 08:28:46 +00:00
}),
(false, false) => {
let mut err = struct_span_err!(
sess.span_diagnostic,
2020-03-09 10:16:00 +00:00
span,
2020-03-08 08:28:46 +00: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 10:16:00 +00:00
span,
2020-06-08 10:57:56 +00:00
E0761,
2020-03-08 08:28:46 +00: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 }
}