@@ -4,13 +4,15 @@ use rustc_ast::token;
4
4
use rustc_ast:: tokenstream:: TokenStream ;
5
5
use rustc_ast_pretty:: pprust;
6
6
use rustc_expand:: base:: { self , * } ;
7
+ use rustc_expand:: module:: DirectoryOwnership ;
7
8
use rustc_expand:: panictry;
8
9
use rustc_parse:: { self , new_sub_parser_from_file, parser:: Parser } ;
9
10
use rustc_session:: lint:: builtin:: INCOMPLETE_INCLUDE ;
10
11
use rustc_span:: symbol:: Symbol ;
11
12
use rustc_span:: { self , Pos , Span } ;
12
13
13
14
use smallvec:: SmallVec ;
15
+ use std:: rc:: Rc ;
14
16
15
17
use rustc_data_structures:: sync:: Lrc ;
16
18
@@ -101,7 +103,7 @@ pub fn expand_include<'cx>(
101
103
None => return DummyResult :: any ( sp) ,
102
104
} ;
103
105
// The file will be added to the code map by the parser
104
- let file = match cx. resolve_path ( file, sp) {
106
+ let mut file = match cx. resolve_path ( file, sp) {
105
107
Ok ( f) => f,
106
108
Err ( mut err) => {
107
109
err. emit ( ) ;
@@ -110,6 +112,15 @@ pub fn expand_include<'cx>(
110
112
} ;
111
113
let p = new_sub_parser_from_file ( cx. parse_sess ( ) , & file, None , sp) ;
112
114
115
+ // If in the included file we have e.g., `mod bar;`,
116
+ // then the path of `bar.rs` should be relative to the directory of `file`.
117
+ // See https://github.com/rust-lang/rust/pull/69838/files#r395217057 for a discussion.
118
+ // `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
119
+ file. pop ( ) ;
120
+ cx. current_expansion . directory_ownership = DirectoryOwnership :: Owned { relative : None } ;
121
+ let mod_path = cx. current_expansion . module . mod_path . clone ( ) ;
122
+ cx. current_expansion . module = Rc :: new ( ModuleData { mod_path, directory : file } ) ;
123
+
113
124
struct ExpandResult < ' a > {
114
125
p : Parser < ' a > ,
115
126
}
0 commit comments