Skip to content

Commit c46eb66

Browse files
committed
rust: track inline module scopes for module file resolution
The set of inline modules is required to find the expected location of a module file. Track this information with an RAII object (`InlineModuleStackScope`) and pass it down to any out-of-line modules so that, when requested, the set of inline modules can be added to the search path. Signed-off-by: Ben Boeckel <[email protected]>
1 parent 09af9b1 commit c46eb66

File tree

8 files changed

+151
-11
lines changed

8 files changed

+151
-11
lines changed

gcc/rust/ast/rust-ast-full-test.cc

+13-6
Original file line numberDiff line numberDiff line change
@@ -4057,6 +4057,13 @@ Module::process_file_path ()
40574057
current_directory_name
40584058
= including_fname.substr (0, dir_slash_pos) + file_separator;
40594059

4060+
// Handle inline module declarations adding path components.
4061+
for (auto const &name : module_scope)
4062+
{
4063+
current_directory_name.append (name);
4064+
current_directory_name.append (file_separator);
4065+
}
4066+
40604067
auto path_string = filename_from_path_attribute (get_outer_attrs ());
40614068
if (!path_string.empty ())
40624069
{
@@ -4070,12 +4077,13 @@ Module::process_file_path ()
40704077
// current file is titled `mod.rs`.
40714078

40724079
// First, we search for <directory>/<module_name>.rs
4073-
bool file_mod_found
4074-
= file_exists (current_directory_name + expected_file_path);
4080+
std::string file_mod_path = current_directory_name + expected_file_path;
4081+
bool file_mod_found = file_exists (file_mod_path);
40754082

40764083
// Then, search for <directory>/<module_name>/mod.rs
4077-
current_directory_name += module_name + file_separator;
4078-
bool dir_mod_found = file_exists (current_directory_name + expected_dir_path);
4084+
std::string dir_mod_path
4085+
= current_directory_name + module_name + file_separator + expected_dir_path;
4086+
bool dir_mod_found = file_exists (dir_mod_path);
40794087

40804088
bool multiple_candidates_found = file_mod_found && dir_mod_found;
40814089
bool no_candidates_found = !file_mod_found && !dir_mod_found;
@@ -4093,8 +4101,7 @@ Module::process_file_path ()
40934101
if (no_candidates_found || multiple_candidates_found)
40944102
return;
40954103

4096-
module_file = file_mod_found ? expected_file_path
4097-
: current_directory_name + expected_dir_path;
4104+
module_file = std::move (file_mod_found ? file_mod_path : dir_mod_path);
40984105
}
40994106

41004107
void

gcc/rust/ast/rust-item.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,8 @@ class Module : public VisItem
991991
std::vector<Attribute> inner_attrs;
992992
// bool has_items;
993993
std::vector<std::unique_ptr<Item>> items;
994+
// Names of including inline modules (immediate parent is last in the list)
995+
std::vector<std::string> module_scope;
994996

995997
// Filename the module refers to. Empty string on LOADED modules or if an
996998
// error occured when dealing with UNLOADED modules
@@ -1013,11 +1015,12 @@ class Module : public VisItem
10131015
// Unloaded module constructor
10141016
Module (Identifier module_name, Visibility visibility,
10151017
std::vector<Attribute> outer_attrs, Location locus,
1016-
std::string outer_filename)
1018+
std::string outer_filename, std::vector<std::string> module_scope)
10171019
: VisItem (std::move (visibility), std::move (outer_attrs)),
10181020
module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
10191021
outer_filename (outer_filename), inner_attrs (std::vector<Attribute> ()),
1020-
items (std::vector<std::unique_ptr<Item>> ())
1022+
items (std::vector<std::unique_ptr<Item>> ()),
1023+
module_scope (std::move (module_scope))
10211024
{}
10221025

10231026
// Loaded module constructor, with items
@@ -1035,7 +1038,8 @@ class Module : public VisItem
10351038
// Copy constructor with vector clone
10361039
Module (Module const &other)
10371040
: VisItem (other), module_name (other.module_name), locus (other.locus),
1038-
kind (other.kind), inner_attrs (other.inner_attrs)
1041+
kind (other.kind), inner_attrs (other.inner_attrs),
1042+
module_scope (other.module_scope)
10391043
{
10401044
// We need to check whether we are copying a loaded module or an unloaded
10411045
// one. In the second case, clear the `items` vector.
@@ -1054,6 +1058,7 @@ class Module : public VisItem
10541058
locus = other.locus;
10551059
kind = other.kind;
10561060
inner_attrs = other.inner_attrs;
1061+
module_scope = other.module_scope;
10571062

10581063
// Likewise, we need to clear the `items` vector in case the other module is
10591064
// unloaded

gcc/rust/parse/rust-parse-impl.h

+78-2
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,78 @@ Parser<ManagedTokenSource>::parse_visibility ()
20782078
}
20792079
}
20802080

