1
+ //! The implementation of built-in macros which relate to the file system.
2
+
1
3
use std:: path:: { Path , PathBuf } ;
2
4
use std:: rc:: Rc ;
3
5
use std:: sync:: Arc ;
@@ -23,11 +25,7 @@ use crate::util::{
23
25
check_zero_tts, get_single_str_from_tts, get_single_str_spanned_from_tts, parse_expr,
24
26
} ;
25
27
26
- // These macros all relate to the file system; they either return
27
- // the column/row/filename of the expression, or they include
28
- // a given file into the current one.
29
-
30
- /// line!(): expands to the current line number
28
+ /// Expand `line!()` to the current line number.
31
29
pub ( crate ) fn expand_line (
32
30
cx : & mut ExtCtxt < ' _ > ,
33
31
sp : Span ,
@@ -42,7 +40,7 @@ pub(crate) fn expand_line(
42
40
ExpandResult :: Ready ( MacEager :: expr ( cx. expr_u32 ( topmost, loc. line as u32 ) ) )
43
41
}
44
42
45
- /* column!(): expands to the current column number */
43
+ /// Expand ` column!()` to the current column number.
46
44
pub ( crate ) fn expand_column (
47
45
cx : & mut ExtCtxt < ' _ > ,
48
46
sp : Span ,
@@ -57,9 +55,7 @@ pub(crate) fn expand_column(
57
55
ExpandResult :: Ready ( MacEager :: expr ( cx. expr_u32 ( topmost, loc. col . to_usize ( ) as u32 + 1 ) ) )
58
56
}
59
57
60
- /// file!(): expands to the current filename */
61
- /// The source_file (`loc.file`) contains a bunch more information we could spit
62
- /// out if we wanted.
58
+ /// Expand `file!()` to the current filename.
63
59
pub ( crate ) fn expand_file (
64
60
cx : & mut ExtCtxt < ' _ > ,
65
61
sp : Span ,
@@ -81,6 +77,7 @@ pub(crate) fn expand_file(
81
77
) ) )
82
78
}
83
79
80
+ /// Expand `stringify!($input)`.
84
81
pub ( crate ) fn expand_stringify (
85
82
cx : & mut ExtCtxt < ' _ > ,
86
83
sp : Span ,
@@ -91,6 +88,7 @@ pub(crate) fn expand_stringify(
91
88
ExpandResult :: Ready ( MacEager :: expr ( cx. expr_str ( sp, Symbol :: intern ( & s) ) ) )
92
89
}
93
90
91
+ /// Expand `module_path!()` to (a textual representation of) the current module path.
94
92
pub ( crate ) fn expand_mod (
95
93
cx : & mut ExtCtxt < ' _ > ,
96
94
sp : Span ,
@@ -104,9 +102,9 @@ pub(crate) fn expand_mod(
104
102
ExpandResult :: Ready ( MacEager :: expr ( cx. expr_str ( sp, Symbol :: intern ( & string) ) ) )
105
103
}
106
104
107
- /// include! : parse the given file as an expr
108
- /// This is generally a bad idea because it's going to behave
109
- /// unhygienically .
105
+ /// Expand ` include!($input)`.
106
+ ///
107
+ /// This works in item and expression position. Notably, it doesn't work in pattern position .
110
108
pub ( crate ) fn expand_include < ' cx > (
111
109
cx : & ' cx mut ExtCtxt < ' _ > ,
112
110
sp : Span ,
@@ -187,7 +185,9 @@ pub(crate) fn expand_include<'cx>(
187
185
ExpandResult :: Ready ( Box :: new ( ExpandInclude { p, node_id : cx. current_expansion . lint_node_id } ) )
188
186
}
189
187
190
- /// `include_str!`: read the given file, insert it as a literal string expr
188
+ /// Expand `include_str!($input)` to the content of the UTF-8-encoded file given by path `$input` as a string literal.
189
+ ///
190
+ /// This works in expression, pattern and statement position.
191
191
pub ( crate ) fn expand_include_str (
192
192
cx : & mut ExtCtxt < ' _ > ,
193
193
sp : Span ,
@@ -206,6 +206,8 @@ pub(crate) fn expand_include_str(
206
206
Ok ( ( bytes, bsp) ) => match std:: str:: from_utf8 ( & bytes) {
207
207
Ok ( src) => {
208
208
let interned_src = Symbol :: intern ( src) ;
209
+ // Even though we only expand to an expression, `include_str` is also works in patterns by
210
+ // virtue of us allowing certain expressions in patterns (see also `lower_expr_within_pat`).
209
211
MacEager :: expr ( cx. expr_str ( cx. with_def_site_ctxt ( bsp) , interned_src) )
210
212
}
211
213
Err ( utf8err) => {
@@ -218,6 +220,9 @@ pub(crate) fn expand_include_str(
218
220
} )
219
221
}
220
222
223
+ /// Expand `include_bytes!($input)` to the content of the file given by path `$input`.
224
+ ///
225
+ /// This works in expression, pattern and statement position.
221
226
pub ( crate ) fn expand_include_bytes (
222
227
cx : & mut ExtCtxt < ' _ > ,
223
228
sp : Span ,
@@ -234,6 +239,9 @@ pub(crate) fn expand_include_bytes(
234
239
} ;
235
240
ExpandResult :: Ready ( match load_binary_file ( cx, path. as_str ( ) . as_ref ( ) , sp, path_span) {
236
241
Ok ( ( bytes, _bsp) ) => {
242
+ // Even though we only expand to an expression, `include_bytes` is also works in patterns by
243
+ // virtue of us allowing certain expressions in patterns (see also `lower_expr_within_pat`).
244
+ //
237
245
// Don't care about getting the span for the raw bytes,
238
246
// because the console can't really show them anyway.
239
247
let expr = cx. expr ( sp, ast:: ExprKind :: IncludedBytes ( ByteSymbol :: intern ( & bytes) ) ) ;
0 commit comments