Skip to content

Commit de2070b

Browse files
committed
Allow foo.rs to be parent to foo/bar.rs
Prior to this commit, in order for a module to have submodules, it had to be at the `/foo/mod.rs` filepath. After this commit, moudles at the `/foo.rs` filepath have submodules located in the `/foo/` directory.
1 parent 29dece1 commit de2070b

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/libsyntax/ext/expand.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -898,9 +898,16 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
898898
} else {
899899
let mut path =
900900
PathBuf::from(self.cx.parse_sess.codemap().span_to_filename(inner));
901-
let directory_ownership = match path.file_name().unwrap().to_str() {
902-
Some("mod.rs") => DirectoryOwnership::Owned,
903-
_ => DirectoryOwnership::UnownedViaMod(false),
901+
let directory_ownership = {
902+
let file_name = path.file_name().unwrap();
903+
match file_name.to_str() {
904+
Some("mod.rs") => DirectoryOwnership::Owned,
905+
Some(s) if s.ends_with(".rs") && s.len() > 3 => {
906+
let directory_name = s.rsplitn(2, '.').skip(1).next().unwrap();
907+
DirectoryOwnership::OwnedUnder(Ident::from_str(directory_name))
908+
}
909+
_ => DirectoryOwnership::UnownedViaMod(true),
910+
}
904911
};
905912
path.pop();
906913
module.directory = path;

src/libsyntax/parse/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! The main parser interface
1212
13-
use ast::{self, CrateConfig};
13+
use ast::{self, CrateConfig, Ident};
1414
use codemap::CodeMap;
1515
use syntax_pos::{self, Span, FileMap};
1616
use errors::{Handler, ColorConfig, DiagnosticBuilder};
@@ -87,6 +87,7 @@ pub enum DirectoryOwnership {
8787
Owned,
8888
UnownedViaBlock,
8989
UnownedViaMod(bool /* legacy warnings? */),
90+
OwnedUnder(Ident),
9091
}
9192

9293
// a bunch of utility functions of the form parse_<thing>_from_<source>

src/libsyntax/parse/parser.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -5146,20 +5146,28 @@ impl<'a> Parser<'a> {
51465146
}
51475147

51485148
/// Returns either a path to a module, or .
5149-
pub fn default_submod_path(id: ast::Ident, dir_path: &Path, codemap: &CodeMap) -> ModulePath
5149+
pub fn default_submod_path(id: ast::Ident, dir: &Directory, codemap: &CodeMap) -> ModulePath
51505150
{
51515151
let mod_name = id.to_string();
5152+
let dir_path = &dir.path;
51525153
let default_path_str = format!("{}.rs", mod_name);
51535154
let secondary_path_str = format!("{}/mod.rs", mod_name);
5154-
let default_path = dir_path.join(&default_path_str);
5155-
let secondary_path = dir_path.join(&secondary_path_str);
5155+
5156+
let (default_path, secondary_path) = match dir.ownership {
5157+
DirectoryOwnership::OwnedUnder(under_id) => {
5158+
let under_path = dir_path.join(under_id.to_string());
5159+
(under_path.join(&default_path_str), under_path.join(&secondary_path_str))
5160+
}
5161+
_ => (dir_path.join(&default_path_str), dir_path.join(&secondary_path_str)),
5162+
};
5163+
51565164
let default_exists = codemap.file_exists(&default_path);
51575165
let secondary_exists = codemap.file_exists(&secondary_path);
51585166

51595167
let result = match (default_exists, secondary_exists) {
51605168
(true, false) => Ok(ModulePathSuccess {
51615169
path: default_path,
5162-
directory_ownership: DirectoryOwnership::UnownedViaMod(false),
5170+
directory_ownership: DirectoryOwnership::OwnedUnder(id),
51635171
warn: false,
51645172
}),
51655173
(false, true) => Ok(ModulePathSuccess {
@@ -5195,17 +5203,25 @@ impl<'a> Parser<'a> {
51955203
outer_attrs: &[ast::Attribute],
51965204
id_sp: Span) -> PResult<'a, ModulePathSuccess> {
51975205
if let Some(path) = Parser::submod_path_from_attr(outer_attrs, &self.directory.path) {
5198-
return Ok(ModulePathSuccess {
5199-
directory_ownership: match path.file_name().and_then(|s| s.to_str()) {
5206+
let ownership = {
5207+
let file_name = path.file_name().unwrap();
5208+
match file_name.to_str() {
52005209
Some("mod.rs") => DirectoryOwnership::Owned,
5210+
Some(s) if s.ends_with(".rs") && s.len() > 3 => {
5211+
let directory_name = s.rsplitn(2, '.').skip(1).next().unwrap();
5212+
DirectoryOwnership::OwnedUnder(Ident::from_str(directory_name))
5213+
}
52015214
_ => DirectoryOwnership::UnownedViaMod(true),
5202-
},
5215+
}
5216+
};
5217+
return Ok(ModulePathSuccess {
5218+
directory_ownership: ownership,
52035219
path: path,
52045220
warn: false,
52055221
});
52065222
}
52075223

5208-
let paths = Parser::default_submod_path(id, &self.directory.path, self.sess.codemap());
5224+
let paths = Parser::default_submod_path(id, &self.directory, self.sess.codemap());
52095225

52105226
if let DirectoryOwnership::UnownedViaBlock = self.directory.ownership {
52115227
let msg =

0 commit comments

Comments
 (0)