Skip to content

Commit 7df5868

Browse files
committed
tweak outline module parsing spans
1 parent ad0b078 commit 7df5868

15 files changed

+55
-49
lines changed

src/librustc_expand/expand.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1361,17 +1361,15 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13611361

13621362
let mut attrs = mem::take(&mut item.attrs); // We do this to please borrowck.
13631363
let ident = item.ident;
1364+
let span = item.span;
13641365

13651366
match item.kind {
13661367
ast::ItemKind::Mac(..) => {
13671368
item.attrs = attrs;
13681369
self.check_attributes(&item.attrs);
13691370
item.and_then(|item| match item.kind {
13701371
ItemKind::Mac(mac) => self
1371-
.collect(
1372-
AstFragmentKind::Items,
1373-
InvocationKind::Bang { mac, span: item.span },
1374-
)
1372+
.collect(AstFragmentKind::Items, InvocationKind::Bang { mac, span })
13751373
.make_items(),
13761374
_ => unreachable!(),
13771375
})
@@ -1389,7 +1387,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13891387
push_directory(ident, &item.attrs, dir)
13901388
} else {
13911389
// We have an outline `mod foo;` so we need to parse the file.
1392-
let (new_mod, dir) = parse_external_mod(sess, ident, dir, &mut attrs, pushed);
1390+
let (new_mod, dir) =
1391+
parse_external_mod(sess, ident, span, dir, &mut attrs, pushed);
13931392
*old_mod = new_mod;
13941393
item.attrs = attrs;
13951394
// File can have inline attributes, e.g., `#![cfg(...)]` & co. => Reconfigure.

src/librustc_expand/module.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,26 @@ pub struct ModulePathSuccess {
4141
crate fn parse_external_mod(
4242
sess: &ParseSess,
4343
id: ast::Ident,
44+
span: Span, // The span to blame on errors.
4445
Directory { mut ownership, path }: Directory,
4546
attrs: &mut Vec<Attribute>,
4647
pop_mod_stack: &mut bool,
4748
) -> (Mod, Directory) {
4849
// We bail on the first error, but that error does not cause a fatal error... (1)
4950
let result: PResult<'_, _> = try {
5051
// Extract the file path and the new ownership.
51-
let mp = submod_path(sess, id, &attrs, ownership, &path)?;
52+
let mp = submod_path(sess, id, span, &attrs, ownership, &path)?;
5253
ownership = mp.ownership;
5354

5455
// Ensure file paths are acyclic.
5556
let mut included_mod_stack = sess.included_mod_stack.borrow_mut();
56-
error_on_circular_module(sess, id.span, &mp.path, &included_mod_stack)?;
57+
error_on_circular_module(sess, span, &mp.path, &included_mod_stack)?;
5758
included_mod_stack.push(mp.path.clone());
5859
*pop_mod_stack = true; // We have pushed, so notify caller.
5960
drop(included_mod_stack);
6061

6162
// Actually parse the external file as amodule.
62-
let mut p0 = new_sub_parser_from_file(sess, &mp.path, Some(id.to_string()), id.span);
63+
let mut p0 = new_sub_parser_from_file(sess, &mp.path, Some(id.to_string()), span);
6364
let mut module = p0.parse_mod(&token::Eof)?;
6465
module.0.inline = false;
6566
module
@@ -126,6 +127,7 @@ crate fn push_directory(
126127
fn submod_path<'a>(
127128
sess: &'a ParseSess,
128129
id: ast::Ident,
130+
span: Span,
129131
attrs: &[Attribute],
130132
ownership: DirectoryOwnership,
131133
dir_path: &Path,
@@ -150,54 +152,53 @@ fn submod_path<'a>(
150152
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
151153
};
152154
let ModulePath { path_exists, name, result } =
153-
default_submod_path(sess, id, relative, dir_path);
155+
default_submod_path(sess, id, span, relative, dir_path);
154156
match ownership {
155157
DirectoryOwnership::Owned { .. } => Ok(result?),
156158
DirectoryOwnership::UnownedViaBlock => {
157159
let _ = result.map_err(|mut err| err.cancel());
158-
error_decl_mod_in_block(sess, id.span, path_exists, &name)
160+
error_decl_mod_in_block(sess, span, path_exists, &name)
159161
}
160162
DirectoryOwnership::UnownedViaMod => {
161163
let _ = result.map_err(|mut err| err.cancel());
162-
error_cannot_declare_mod_here(sess, id.span, path_exists, &name)
164+
error_cannot_declare_mod_here(sess, span, path_exists, &name)
163165
}
164166
}
165167
}
166168

167169
fn error_decl_mod_in_block<'a, T>(
168170
sess: &'a ParseSess,
169-
id_sp: Span,
171+
span: Span,
170172
path_exists: bool,
171173
name: &str,
172174
) -> PResult<'a, T> {
173175
let msg = "Cannot declare a non-inline module inside a block unless it has a path attribute";
174-
let mut err = sess.span_diagnostic.struct_span_err(id_sp, msg);
176+
let mut err = sess.span_diagnostic.struct_span_err(span, msg);
175177
if path_exists {
176178
let msg = format!("Maybe `use` the module `{}` instead of redeclaring it", name);
177-
err.span_note(id_sp, &msg);
179+
err.span_note(span, &msg);
178180
}
179181
Err(err)
180182
}
181183

182184
fn error_cannot_declare_mod_here<'a, T>(
183185
sess: &'a ParseSess,
184-
id_sp: Span,
186+
span: Span,
185187
path_exists: bool,
186188
name: &str,
187189
) -> PResult<'a, T> {
188190
let mut err =
189-
sess.span_diagnostic.struct_span_err(id_sp, "cannot declare a new module at this location");
190-
if !id_sp.is_dummy() {
191-
if let FileName::Real(src_path) = sess.source_map().span_to_filename(id_sp) {
191+
sess.span_diagnostic.struct_span_err(span, "cannot declare a new module at this location");
192+
if !span.is_dummy() {
193+
if let FileName::Real(src_path) = sess.source_map().span_to_filename(span) {
192194
if let Some(stem) = src_path.file_stem() {
193195
let mut dest_path = src_path.clone();
194196
dest_path.set_file_name(stem);
195197
dest_path.push("mod.rs");
196198
err.span_note(
197-
id_sp,
199+
span,
198200
&format!(
199-
"maybe move this module `{}` to its own \
200-
directory via `{}`",
201+
"maybe move this module `{}` to its own directory via `{}`",
201202
src_path.display(),
202203
dest_path.display()
203204
),
@@ -207,7 +208,7 @@ fn error_cannot_declare_mod_here<'a, T>(
207208
}
208209
if path_exists {
209210
err.span_note(
210-
id_sp,
211+
span,
211212
&format!("... or maybe `use` the module `{}` instead of possibly redeclaring it", name),
212213
);
213214
}
@@ -237,6 +238,7 @@ pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option<Pat
237238
pub fn default_submod_path<'a>(
238239
sess: &'a ParseSess,
239240
id: ast::Ident,
241+
span: Span,
240242
relative: Option<ast::Ident>,
241243
dir_path: &Path,
242244
) -> ModulePath<'a> {
@@ -273,7 +275,7 @@ pub fn default_submod_path<'a>(
273275
(false, false) => {
274276
let mut err = struct_span_err!(
275277
sess.span_diagnostic,
276-
id.span,
278+
span,
277279
E0583,
278280
"file not found for module `{}`",
279281
mod_name,
@@ -289,7 +291,7 @@ pub fn default_submod_path<'a>(
289291
(true, true) => {
290292
let mut err = struct_span_err!(
291293
sess.span_diagnostic,
292-
id.span,
294+
span,
293295
E0584,
294296
"file for module `{}` found at both {} and {}",
295297
mod_name,

src/librustc_parse/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a> Parser<'a> {
3838
}
3939

4040
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
41-
pub(super) fn parse_item_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
41+
fn parse_item_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
4242
let id = self.parse_ident()?;
4343
let (module, mut inner_attrs) = if self.eat(&token::Semi) {
4444
Default::default()

src/test/ui/directory_ownership/macro-expanded-mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
macro_rules! mod_decl {
44
($i:ident) => {
5-
mod $i;
5+
mod $i; //~ ERROR Cannot declare a non-inline module inside a block
66
};
77
}
88

@@ -11,5 +11,5 @@ mod macro_expanded_mod_helper {
1111
}
1212

1313
fn main() {
14-
mod_decl!(foo); //~ ERROR Cannot declare a non-inline module inside a block
14+
mod_decl!(foo);
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
error: Cannot declare a non-inline module inside a block unless it has a path attribute
2-
--> $DIR/macro-expanded-mod.rs:14:15
2+
--> $DIR/macro-expanded-mod.rs:5:9
33
|
4+
LL | mod $i;
5+
| ^^^^^^^
6+
...
47
LL | mod_decl!(foo);
5-
| ^^^
8+
| --------------- in this macro invocation
9+
|
10+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
611

712
error: aborting due to previous error
813

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Cannot declare a non-inline module inside a block unless it has a path attribute
2-
--> $DIR/non-inline-mod-restriction.rs:4:9
2+
--> $DIR/non-inline-mod-restriction.rs:4:5
33
|
44
LL | mod foo;
5-
| ^^^
5+
| ^^^^^^^^
66

77
error: aborting due to previous error
88

src/test/ui/error-codes/E0583.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0583]: file not found for module `module_that_doesnt_exist`
2-
--> $DIR/E0583.rs:1:5
2+
--> $DIR/E0583.rs:1:1
33
|
44
LL | mod module_that_doesnt_exist;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: name the file either module_that_doesnt_exist.rs or module_that_doesnt_exist/mod.rs inside the directory "$DIR"
88

src/test/ui/invalid-module-declaration/invalid-module-declaration.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0583]: file not found for module `baz`
2-
--> $DIR/auxiliary/foo/bar.rs:1:9
2+
--> $DIR/auxiliary/foo/bar.rs:1:1
33
|
44
LL | pub mod baz;
5-
| ^^^
5+
| ^^^^^^^^^^^^
66
|
77
= help: name the file either bar/baz.rs or bar/baz/mod.rs inside the directory "$DIR/auxiliary/foo"
88

src/test/ui/missing_non_modrs_mod/missing_non_modrs_mod.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0583]: file not found for module `missing`
2-
--> $DIR/foo.rs:4:5
2+
--> $DIR/foo.rs:4:1
33
|
44
LL | mod missing;
5-
| ^^^^^^^
5+
| ^^^^^^^^^^^^
66
|
77
= help: name the file either foo/missing.rs or foo/missing/mod.rs inside the directory "$DIR"
88

src/test/ui/missing_non_modrs_mod/missing_non_modrs_mod_inline.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0583]: file not found for module `missing`
2-
--> $DIR/foo_inline.rs:4:9
2+
--> $DIR/foo_inline.rs:4:5
33
|
44
LL | mod missing;
5-
| ^^^^^^^
5+
| ^^^^^^^^^^^^
66
|
77
= help: name the file either missing.rs or missing/mod.rs inside the directory "$DIR/foo_inline/inline"
88

src/test/ui/mod/mod_file_disambig.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0584]: file for module `mod_file_disambig_aux` found at both mod_file_disambig_aux.rs and mod_file_disambig_aux/mod.rs
2-
--> $DIR/mod_file_disambig.rs:1:5
2+
--> $DIR/mod_file_disambig.rs:1:1
33
|
44
LL | mod mod_file_disambig_aux;
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: delete or rename one of them to remove the ambiguity
88

src/test/ui/parser/circular_modules_main.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: circular modules: $DIR/circular_modules_hello.rs -> $DIR/circular_modules_main.rs -> $DIR/circular_modules_hello.rs
2-
--> $DIR/circular_modules_main.rs:2:5
2+
--> $DIR/circular_modules_main.rs:2:1
33
|
44
LL | mod circular_modules_hello;
5-
| ^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
error[E0425]: cannot find function `say_hello` in module `circular_modules_hello`
88
--> $DIR/circular_modules_main.rs:9:29

src/test/ui/parser/issue-5806.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: couldn't read $DIR/../parser: $ACCESS_DENIED_MSG (os error $ACCESS_DENIED_CODE)
2-
--> $DIR/issue-5806.rs:5:5
2+
--> $DIR/issue-5806.rs:5:1
33
|
44
LL | mod foo;
5-
| ^^^
5+
| ^^^^^^^^
66

77
error: aborting due to previous error
88

src/test/ui/parser/mod_file_not_exist.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0583]: file not found for module `not_a_real_file`
2-
--> $DIR/mod_file_not_exist.rs:3:5
2+
--> $DIR/mod_file_not_exist.rs:3:1
33
|
44
LL | mod not_a_real_file;
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: name the file either not_a_real_file.rs or not_a_real_file/mod.rs inside the directory "$DIR"
88

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: couldn't read $DIR/not_a_real_file.rs: $FILE_NOT_FOUND_MSG (os error 2)
2-
--> $DIR/mod_file_with_path_attr.rs:4:5
2+
--> $DIR/mod_file_with_path_attr.rs:4:1
33
|
44
LL | mod m;
5-
| ^
5+
| ^^^^^^
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)