Skip to content

Commit 75ad1b8

Browse files
committed
de-fatalize outline module parsing
1 parent aff35c4 commit 75ad1b8

File tree

8 files changed

+52
-27
lines changed

8 files changed

+52
-27
lines changed

src/librustc_ast/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ impl FnRetTy {
21532153
/// Module declaration.
21542154
///
21552155
/// E.g., `mod foo;` or `mod foo { .. }`.
2156-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2156+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Default)]
21572157
pub struct Mod {
21582158
/// A span from the first token past `{` to the last token until `}`.
21592159
/// For `mod foo;`, the inner span ranges from the first token

src/librustc_parse/parser/module.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::attr;
88
use rustc_ast::token::{self, TokenKind};
99
use rustc_errors::{struct_span_err, PResult};
1010
use rustc_session::parse::ParseSess;
11-
use rustc_span::source_map::{FileName, Span, DUMMY_SP};
11+
use rustc_span::source_map::{FileName, Span};
1212
use rustc_span::symbol::sym;
1313

1414
use std::path::{self, Path, PathBuf};
@@ -24,7 +24,7 @@ pub struct ModulePath<'a> {
2424
// Public for rustfmt usage.
2525
pub struct ModulePathSuccess {
2626
pub path: PathBuf,
27-
pub directory_ownership: DirectoryOwnership,
27+
pub ownership: DirectoryOwnership,
2828
}
2929

3030
impl<'a> Parser<'a> {
@@ -45,16 +45,13 @@ impl<'a> Parser<'a> {
4545
let (module, mut inner_attrs) = if self.eat(&token::Semi) {
4646
if in_cfg && self.recurse_into_file_modules {
4747
// This mod is in an external file. Let's go get it!
48-
let ModulePathSuccess { path, directory_ownership } = submod_path(
49-
self.sess,
50-
id,
51-
&attrs,
52-
self.directory.ownership,
53-
&self.directory.path,
54-
)?;
55-
eval_src_mod(self.sess, self.cfg_mods, path, directory_ownership, id)?
48+
let dir = &self.directory;
49+
submod_path(self.sess, id, &attrs, dir.ownership, &dir.path)
50+
.and_then(|r| eval_src_mod(self.sess, self.cfg_mods, r.path, r.ownership, id))
51+
.map_err(|mut err| err.emit())
52+
.unwrap_or_default()
5653
} else {
57-
(ast::Mod { inner: DUMMY_SP, items: Vec::new(), inline: false }, Vec::new())
54+
Default::default()
5855
}
5956
} else {
6057
let old_directory = self.directory.clone();
@@ -162,12 +159,12 @@ pub fn push_directory(
162159
fn submod_path<'a>(
163160
sess: &'a ParseSess,
164161
id: ast::Ident,
165-
outer_attrs: &[Attribute],
166-
directory_ownership: DirectoryOwnership,
162+
attrs: &[Attribute],
163+
ownership: DirectoryOwnership,
167164
dir_path: &Path,
168165
) -> PResult<'a, ModulePathSuccess> {
169-
if let Some(path) = submod_path_from_attr(outer_attrs, dir_path) {
170-
let directory_ownership = match path.file_name().and_then(|s| s.to_str()) {
166+
if let Some(path) = submod_path_from_attr(attrs, dir_path) {
167+
let ownership = match path.file_name().and_then(|s| s.to_str()) {
171168
// All `#[path]` files are treated as though they are a `mod.rs` file.
172169
// This means that `mod foo;` declarations inside `#[path]`-included
173170
// files are siblings,
@@ -178,16 +175,16 @@ fn submod_path<'a>(
178175
Some(_) => DirectoryOwnership::Owned { relative: None },
179176
_ => DirectoryOwnership::UnownedViaMod,
180177
};
181-
return Ok(ModulePathSuccess { directory_ownership, path });
178+
return Ok(ModulePathSuccess { ownership, path });
182179
}
183180

184-
let relative = match directory_ownership {
181+
let relative = match ownership {
185182
DirectoryOwnership::Owned { relative } => relative,
186183
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
187184
};
188185
let ModulePath { path_exists, name, result } =
189186
default_submod_path(sess, id, relative, dir_path);
190-
match directory_ownership {
187+
match ownership {
191188
DirectoryOwnership::Owned { .. } => Ok(result?),
192189
DirectoryOwnership::UnownedViaBlock => {
193190
let _ = result.map_err(|mut err| err.cancel());
@@ -300,11 +297,11 @@ pub fn default_submod_path<'a>(
300297
let result = match (default_exists, secondary_exists) {
301298
(true, false) => Ok(ModulePathSuccess {
302299
path: default_path,
303-
directory_ownership: DirectoryOwnership::Owned { relative: Some(id) },
300+
ownership: DirectoryOwnership::Owned { relative: Some(id) },
304301
}),
305302
(false, true) => Ok(ModulePathSuccess {
306303
path: secondary_path,
307-
directory_ownership: DirectoryOwnership::Owned { relative: None },
304+
ownership: DirectoryOwnership::Owned { relative: None },
308305
}),
309306
(false, false) => {
310307
let mut err = struct_span_err!(

src/test/ui/mod/mod_file_disambig.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ mod mod_file_disambig_aux; //~ ERROR file for module `mod_file_disambig_aux` fou
22

33
fn main() {
44
assert_eq!(mod_file_aux::bar(), 10);
5+
//~^ ERROR failed to resolve: use of undeclared type or module `mod_file_aux`
56
}

src/test/ui/mod/mod_file_disambig.stderr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ LL | mod mod_file_disambig_aux;
66
|
77
= help: delete or rename one of them to remove the ambiguity
88

9-
error: aborting due to previous error
9+
error[E0433]: failed to resolve: use of undeclared type or module `mod_file_aux`
10+
--> $DIR/mod_file_disambig.rs:4:16
11+
|
12+
LL | assert_eq!(mod_file_aux::bar(), 10);
13+
| ^^^^^^^^^^^^ use of undeclared type or module `mod_file_aux`
14+
15+
error: aborting due to 2 previous errors
1016

11-
For more information about this error, try `rustc --explain E0584`.
17+
Some errors have detailed explanations: E0433, E0584.
18+
For more information about an error, try `rustc --explain E0433`.

src/test/ui/parser/circular_modules_main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ pub fn hi_str() -> String {
66
}
77

88
fn main() {
9-
circular_modules_hello::say_hello();
9+
circular_modules_hello::say_hello(); //~ ERROR cannot find function `say_hello` in module
1010
}

src/test/ui/parser/circular_modules_main.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,17 @@ error: circular modules: $DIR/circular_modules_hello.rs -> $DIR/circular_modules
44
LL | mod circular_modules_hello;
55
| ^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error[E0425]: cannot find function `say_hello` in module `circular_modules_hello`
8+
--> $DIR/circular_modules_main.rs:9:29
9+
|
10+
LL | circular_modules_hello::say_hello();
11+
| ^^^^^^^^^ not found in `circular_modules_hello`
12+
|
13+
help: possible candidate is found in another module, you can import it into scope
14+
|
15+
LL | use circular_modules_hello::say_hello;
16+
|
17+
18+
error: aborting due to 2 previous errors
819

20+
For more information about this error, try `rustc --explain E0425`.

src/test/ui/parser/mod_file_not_exist.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ mod not_a_real_file; //~ ERROR file not found for module `not_a_real_file`
55

66
fn main() {
77
assert_eq!(mod_file_aux::bar(), 10);
8+
//~^ ERROR failed to resolve: use of undeclared type or module `mod_file_aux`
89
}

src/test/ui/parser/mod_file_not_exist.stderr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ LL | mod not_a_real_file;
66
|
77
= help: name the file either not_a_real_file.rs or not_a_real_file/mod.rs inside the directory "$DIR"
88

9-
error: aborting due to previous error
9+
error[E0433]: failed to resolve: use of undeclared type or module `mod_file_aux`
10+
--> $DIR/mod_file_not_exist.rs:7:16
11+
|
12+
LL | assert_eq!(mod_file_aux::bar(), 10);
13+
| ^^^^^^^^^^^^ use of undeclared type or module `mod_file_aux`
14+
15+
error: aborting due to 2 previous errors
1016

11-
For more information about this error, try `rustc --explain E0583`.
17+
Some errors have detailed explanations: E0433, E0583.
18+
For more information about an error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)