Skip to content

Commit 2d0cc75

Browse files
committed
Improve docs of certain built-in macro expanders
1 parent 2371802 commit 2d0cc75

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! The implementation of built-in macros which relate to the file system.
2+
13
use std::path::{Path, PathBuf};
24
use std::rc::Rc;
35
use std::sync::Arc;
@@ -23,11 +25,7 @@ use crate::util::{
2325
check_zero_tts, get_single_str_from_tts, get_single_str_spanned_from_tts, parse_expr,
2426
};
2527

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.
3129
pub(crate) fn expand_line(
3230
cx: &mut ExtCtxt<'_>,
3331
sp: Span,
@@ -42,7 +40,7 @@ pub(crate) fn expand_line(
4240
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.line as u32)))
4341
}
4442

45-
/* column!(): expands to the current column number */
43+
/// Expand `column!()` to the current column number.
4644
pub(crate) fn expand_column(
4745
cx: &mut ExtCtxt<'_>,
4846
sp: Span,
@@ -57,9 +55,7 @@ pub(crate) fn expand_column(
5755
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1)))
5856
}
5957

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.
6359
pub(crate) fn expand_file(
6460
cx: &mut ExtCtxt<'_>,
6561
sp: Span,
@@ -81,6 +77,7 @@ pub(crate) fn expand_file(
8177
)))
8278
}
8379

80+
/// Expand `stringify!($input)`.
8481
pub(crate) fn expand_stringify(
8582
cx: &mut ExtCtxt<'_>,
8683
sp: Span,
@@ -91,6 +88,7 @@ pub(crate) fn expand_stringify(
9188
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))))
9289
}
9390

91+
/// Expand `module_path!()` to (a textual representation of) the current module path.
9492
pub(crate) fn expand_mod(
9593
cx: &mut ExtCtxt<'_>,
9694
sp: Span,
@@ -104,9 +102,9 @@ pub(crate) fn expand_mod(
104102
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&string))))
105103
}
106104

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.
110108
pub(crate) fn expand_include<'cx>(
111109
cx: &'cx mut ExtCtxt<'_>,
112110
sp: Span,
@@ -187,7 +185,9 @@ pub(crate) fn expand_include<'cx>(
187185
ExpandResult::Ready(Box::new(ExpandInclude { p, node_id: cx.current_expansion.lint_node_id }))
188186
}
189187

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.
191191
pub(crate) fn expand_include_str(
192192
cx: &mut ExtCtxt<'_>,
193193
sp: Span,
@@ -206,6 +206,8 @@ pub(crate) fn expand_include_str(
206206
Ok((bytes, bsp)) => match std::str::from_utf8(&bytes) {
207207
Ok(src) => {
208208
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`).
209211
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
210212
}
211213
Err(utf8err) => {
@@ -218,6 +220,9 @@ pub(crate) fn expand_include_str(
218220
})
219221
}
220222

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.
221226
pub(crate) fn expand_include_bytes(
222227
cx: &mut ExtCtxt<'_>,
223228
sp: Span,
@@ -234,6 +239,9 @@ pub(crate) fn expand_include_bytes(
234239
};
235240
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
236241
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+
//
237245
// Don't care about getting the span for the raw bytes,
238246
// because the console can't really show them anyway.
239247
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(ByteSymbol::intern(&bytes)));

0 commit comments

Comments
 (0)