Skip to content

Commit a74d92e

Browse files
committed
syntax: Bless mod.rs. #4116
When loading a module the parser will look for either foo.rs or foo/mod.rs and generate an error when both are found.
1 parent fe3f75f commit a74d92e

File tree

7 files changed

+82
-16
lines changed

7 files changed

+82
-16
lines changed

src/libsyntax/parse/parser.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -3996,37 +3996,50 @@ impl Parser {
39963996
let prefix = prefix.dir_path();
39973997
let mod_path_stack = &*self.mod_path_stack;
39983998
let mod_path = Path(".").push_many(*mod_path_stack);
3999+
let dir_path = prefix.push_many(mod_path.components);
39994000
let file_path = match ::attr::first_attr_value_str_by_name(
40004001
outer_attrs, "path") {
40014002
Some(d) => {
40024003
let path = Path(d);
40034004
if !path.is_absolute {
4004-
mod_path.push(d)
4005+
dir_path.push(d)
40054006
} else {
40064007
path
40074008
}
40084009
}
4009-
None => mod_path.push(token::interner_get(id.name) + ".rs") // default
4010+
None => {
4011+
let mod_name = token::interner_get(id.name).to_owned();
4012+
let default_path_str = mod_name + ".rs";
4013+
let secondary_path_str = mod_name + "/mod.rs";
4014+
let default_path = dir_path.push(default_path_str);
4015+
let secondary_path = dir_path.push(secondary_path_str);
4016+
let default_exists = default_path.exists();
4017+
let secondary_exists = secondary_path.exists();
4018+
match (default_exists, secondary_exists) {
4019+
(true, false) => default_path,
4020+
(false, true) => secondary_path,
4021+
(false, false) => {
4022+
self.span_fatal(id_sp, fmt!("file not found for module `%s`", mod_name));
4023+
}
4024+
(true, true) => {
4025+
self.span_fatal(id_sp,
4026+
fmt!("file for module `%s` found at both %s and %s",
4027+
mod_name, default_path_str, secondary_path_str));
4028+
}
4029+
}
4030+
}
40104031
};
40114032

4012-
self.eval_src_mod_from_path(prefix,
4013-
file_path,
4033+
self.eval_src_mod_from_path(file_path,
40144034
outer_attrs.to_owned(),
40154035
id_sp)
40164036
}
40174037

40184038
fn eval_src_mod_from_path(&self,
4019-
prefix: Path,
40204039
path: Path,
40214040
outer_attrs: ~[ast::Attribute],
40224041
id_sp: span) -> (ast::item_, ~[ast::Attribute]) {
4023-
4024-
let full_path = if path.is_absolute {
4025-
path
4026-
} else {
4027-
prefix.push_many(path.components)
4028-
};
4029-
let full_path = full_path.normalize();
4042+
let full_path = path.normalize();
40304043

40314044
let maybe_i = do self.sess.included_mod_stack.iter().position |p| { *p == full_path };
40324045
match maybe_i {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod mod_file_disambig_aux; //~ ERROR file for module `mod_file_disambig_aux` found at both
12+
13+
fn main() {
14+
assert_eq!(mod_file_aux::bar(), 10);
15+
}

src/test/compile-fail/missingmod.rc renamed to src/test/compile-fail/mod_file_disambig_aux.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:error opening
12-
13-
mod doesnotexist;
11+
// xfail-test not a test. aux file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-test not a test. aux file

src/test/compile-fail/mod_file_not_exist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
mod not_a_real_file; //~ ERROR not_a_real_file.rs
11+
mod not_a_real_file; //~ ERROR file not found for module `not_a_real_file`
1212

1313
fn main() {
1414
assert_eq!(mod_file_aux::bar(), 10);

src/test/run-pass/mod_dir_implicit.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-pretty
12+
// xfail-fast
13+
14+
mod mod_dir_implicit_aux;
15+
16+
pub fn main() {
17+
assert_eq!(mod_dir_implicit_aux::foo(), 10);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn foo() -> int { 10 }

0 commit comments

Comments
 (0)