From bc18eb471772403de20cd9bc0a836ce1f5e09e98 Mon Sep 17 00:00:00 2001
From: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>
Date: Mon, 22 Feb 2021 16:12:11 +0300
Subject: [PATCH] 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.
---
 compiler/rustc_expand/src/module.rs          | 61 +++-----------------
 src/test/ui/modules/path-no-file-name.rs     |  7 +++
 src/test/ui/modules/path-no-file-name.stderr |  8 +++
 3 files changed, 24 insertions(+), 52 deletions(-)
 create mode 100644 src/test/ui/modules/path-no-file-name.rs
 create mode 100644 src/test/ui/modules/path-no-file-name.stderr

diff --git a/compiler/rustc_expand/src/module.rs b/compiler/rustc_expand/src/module.rs
index 076d3b02be9..99c1591d40a 100644
--- a/compiler/rustc_expand/src/module.rs
+++ b/compiler/rustc_expand/src/module.rs
@@ -22,7 +22,6 @@ pub enum DirectoryOwnership {
         relative: Option<Ident>,
     },
     UnownedViaBlock,
-    UnownedViaMod,
 }
 
 /// Information about the path to a module.
@@ -134,23 +133,20 @@ fn submod_path<'a>(
     dir_path: &Path,
 ) -> PResult<'a, ModulePathSuccess> {
     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.
-            // 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,
-        };
+        // 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 ownership = DirectoryOwnership::Owned { relative: None };
         return Ok(ModulePathSuccess { ownership, path });
     }
 
     let relative = match ownership {
         DirectoryOwnership::Owned { relative } => relative,
-        DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
+        DirectoryOwnership::UnownedViaBlock => None,
     };
     let ModulePath { path_exists, name, result } =
         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());
             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)
 }
 
-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"]`.
 /// The provided `dir_path` is joined with the `path_string`.
 pub(super) fn submod_path_from_attr(
diff --git a/src/test/ui/modules/path-no-file-name.rs b/src/test/ui/modules/path-no-file-name.rs
new file mode 100644
index 00000000000..f62cd2a9eb4
--- /dev/null
+++ b/src/test/ui/modules/path-no-file-name.rs
@@ -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() {}
diff --git a/src/test/ui/modules/path-no-file-name.stderr b/src/test/ui/modules/path-no-file-name.stderr
new file mode 100644
index 00000000000..32a213c68f6
--- /dev/null
+++ b/src/test/ui/modules/path-no-file-name.stderr
@@ -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
+