@@ -6,13 +6,13 @@ use crate::print::pprust;
6
6
use crate :: ptr:: P ;
7
7
use crate :: symbol:: Symbol ;
8
8
use crate :: tokenstream;
9
+ use crate :: util:: path;
9
10
10
11
use smallvec:: SmallVec ;
11
- use syntax_pos:: { self , Pos , Span , FileName } ;
12
+ use syntax_pos:: { self , Pos , Span } ;
12
13
13
14
use std:: fs;
14
15
use std:: io:: ErrorKind ;
15
- use std:: path:: PathBuf ;
16
16
use rustc_data_structures:: sync:: Lrc ;
17
17
18
18
// These macros all relate to the file system; they either return
@@ -78,9 +78,9 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: &[tokenstrea
78
78
None => return DummyResult :: any ( sp) ,
79
79
} ;
80
80
// The file will be added to the code map by the parser
81
- let path = res_rel_file ( cx , sp, file ) ;
81
+ let file = path :: resolve ( file , sp, cx . source_map ( ) ) ;
82
82
let directory_ownership = DirectoryOwnership :: Owned { relative : None } ;
83
- let p = parse:: new_sub_parser_from_file ( cx. parse_sess ( ) , & path , directory_ownership, None , sp) ;
83
+ let p = parse:: new_sub_parser_from_file ( cx. parse_sess ( ) , & file , directory_ownership, None , sp) ;
84
84
85
85
struct ExpandResult < ' a > {
86
86
p : parse:: parser:: Parser < ' a > ,
@@ -115,7 +115,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To
115
115
Some ( f) => f,
116
116
None => return DummyResult :: expr ( sp)
117
117
} ;
118
- let file = res_rel_file ( cx , sp, file ) ;
118
+ let file = path :: resolve ( file , sp, cx . source_map ( ) ) ;
119
119
match fs:: read_to_string ( & file) {
120
120
Ok ( src) => {
121
121
let interned_src = Symbol :: intern ( & src) ;
@@ -143,7 +143,7 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::
143
143
Some ( f) => f,
144
144
None => return DummyResult :: expr ( sp)
145
145
} ;
146
- let file = res_rel_file ( cx , sp, file ) ;
146
+ let file = path :: resolve ( file , sp, cx . source_map ( ) ) ;
147
147
match fs:: read ( & file) {
148
148
Ok ( bytes) => {
149
149
// Add the contents to the source map if it contains UTF-8.
@@ -164,24 +164,3 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::
164
164
}
165
165
}
166
166
}
167
-
168
- // resolve a file-system path to an absolute file-system path (if it
169
- // isn't already)
170
- fn res_rel_file ( cx : & mut ExtCtxt < ' _ > , sp : syntax_pos:: Span , arg : String ) -> PathBuf {
171
- let arg = PathBuf :: from ( arg) ;
172
- // Relative paths are resolved relative to the file in which they are found
173
- // after macro expansion (that is, they are unhygienic).
174
- if !arg. is_absolute ( ) {
175
- let callsite = sp. source_callsite ( ) ;
176
- let mut path = match cx. source_map ( ) . span_to_unmapped_path ( callsite) {
177
- FileName :: Real ( path) => path,
178
- FileName :: DocTest ( path, _) => path,
179
- other => panic ! ( "cannot resolve relative path in non-file source `{}`" , other) ,
180
- } ;
181
- path. pop ( ) ;
182
- path. push ( arg) ;
183
- path
184
- } else {
185
- arg
186
- }
187
- }
0 commit comments