[gcc r14-7648] gccrs: Fix non-mod-rs files' external module loading paths

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:50:25 GMT 2024


https://gcc.gnu.org/g:e6d40678dd1829a8b24b61aa07dc647ffacd06e6

commit r14-7648-ge6d40678dd1829a8b24b61aa07dc647ffacd06e6
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date:   Sun May 28 09:44:39 2023 -0400

    gccrs: Fix non-mod-rs files' external module loading paths
    
    gcc/rust/ChangeLog:
    
            * Make-lang.in: Add "rust-dir-owner.o".
            * ast/rust-ast.cc: Include "rust-dir-owner.h".
            (Module::process_file_path):
            Handle non-mod-rs external file modules properly.
            * parse/rust-parse-impl.h: Include "rust-dir-owner.h".
            (Parser::parse_module):
            Handle non-mod-rs external file modules properly.
            * util/rust-dir-owner.cc: New file.
            * util/rust-dir-owner.h: New file.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/test_mod.rs: Moved to...
            * rust/compile/issue-1089/test_mod.rs: ...here.
            * rust/compile/mod_missing_middle.rs: Fix paths.
            * rust/compile/missing_middle/both_path.rs: Moved to...
            * rust/compile/mod_missing_middle/missing_middle/both_path.rs: ...here.
            * rust/compile/missing_middle/explicit.not.rs: Moved to...
            * rust/compile/mod_missing_middle/missing_middle/explicit.not.rs: ...here.
            * rust/compile/missing_middle/other.rs: Moved to...
            * rust/compile/mod_missing_middle/missing_middle/explicit.not/other.rs: ...here.
            * rust/compile/missing_middle/inner_path.rs: Moved to...
            * rust/compile/mod_missing_middle/missing_middle/inner_path.rs: ...here.
            * rust/compile/missing_middle/outer_path.rs: Moved to...
            * rust/compile/mod_missing_middle/missing_middle/outer_path.rs: ...here.
            * rust/compile/missing_middle/sub/mod.rs: Moved to...
            * rust/compile/mod_missing_middle/missing_middle/sub/mod.rs: ...here.
            * rust/compile/torture/modules/mod.rs: Moved to...
            * rust/compile/torture/extern_mod1/modules/mod.rs: ...here.
            * rust/execute/torture/modules/mod.rs: Moved to...
            * rust/execute/torture/extern_mod4/modules/mod.rs: ...here.
    
    Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>

