1+ use crate :: base:: ModuleData ;
12use rustc_ast:: ptr:: P ;
23use rustc_ast:: { token, Attribute , Item } ;
34use rustc_errors:: { struct_span_err, PResult } ;
@@ -9,12 +10,6 @@ use rustc_span::Span;
910
1011use std:: path:: { self , Path , PathBuf } ;
1112
12- #[ derive( Clone ) ]
13- pub struct Directory {
14- pub path : PathBuf ,
15- pub ownership : DirectoryOwnership ,
16- }
17-
1813#[ derive( Copy , Clone ) ]
1914pub enum DirectoryOwnership {
2015 Owned {
@@ -38,22 +33,30 @@ pub struct ModulePathSuccess {
3833 pub ownership : DirectoryOwnership ,
3934}
4035
36+ crate struct ParsedExternalMod {
37+ pub items : Vec < P < Item > > ,
38+ pub inner_span : Span ,
39+ pub file_path : PathBuf ,
40+ pub dir_path : PathBuf ,
41+ pub dir_ownership : DirectoryOwnership ,
42+ }
43+
4144crate fn parse_external_mod (
4245 sess : & Session ,
4346 id : Ident ,
4447 span : Span , // The span to blame on errors.
45- file_path_stack : & [ PathBuf ] ,
46- Directory { mut ownership , path } : Directory ,
48+ module : & ModuleData ,
49+ mut dir_ownership : DirectoryOwnership ,
4750 attrs : & mut Vec < Attribute > ,
48- ) -> ( Vec < P < Item > > , Span , PathBuf , Directory ) {
51+ ) -> ParsedExternalMod {
4952 // We bail on the first error, but that error does not cause a fatal error... (1)
5053 let result: PResult < ' _ , _ > = try {
5154 // Extract the file path and the new ownership.
52- let mp = submod_path ( sess, id, span, & attrs, ownership , & path ) ?;
53- ownership = mp. ownership ;
55+ let mp = submod_path ( sess, id, span, & attrs, dir_ownership , & module . dir_path ) ?;
56+ dir_ownership = mp. ownership ;
5457
5558 // Ensure file paths are acyclic.
56- error_on_circular_module ( & sess. parse_sess , span, & mp. path , file_path_stack) ?;
59+ error_on_circular_module ( & sess. parse_sess , span, & mp. path , & module . file_path_stack ) ?;
5760
5861 // Actually parse the external file as a module.
5962 let mut parser = new_parser_from_file ( & sess. parse_sess , & mp. path , Some ( span) ) ;
@@ -65,9 +68,9 @@ crate fn parse_external_mod(
6568 let ( items, inner_span, file_path) = result. map_err ( |mut err| err. emit ( ) ) . unwrap_or_default ( ) ;
6669
6770 // Extract the directory path for submodules of the module.
68- let path = file_path. parent ( ) . unwrap_or ( & file_path) . to_owned ( ) ;
71+ let dir_path = file_path. parent ( ) . unwrap_or ( & file_path) . to_owned ( ) ;
6972
70- ( items, inner_span, file_path, Directory { ownership , path } )
73+ ParsedExternalMod { items, inner_span, file_path, dir_path , dir_ownership }
7174}
7275
7376fn error_on_circular_module < ' a > (
@@ -92,27 +95,30 @@ crate fn push_directory(
9295 sess : & Session ,
9396 id : Ident ,
9497 attrs : & [ Attribute ] ,
95- Directory { mut ownership, mut path } : Directory ,
96- ) -> Directory {
97- if let Some ( filename) = sess. first_attr_value_str_by_name ( attrs, sym:: path) {
98- path. push ( & * filename. as_str ( ) ) ;
99- ownership = DirectoryOwnership :: Owned { relative : None } ;
98+ module : & ModuleData ,
99+ mut dir_ownership : DirectoryOwnership ,
100+ ) -> ( PathBuf , DirectoryOwnership ) {
101+ let mut dir_path = module. dir_path . clone ( ) ;
102+ if let Some ( file_path) = sess. first_attr_value_str_by_name ( attrs, sym:: path) {
103+ dir_path. push ( & * file_path. as_str ( ) ) ;
104+ dir_ownership = DirectoryOwnership :: Owned { relative : None } ;
100105 } else {
101106 // We have to push on the current module name in the case of relative
102107 // paths in order to ensure that any additional module paths from inline
103108 // `mod x { ... }` come after the relative extension.
104109 //
105110 // For example, a `mod z { ... }` inside `x/y.rs` should set the current
106111 // directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
107- if let DirectoryOwnership :: Owned { relative } = & mut ownership {
112+ if let DirectoryOwnership :: Owned { relative } = & mut dir_ownership {
108113 if let Some ( ident) = relative. take ( ) {
109114 // Remove the relative offset.
110- path . push ( & * ident. as_str ( ) ) ;
115+ dir_path . push ( & * ident. as_str ( ) ) ;
111116 }
112117 }
113- path . push ( & * id. as_str ( ) ) ;
118+ dir_path . push ( & * id. as_str ( ) ) ;
114119 }
115- Directory { ownership, path }
120+
121+ ( dir_path, dir_ownership)
116122}
117123
118124fn submod_path < ' a > (
0 commit comments