@@ -264,8 +264,7 @@ pub struct Parser<'a> {
264
264
/// extra detail when the same error is seen twice
265
265
pub obsolete_set : HashSet < ObsoleteSyntax > ,
266
266
/// Used to determine the path to externally loaded source files
267
- pub filename : Option < String > ,
268
- pub mod_path_stack : Vec < InternedString > ,
267
+ pub directory : PathBuf ,
269
268
/// Stack of open delimiters and their spans. Used for error message.
270
269
pub open_braces : Vec < ( token:: DelimToken , Span ) > ,
271
270
/// Flag if this parser "owns" the directory that it is currently parsing
@@ -346,9 +345,11 @@ impl<'a> Parser<'a> {
346
345
{
347
346
let tok0 = rdr. real_token ( ) ;
348
347
let span = tok0. sp ;
349
- let filename = if span != syntax_pos:: DUMMY_SP {
350
- Some ( sess. codemap ( ) . span_to_filename ( span) )
351
- } else { None } ;
348
+ let mut directory = match span {
349
+ syntax_pos:: DUMMY_SP => PathBuf :: new ( ) ,
350
+ _ => PathBuf :: from ( sess. codemap ( ) . span_to_filename ( span) ) ,
351
+ } ;
352
+ directory. pop ( ) ;
352
353
let placeholder = TokenAndSpan {
353
354
tok : token:: Underscore ,
354
355
sp : span,
@@ -377,8 +378,7 @@ impl<'a> Parser<'a> {
377
378
quote_depth : 0 ,
378
379
parsing_token_tree : false ,
379
380
obsolete_set : HashSet :: new ( ) ,
380
- mod_path_stack : Vec :: new ( ) ,
381
- filename : filename,
381
+ directory : directory,
382
382
open_braces : Vec :: new ( ) ,
383
383
owns_directory : true ,
384
384
root_module_name : None ,
@@ -5306,27 +5306,24 @@ impl<'a> Parser<'a> {
5306
5306
let ( m, attrs) = self . eval_src_mod ( id, & outer_attrs, id_span) ?;
5307
5307
Ok ( ( id, m, Some ( attrs) ) )
5308
5308
} else {
5309
- self . push_mod_path ( id, & outer_attrs) ;
5309
+ let directory = self . directory . clone ( ) ;
5310
+ self . push_directory ( id, & outer_attrs) ;
5310
5311
self . expect ( & token:: OpenDelim ( token:: Brace ) ) ?;
5311
5312
let mod_inner_lo = self . span . lo ;
5312
5313
let attrs = self . parse_inner_attributes ( ) ?;
5313
5314
let m = self . parse_mod_items ( & token:: CloseDelim ( token:: Brace ) , mod_inner_lo) ?;
5314
- self . pop_mod_path ( ) ;
5315
+ self . directory = directory ;
5315
5316
Ok ( ( id, ItemKind :: Mod ( m) , Some ( attrs) ) )
5316
5317
}
5317
5318
}
5318
5319
5319
- fn push_mod_path ( & mut self , id : Ident , attrs : & [ Attribute ] ) {
5320
+ fn push_directory ( & mut self , id : Ident , attrs : & [ Attribute ] ) {
5320
5321
let default_path = self . id_to_interned_str ( id) ;
5321
5322
let file_path = match :: attr:: first_attr_value_str_by_name ( attrs, "path" ) {
5322
5323
Some ( d) => d,
5323
5324
None => default_path,
5324
5325
} ;
5325
- self . mod_path_stack . push ( file_path)
5326
- }
5327
-
5328
- fn pop_mod_path ( & mut self ) {
5329
- self . mod_path_stack . pop ( ) . unwrap ( ) ;
5326
+ self . directory . push ( & * file_path)
5330
5327
}
5331
5328
5332
5329
pub fn submod_path_from_attr ( attrs : & [ ast:: Attribute ] , dir_path : & Path ) -> Option < PathBuf > {
@@ -5374,18 +5371,11 @@ impl<'a> Parser<'a> {
5374
5371
id : ast:: Ident ,
5375
5372
outer_attrs : & [ ast:: Attribute ] ,
5376
5373
id_sp : Span ) -> PResult < ' a , ModulePathSuccess > {
5377
- let mut prefix = PathBuf :: from ( self . filename . as_ref ( ) . unwrap ( ) ) ;
5378
- prefix. pop ( ) ;
5379
- let mut dir_path = prefix;
5380
- for part in & self . mod_path_stack {
5381
- dir_path. push ( & * * part) ;
5382
- }
5383
-
5384
- if let Some ( p) = Parser :: submod_path_from_attr ( outer_attrs, & dir_path) {
5374
+ if let Some ( p) = Parser :: submod_path_from_attr ( outer_attrs, & self . directory ) {
5385
5375
return Ok ( ModulePathSuccess { path : p, owns_directory : true } ) ;
5386
5376
}
5387
5377
5388
- let paths = Parser :: default_submod_path ( id, & dir_path , self . sess . codemap ( ) ) ;
5378
+ let paths = Parser :: default_submod_path ( id, & self . directory , self . sess . codemap ( ) ) ;
5389
5379
5390
5380
if self . restrictions . contains ( Restrictions :: NO_NONINLINE_MOD ) {
5391
5381
let msg =
@@ -5400,8 +5390,8 @@ impl<'a> Parser<'a> {
5400
5390
} else if !self . owns_directory {
5401
5391
let mut err = self . diagnostic ( ) . struct_span_err ( id_sp,
5402
5392
"cannot declare a new module at this location" ) ;
5403
- let this_module = match self . mod_path_stack . last ( ) {
5404
- Some ( name ) => name . to_string ( ) ,
5393
+ let this_module = match self . directory . file_name ( ) {
5394
+ Some ( file_name ) => file_name . to_str ( ) . unwrap ( ) . to_owned ( ) ,
5405
5395
None => self . root_module_name . as_ref ( ) . unwrap ( ) . clone ( ) ,
5406
5396
} ;
5407
5397
err. span_note ( id_sp,
0 commit comments