@@ -4,13 +4,15 @@ use rustc_ast::token;
44use rustc_ast:: tokenstream:: TokenStream ;
55use rustc_ast_pretty:: pprust;
66use rustc_expand:: base:: { self , * } ;
7+ use rustc_expand:: module:: DirectoryOwnership ;
78use rustc_expand:: panictry;
89use rustc_parse:: { self , new_sub_parser_from_file, parser:: Parser } ;
910use rustc_session:: lint:: builtin:: INCOMPLETE_INCLUDE ;
1011use rustc_span:: symbol:: Symbol ;
1112use rustc_span:: { self , Pos , Span } ;
1213
1314use smallvec:: SmallVec ;
15+ use std:: rc:: Rc ;
1416
1517use rustc_data_structures:: sync:: Lrc ;
1618
@@ -101,7 +103,7 @@ pub fn expand_include<'cx>(
101103 None => return DummyResult :: any ( sp) ,
102104 } ;
103105 // 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) {
105107 Ok ( f) => f,
106108 Err ( mut err) => {
107109 err. emit ( ) ;
@@ -110,6 +112,15 @@ pub fn expand_include<'cx>(
110112 } ;
111113 let p = new_sub_parser_from_file ( cx. parse_sess ( ) , & file, None , sp) ;
112114
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+
113124 struct ExpandResult < ' a > {
114125 p : Parser < ' a > ,
115126 }
0 commit comments