expand: Remove obsolete DirectoryOwnership::UnownedViaMod

This ownership kind is only constructed in the case of path attributes like `#[path = ".."]` without a file name segment, which always represent some kind of directories and will produce and error on attempt to parse them as a module file.
This commit is contained in:
Vadim Petrochenkov 2021-02-22 16:12:11 +03:00
parent 45b3c28518
commit bc18eb4717
3 changed files with 24 additions and 52 deletions

View File

@ -22,7 +22,6 @@ pub enum DirectoryOwnership {
relative: Option<Ident>, relative: Option<Ident>,
}, },
UnownedViaBlock, UnownedViaBlock,
UnownedViaMod,
} }
/// Information about the path to a module. /// Information about the path to a module.
@ -134,23 +133,20 @@ fn submod_path<'a>(
dir_path: &Path, dir_path: &Path,
) -> PResult<'a, ModulePathSuccess> { ) -> PResult<'a, ModulePathSuccess> {
if let Some(path) = submod_path_from_attr(sess, attrs, dir_path) { if let Some(path) = submod_path_from_attr(sess, attrs, dir_path) {
let ownership = match path.file_name().and_then(|s| s.to_str()) { // 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, //
// // 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 };
Some(_) => DirectoryOwnership::Owned { relative: None },
_ => DirectoryOwnership::UnownedViaMod,
};
return Ok(ModulePathSuccess { ownership, path }); return Ok(ModulePathSuccess { ownership, path });
} }
let relative = match ownership { let relative = match ownership {
DirectoryOwnership::Owned { relative } => relative, DirectoryOwnership::Owned { relative } => relative,
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None, DirectoryOwnership::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, id, span, relative, dir_path);
@ -160,10 +156,6 @@ fn submod_path<'a>(
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)
} }
DirectoryOwnership::UnownedViaMod => {
let _ = result.map_err(|mut err| err.cancel());
error_cannot_declare_mod_here(&sess.parse_sess, span, path_exists, &name)
}
} }
} }
@ -182,41 +174,6 @@ fn error_decl_mod_in_block<'a, T>(
Err(err) Err(err)
} }
fn error_cannot_declare_mod_here<'a, T>(
sess: &'a ParseSess,
span: Span,
path_exists: bool,
name: &str,
) -> PResult<'a, T> {
let mut err =
sess.span_diagnostic.struct_span_err(span, "cannot declare a new module at this location");
if !span.is_dummy() {
if let FileName::Real(src_name) = sess.source_map().span_to_filename(span) {
let src_path = src_name.into_local_path();
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(
span,
&format!(
"maybe move this module `{}` to its own directory via `{}`",
src_path.display(),
dest_path.display()
),
);
}
}
}
if path_exists {
err.span_note(
span,
&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"]`. /// 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( pub(super) fn submod_path_from_attr(

View File

@ -0,0 +1,7 @@
// normalize-stderr-test: "\.:.*\(" -> ".: $$ACCESS_DENIED_MSG ("
// normalize-stderr-test: "os error \d+" -> "os error $$ACCESS_DENIED_CODE"
#[path = "."]
mod m; //~ ERROR couldn't read
fn main() {}

View File

@ -0,0 +1,8 @@
error: couldn't read $DIR/.: $ACCESS_DENIED_MSG (os error $ACCESS_DENIED_CODE)
--> $DIR/path-no-file-name.rs:5:1
|
LL | mod m;
| ^^^^^^
error: aborting due to previous error