2081+
static std::string
2082+
extract_module_path (const AST::AttrVec &inner_attrs, const AST::AttrVec &outer_attrs, const std::string &name)
2083+
{
2084+
AST::Attribute path_attr = AST::Attribute::create_empty ();
2085+
for (const auto &attr : inner_attrs)
2086+
{
2087+
if (attr.get_path ().as_string () == "path")
2088+
{
2089+
path_attr = attr;
2090+
break;
2091+
}
2092+
}
2093+
2094+
// Here, we found a path attribute, but it has no associated string. This is
2095+
// invalid
2096+
if (!path_attr.is_empty () && !path_attr.has_attr_input ())
2097+
{
2098+
rust_error_at (
2099+
path_attr.get_locus (),
2100+
// Split the format string so that -Wformat-diag does not complain...
2101+
"path attributes must contain a filename: '%s'", "#![path = \"file\"]");
2102+
return name;
2103+
}
2104+
2105+
for (const auto &attr : outer_attrs)
2106+
{
2107+
if (attr.get_path ().as_string () == "path")
2108+
{
2109+
path_attr = attr;
2110+
break;
2111+
}
2112+
}
2113+
2114+
// We didn't find a path attribute. This is not an error, there simply isn't
2115+
// one present
2116+
if (path_attr.is_empty ())
2117+
return name;
2118+
2119+
// Here, we found a path attribute, but it has no associated string. This is
2120+
// invalid
2121+
if (!path_attr.has_attr_input ())
2122+
{
2123+
rust_error_at (
2124+
path_attr.get_locus (),
2125+
// Split the format string so that -Wformat-diag does not complain...
2126+
"path attributes must contain a filename: '%s'", "#[path = \"file\"]");
2127+
return name;
2128+
}
2129+
2130+
auto path_value = path_attr.get_attr_input ().as_string ();
2131+
2132+
// At this point, the 'path' is of the following format: '= "<file.rs>"'
2133+
// We need to remove the equal sign and only keep the actual filename.
2134+
// In order to do this, we can simply go through the string until we find
2135+
// a character that is not an equal sign or whitespace
2136+
auto filename_begin = path_value.find_first_not_of ("=\t ");
2137+
2138+
auto path = path_value.substr (filename_begin);
2139+
2140+
// On windows, the path might mix '/' and '\' separators. Replace the
2141+
// UNIX-like separators by MSDOS separators to make sure the path will resolve
2142+
// properly.
2143+
//
2144+
// Source: rustc compiler
2145+
// (https://github.com/rust-lang/rust/blob/9863bf51a52b8e61bcad312f81b5193d53099f9f/compiler/rustc_expand/src/module.rs#L174)
2146+
#if defined(HAVE_DOS_BASED_FILE_SYSTEM)
2147+
path.replace ('/', '\\');
2148+
#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2149+
2150+
return path;
2151+
}
2152+
20812153
// Parses a module - either a bodied module or a module defined in another file.
20822154
template <typename ManagedTokenSource>
20832155
std::unique_ptr<AST::Module>
@@ -2104,14 +2176,18 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
21042176
// Construct an external module
21052177
return std::unique_ptr<AST::Module> (
21062178
new AST::Module (std::move (name), std::move (vis),
2107-
std::move (outer_attrs), locus,
2108-
lexer.get_filename ()));
2179+
std::move (outer_attrs), locus, lexer.get_filename (),
2180+
inline_module_stack));
21092181
case LEFT_CURLY: {
21102182
lexer.skip_token ();
21112183

21122184
// parse inner attributes
21132185
AST::AttrVec inner_attrs = parse_inner_attributes ();
21142186

2187+
std::string module_path_name = extract_module_path (inner_attrs, outer_attrs, name);
2188+
rust_debug ("pushing inline module path for %s: %s", name.c_str (), module_path_name.c_str ());
2189+
InlineModuleStackScope scope (*this, std::move (module_path_name));
2190+
21152191
// parse items
21162192
std::vector<std::unique_ptr<AST::Item>> items;
21172193
const_TokenPtr tok = lexer.peek_token ();

gcc/rust/parse/rust-parse.h

+15
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,21 @@ template <typename ManagedTokenSource> class Parser
622622
ManagedTokenSource lexer;
623623
// The error list.
624624
std::vector<Error> error_table;
625+
// The names of inline modules while parsing.
626+
std::vector<std::string> inline_module_stack;
627+
628+
class InlineModuleStackScope
629+
{
630+
private:
631+
Parser &parser;
632+
633+
public:
634+
InlineModuleStackScope (Parser &parser, std::string name) : parser (parser)
635+
{
636+
parser.inline_module_stack.emplace_back (std::move (name));
637+
}
638+
~InlineModuleStackScope () { parser.inline_module_stack.pop_back (); }
639+
};
625640
};
626641
} // namespace Rust
627642

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod other;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn f() -> u32 {
2+
2
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn f() -> u32 {
2+
1
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// { dg-additional-options "-w" }
2+
3+
mod missing_middle {
4+
mod sub;
5+
6+
#[path = "explicit.not.rs"]
7+
mod explicit;
8+
}
9+
10+
#[path = "missing_middle"]
11+
mod with_outer_path_attr {
12+
#[path = "outer_path.rs"]
13+
mod inner;
14+
}
15+
16+
// FIXME: the parser doesn't accept inner attributes for inline modules.
17+
// mod with_inner_path_attr {
18+
// #![path = "missing_middle"]
19+
20+
// #[path = "explicit.not.rs"]
21+
// mod inner;
22+
// }
23+
24+
// #![path = "missing_middle"]
25+
// mod with_both_path_attr {
26+
// #![path = "this_is_ignored"]
27+
28+
// #[path = "explicit.not.rs"]
29+
// mod inner;
30+
// }

0 commit comments

Comments
 (0)