Diff:
---
 gcc/rust/Make-lang.in                              |  1 +
 gcc/rust/ast/rust-ast.cc                           | 32 ++++++++++++-----
 gcc/rust/parse/rust-parse-impl.h                   | 20 ++++++++++-
 gcc/rust/util/rust-dir-owner.cc                    | 42 ++++++++++++++++++++++
 gcc/rust/util/rust-dir-owner.h                     | 34 ++++++++++++++++++
 .../rust/compile/{ => issue-1089}/test_mod.rs      |  0
 gcc/testsuite/rust/compile/mod_missing_middle.rs   |  6 ++--
 .../missing_middle/both_path.rs                    |  0
 .../missing_middle/explicit.not.rs                 |  0
 .../missing_middle/explicit.not}/other.rs          |  0
 .../missing_middle/inner_path.rs                   |  0
 .../missing_middle/outer_path.rs                   |  0
 .../missing_middle/sub/mod.rs                      |  0
 .../torture/{ => extern_mod1}/modules/mod.rs       |  0
 .../torture/{ => extern_mod4}/modules/mod.rs       |  0
 15 files changed, 123 insertions(+), 12 deletions(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 771c75f1432..03a92ac8874 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -178,6 +178,7 @@ GRS_OBJS = \
     rust/rust-builtins.o \
     rust/rust-feature.o \
     rust/rust-feature-gate.o \
+    rust/rust-dir-owner.o \
     $(END)
 # removed object files from here
 
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index d2550b08607..06b28e0f001 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -26,6 +26,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "rust-lex.h"
 #include "rust-parse.h"
 #include "rust-operators.h"
+#include "rust-dir-owner.h"
 
 /* Compilation unit used for various AST-related functions that would make
  * the headers too long if they were defined inline and don't receive any
@@ -3310,21 +3311,37 @@ Module::process_file_path ()
 
   // This corresponds to the path of the file 'including' the module. So the
   // file that contains the 'mod <file>;' directive
-  std::string including_fname (outer_filename);
+  std::string including_fpath (outer_filename);
 
   std::string expected_file_path = module_name + ".rs";
   std::string expected_dir_path = "mod.rs";
 
-  auto dir_slash_pos = including_fname.rfind (file_separator);
+  auto dir_slash_pos = including_fpath.rfind (file_separator);
   std::string current_directory_name;
+  std::string including_fname;
 
-  // If we haven't found a file_separator, then we have to look for files in the
-  // current directory ('.')
+  // If we haven't found a file_separator, then we may have to look for files in
+  // the current directory ('.')
   if (dir_slash_pos == std::string::npos)
-    current_directory_name = std::string (".") + file_separator;
+    {
+      including_fname = std::move (including_fpath);
+      including_fpath = std::string (".") + file_separator + including_fname;
+      dir_slash_pos = 1;
+    }
   else
-    current_directory_name
-      = including_fname.substr (0, dir_slash_pos) + file_separator;
+    {
+      including_fname = including_fpath.substr (dir_slash_pos + 1);
+    }
+
+  current_directory_name
+    = including_fpath.substr (0, dir_slash_pos) + file_separator;
+
+  auto path_string = filename_from_path_attribute (get_outer_attrs ());
+
+  std::string including_subdir;
+  if (path_string.empty () && module_scope.empty ()
+      && get_file_subdir (including_fname, including_subdir))
+    current_directory_name += including_subdir + file_separator;
 
   // Handle inline module declarations adding path components.
   for (auto const &name : module_scope)
@@ -3333,7 +3350,6 @@ Module::process_file_path ()
       current_directory_name.append (file_separator);
     }
 
-  auto path_string = filename_from_path_attribute (get_outer_attrs ());
   if (!path_string.empty ())
     {
       module_file = current_directory_name + path_string;
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 80ffbcc22f9..6eb5eb6e741 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -25,6 +25,7 @@
 #define INCLUDE_ALGORITHM
 #include "rust-diagnostics.h"
 #include "rust-make-unique.h"
+#include "rust-dir-owner.h"
 
 namespace Rust {
 // Left binding powers of operations.
@@ -2430,8 +2431,25 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
 	// parse inner attributes
 	AST::AttrVec inner_attrs = parse_inner_attributes ();
 
+	std::string default_path = name;
+
+	if (inline_module_stack.empty ())
+	  {
+	    std::string filename = lexer.get_filename ();
+	    auto slash_idx = filename.rfind (file_separator);
+	    if (slash_idx == std::string::npos)
+	      slash_idx = 0;
+	    else
+	      slash_idx++;
+	    filename = filename.substr (slash_idx);
+
+	    std::string subdir;
+	    if (get_file_subdir (filename, subdir))
+	      default_path = subdir + file_separator + name;
+	  }
+
 	std::string module_path_name
-	  = extract_module_path (inner_attrs, outer_attrs, name);
+	  = extract_module_path (inner_attrs, outer_attrs, default_path);
 	InlineModuleStackScope scope (*this, std::move (module_path_name));
 
 	// parse items
diff --git a/gcc/rust/util/rust-dir-owner.cc b/gcc/rust/util/rust-dir-owner.cc
new file mode 100644
index 00000000000..24bbe7b3a28
--- /dev/null
+++ b/gcc/rust/util/rust-dir-owner.cc
@@ -0,0 +1,42 @@
+// Copyright (C) 2023 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// Handles non-mod-rs and mod-rs file differentiation
+
+#include "rust-system.h"
+#include "rust-dir-owner.h"
+
+namespace Rust {
+
+// extracts the owned subdirectory name from a file name
+bool
+get_file_subdir (const std::string &filename, std::string &subdir)
+{
+  // directory owning filenames
+  if (filename == "mod.rs" || filename == "lib.rs" || filename == "main.rs")
+    return false;
+
+  // files not ending in ".rs" are directory owners
+  if (filename.size () < 3 || filename.compare (filename.size () - 3, 3, ".rs"))
+    return false;
+
+  subdir = filename.substr (0, filename.size () - 3);
+  return true;
+}
+
+} // namespace Rust
diff --git a/gcc/rust/util/rust-dir-owner.h b/gcc/rust/util/rust-dir-owner.h
new file mode 100644
index 00000000000..0134f54a428
--- /dev/null
+++ b/gcc/rust/util/rust-dir-owner.h
@@ -0,0 +1,34 @@
+// Copyright (C) 2023 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// Handles non-mod-rs and mod-rs file differentiation
+
+#ifndef RUST_DIR_OWNER
+#define RUST_DIR_OWNER
+
+#include "rust-system.h"
+
+namespace Rust {
+
+// extracts the owned subdirectory name from a file name
+bool
+get_file_subdir (const std::string &filename, std::string &subdir);
+
+} // namespace Rust
+
+#endif // RUST_DIR_OWNER
diff --git a/gcc/testsuite/rust/compile/test_mod.rs b/gcc/testsuite/rust/compile/issue-1089/test_mod.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/test_mod.rs
rename to gcc/testsuite/rust/compile/issue-1089/test_mod.rs
diff --git a/gcc/testsuite/rust/compile/mod_missing_middle.rs b/gcc/testsuite/rust/compile/mod_missing_middle.rs
index 79633407671..0f8371345ac 100644
--- a/gcc/testsuite/rust/compile/mod_missing_middle.rs
+++ b/gcc/testsuite/rust/compile/mod_missing_middle.rs
@@ -7,20 +7,20 @@ mod missing_middle {
     mod explicit;
 }
 
-#[path = "missing_middle"]
+#[path = "mod_missing_middle/missing_middle"]
 mod with_outer_path_attr {
     #[path = "outer_path.rs"]
     mod inner;
 }
 
 mod with_inner_path_attr {
-    #![path = "missing_middle"]
+    #![path = "mod_missing_middle/missing_middle"]
 
     #[path = "inner_path.rs"]
     mod inner;
 }
 
-#[path = "missing_middle"]
+#[path = "mod_missing_middle/missing_middle"]
 mod with_both_path_attr {
     #![path = "this_is_ignored"]
 
diff --git a/gcc/testsuite/rust/compile/missing_middle/both_path.rs b/gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/both_path.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/missing_middle/both_path.rs
rename to gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/both_path.rs
diff --git a/gcc/testsuite/rust/compile/missing_middle/explicit.not.rs b/gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/explicit.not.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/missing_middle/explicit.not.rs
rename to gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/explicit.not.rs
diff --git a/gcc/testsuite/rust/compile/missing_middle/other.rs b/gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/explicit.not/other.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/missing_middle/other.rs
rename to gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/explicit.not/other.rs
diff --git a/gcc/testsuite/rust/compile/missing_middle/inner_path.rs b/gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/inner_path.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/missing_middle/inner_path.rs
rename to gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/inner_path.rs
diff --git a/gcc/testsuite/rust/compile/missing_middle/outer_path.rs b/gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/outer_path.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/missing_middle/outer_path.rs
rename to gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/outer_path.rs
diff --git a/gcc/testsuite/rust/compile/missing_middle/sub/mod.rs b/gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/sub/mod.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/missing_middle/sub/mod.rs
rename to gcc/testsuite/rust/compile/mod_missing_middle/missing_middle/sub/mod.rs
diff --git a/gcc/testsuite/rust/compile/torture/modules/mod.rs b/gcc/testsuite/rust/compile/torture/extern_mod1/modules/mod.rs
similarity index 100%
rename from gcc/testsuite/rust/compile/torture/modules/mod.rs
rename to gcc/testsuite/rust/compile/torture/extern_mod1/modules/mod.rs
diff --git a/gcc/testsuite/rust/execute/torture/modules/mod.rs b/gcc/testsuite/rust/execute/torture/extern_mod4/modules/mod.rs
similarity index 100%
rename from gcc/testsuite/rust/execute/torture/modules/mod.rs
rename to gcc/testsuite/rust/execute/torture/extern_mod4/modules/mod.rs


More information about the Gcc-cvs mailing list