1
+ use crate :: base:: ModuleData ;
1
2
use rustc_ast:: ptr:: P ;
2
3
use rustc_ast:: { token, Attribute , Item } ;
3
4
use rustc_errors:: { struct_span_err, PResult } ;
@@ -9,12 +10,6 @@ use rustc_span::Span;
9
10
10
11
use std:: path:: { self , Path , PathBuf } ;
11
12
12
- #[ derive( Clone ) ]
13
- pub struct Directory {
14
- pub path : PathBuf ,
15
- pub ownership : DirectoryOwnership ,
16
- }
17
-
18
13
#[ derive( Copy , Clone ) ]
19
14
pub enum DirectoryOwnership {
20
15
Owned {
@@ -38,22 +33,30 @@ pub struct ModulePathSuccess {
38
33
pub ownership : DirectoryOwnership ,
39
34
}
40
35
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
+
41
44
crate fn parse_external_mod (
42
45
sess : & Session ,
43
46
id : Ident ,
44
47
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 ,
47
50
attrs : & mut Vec < Attribute > ,
48
- ) -> ( Vec < P < Item > > , Span , PathBuf , Directory ) {
51
+ ) -> ParsedExternalMod {
49
52
// We bail on the first error, but that error does not cause a fatal error... (1)
50
53
let result: PResult < ' _ , _ > = try {
51
54
// 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 ;
54
57
55
58
// 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 ) ?;
57
60
58
61
// Actually parse the external file as a module.
59
62
let mut parser = new_parser_from_file ( & sess. parse_sess , & mp. path , Some ( span) ) ;
@@ -65,9 +68,9 @@ crate fn parse_external_mod(
65
68
let ( items, inner_span, file_path) = result. map_err ( |mut err| err. emit ( ) ) . unwrap_or_default ( ) ;
66
69
67
70
// 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 ( ) ;
69
72
70
- ( items, inner_span, file_path, Directory { ownership , path } )
73
+ ParsedExternalMod { items, inner_span, file_path, dir_path , dir_ownership }
71
74
}
72
75
73
76
fn error_on_circular_module < ' a > (
@@ -92,27 +95,30 @@ crate fn push_directory(
92
95
sess : & Session ,
93
96
id : Ident ,
94
97
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 } ;
100
105
} else {
101
106
// We have to push on the current module name in the case of relative
102
107
// paths in order to ensure that any additional module paths from inline
103
108
// `mod x { ... }` come after the relative extension.
104
109
//
105
110
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
106
111
// 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 {
108
113
if let Some ( ident) = relative. take ( ) {
109
114
// Remove the relative offset.
110
- path . push ( & * ident. as_str ( ) ) ;
115
+ dir_path . push ( & * ident. as_str ( ) ) ;
111
116
}
112
117
}
113
- path . push ( & * id. as_str ( ) ) ;
118
+ dir_path . push ( & * id. as_str ( ) ) ;
114
119
}
115
- Directory { ownership, path }
120
+
121
+ ( dir_path, dir_ownership)
116
122
}
117
123
118
124
fn submod_path < ' a > (
0 commit comments