@@ -41,25 +41,26 @@ pub struct ModulePathSuccess {
41
41
crate fn parse_external_mod (
42
42
sess : & ParseSess ,
43
43
id : ast:: Ident ,
44
+ span : Span , // The span to blame on errors.
44
45
Directory { mut ownership, path } : Directory ,
45
46
attrs : & mut Vec < Attribute > ,
46
47
pop_mod_stack : & mut bool ,
47
48
) -> ( Mod , Directory ) {
48
49
// We bail on the first error, but that error does not cause a fatal error... (1)
49
50
let result: PResult < ' _ , _ > = try {
50
51
// 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) ?;
52
53
ownership = mp. ownership ;
53
54
54
55
// Ensure file paths are acyclic.
55
56
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) ?;
57
58
included_mod_stack. push ( mp. path . clone ( ) ) ;
58
59
* pop_mod_stack = true ; // We have pushed, so notify caller.
59
60
drop ( included_mod_stack) ;
60
61
61
62
// 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) ;
63
64
let mut module = p0. parse_mod ( & token:: Eof ) ?;
64
65
module. 0 . inline = false ;
65
66
module
@@ -126,6 +127,7 @@ crate fn push_directory(
126
127
fn submod_path < ' a > (
127
128
sess : & ' a ParseSess ,
128
129
id : ast:: Ident ,
130
+ span : Span ,
129
131
attrs : & [ Attribute ] ,
130
132
ownership : DirectoryOwnership ,
131
133
dir_path : & Path ,
@@ -150,54 +152,53 @@ fn submod_path<'a>(
150
152
DirectoryOwnership :: UnownedViaBlock | DirectoryOwnership :: UnownedViaMod => None ,
151
153
} ;
152
154
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) ;
154
156
match ownership {
155
157
DirectoryOwnership :: Owned { .. } => Ok ( result?) ,
156
158
DirectoryOwnership :: UnownedViaBlock => {
157
159
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)
159
161
}
160
162
DirectoryOwnership :: UnownedViaMod => {
161
163
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)
163
165
}
164
166
}
165
167
}
166
168
167
169
fn error_decl_mod_in_block < ' a , T > (
168
170
sess : & ' a ParseSess ,
169
- id_sp : Span ,
171
+ span : Span ,
170
172
path_exists : bool ,
171
173
name : & str ,
172
174
) -> PResult < ' a , T > {
173
175
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) ;
175
177
if path_exists {
176
178
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) ;
178
180
}
179
181
Err ( err)
180
182
}
181
183
182
184
fn error_cannot_declare_mod_here < ' a , T > (
183
185
sess : & ' a ParseSess ,
184
- id_sp : Span ,
186
+ span : Span ,
185
187
path_exists : bool ,
186
188
name : & str ,
187
189
) -> PResult < ' a , T > {
188
190
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 ) {
192
194
if let Some ( stem) = src_path. file_stem ( ) {
193
195
let mut dest_path = src_path. clone ( ) ;
194
196
dest_path. set_file_name ( stem) ;
195
197
dest_path. push ( "mod.rs" ) ;
196
198
err. span_note (
197
- id_sp ,
199
+ span ,
198
200
& format ! (
199
- "maybe move this module `{}` to its own \
200
- directory via `{}`",
201
+ "maybe move this module `{}` to its own directory via `{}`" ,
201
202
src_path. display( ) ,
202
203
dest_path. display( )
203
204
) ,
@@ -207,7 +208,7 @@ fn error_cannot_declare_mod_here<'a, T>(
207
208
}
208
209
if path_exists {
209
210
err. span_note (
210
- id_sp ,
211
+ span ,
211
212
& format ! ( "... or maybe `use` the module `{}` instead of possibly redeclaring it" , name) ,
212
213
) ;
213
214
}
@@ -237,6 +238,7 @@ pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option<Pat
237
238
pub fn default_submod_path < ' a > (
238
239
sess : & ' a ParseSess ,
239
240
id : ast:: Ident ,
241
+ span : Span ,
240
242
relative : Option < ast:: Ident > ,
241
243
dir_path : & Path ,
242
244
) -> ModulePath < ' a > {
@@ -273,7 +275,7 @@ pub fn default_submod_path<'a>(
273
275
( false , false ) => {
274
276
let mut err = struct_span_err ! (
275
277
sess. span_diagnostic,
276
- id . span,
278
+ span,
277
279
E0583 ,
278
280
"file not found for module `{}`" ,
279
281
mod_name,
@@ -289,7 +291,7 @@ pub fn default_submod_path<'a>(
289
291
( true , true ) => {
290
292
let mut err = struct_span_err ! (
291
293
sess. span_diagnostic,
292
- id . span,
294
+ span,
293
295
E0584 ,
294
296
"file for module `{}` found at both {} and {}" ,
295
297
mod_name,
0 commit comments