@@ -3984,53 +3984,11 @@ file_exists (const std::string path)
3984
3984
static std::string
3985
3985
filename_from_path_attribute (std::vector<Attribute> &outer_attrs)
3986
3986
{
3987
- Attribute path_attr = Attribute::create_empty ();
3988
- for (auto attr : outer_attrs)
3989
- {
3990
- if (attr.get_path ().as_string () == " path" )
3991
- {
3992
- path_attr = attr;
3993
- break ;
3994
- }
3995
- }
3996
-
3997
- // We didn't find a path attribute. This is not an error, there simply isn't
3998
- // one present
3999
- if (path_attr.is_empty ())
4000
- return " " ;
4001
-
4002
- // Here, we found a path attribute, but it has no associated string. This is
4003
- // invalid
4004
- if (!path_attr.has_attr_input ())
4005
- {
4006
- rust_error_at (
4007
- path_attr.get_locus (),
4008
- // Split the format string so that -Wformat-diag does not complain...
4009
- " path attributes must contain a filename: '%s'" , " #[path = \" file\" ]" );
4010
- return " " ;
4011
- }
4012
-
4013
- auto path_value = path_attr.get_attr_input ().as_string ();
4014
-
4015
- // At this point, the 'path' is of the following format: '= "<file.rs>"'
4016
- // We need to remove the equal sign and only keep the actual filename.
4017
- // In order to do this, we can simply go through the string until we find
4018
- // a character that is not an equal sign or whitespace
4019
- auto filename_begin = path_value.find_first_not_of (" =\t " );
4020
-
4021
- auto path = path_value.substr (filename_begin);
4022
-
4023
- // On windows, the path might mix '/' and '\' separators. Replace the
4024
- // UNIX-like separators by MSDOS separators to make sure the path will resolve
4025
- // properly.
4026
- //
4027
- // Source: rustc compiler
4028
- // (https://github.com/rust-lang/rust/blob/9863bf51a52b8e61bcad312f81b5193d53099f9f/compiler/rustc_expand/src/module.rs#L174)
4029
- #if defined(HAVE_DOS_BASED_FILE_SYSTEM)
4030
- path.replace (' /' , ' \\ ' );
4031
- #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
4032
-
4033
- return path;
3987
+ // An out-of-line module cannot have inner attributes. Additionally, the
3988
+ // default name is specified as `""` so that the caller can detect the case
3989
+ // of "no path given" and use the default path logic (`name.rs` or
3990
+ // `name/mod.rs`).
3991
+ return extract_module_path ({}, outer_attrs, " " );
4034
3992
}
4035
3993
4036
3994
void
@@ -4057,6 +4015,13 @@ Module::process_file_path ()
4057
4015
current_directory_name
4058
4016
= including_fname.substr (0 , dir_slash_pos) + file_separator;
4059
4017
4018
+ // Handle inline module declarations adding path components.
4019
+ for (auto const &name : module_scope)
4020
+ {
4021
+ current_directory_name.append (name);
4022
+ current_directory_name.append (file_separator);
4023
+ }
4024
+
4060
4025
auto path_string = filename_from_path_attribute (get_outer_attrs ());
4061
4026
if (!path_string.empty ())
4062
4027
{
@@ -4070,12 +4035,13 @@ Module::process_file_path ()
4070
4035
// current file is titled `mod.rs`.
4071
4036
4072
4037
// First, we search for <directory>/<module_name>.rs
4073
- bool file_mod_found
4074
- = file_exists (current_directory_name + expected_file_path );
4038
+ std::string file_mod_path = current_directory_name + expected_file_path;
4039
+ bool file_mod_found = file_exists (file_mod_path );
4075
4040
4076
4041
// 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);
4042
+ std::string dir_mod_path
4043
+ = current_directory_name + module_name + file_separator + expected_dir_path;
4044
+ bool dir_mod_found = file_exists (dir_mod_path);
4079
4045
4080
4046
bool multiple_candidates_found = file_mod_found && dir_mod_found;
4081
4047
bool no_candidates_found = !file_mod_found && !dir_mod_found;
@@ -4093,8 +4059,7 @@ Module::process_file_path ()
4093
4059
if (no_candidates_found || multiple_candidates_found)
4094
4060
return ;
4095
4061
4096
- module_file = file_mod_found ? expected_file_path
4097
- : current_directory_name + expected_dir_path;
4062
+ module_file = std::move (file_mod_found ? file_mod_path : dir_mod_path);
4098
4063
}
4099
4064
4100
4065
void
0 commit comments