Skip to content

Commit ec87250

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

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 18 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,7 @@ 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+
// MacEager converts the expr into a pat if need be.
209210
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
210211
}
211212
Err(utf8err) => {
@@ -218,6 +219,9 @@ pub(crate) fn expand_include_str(
218219
})
219220
}
220221

222+
/// Expand `include_bytes!($input)` to the content of the file given by path `$input`.
223+
///
224+
/// This works in expression, pattern and statement position.
221225
pub(crate) fn expand_include_bytes(
222226
cx: &mut ExtCtxt<'_>,
223227
sp: Span,
@@ -237,6 +241,7 @@ pub(crate) fn expand_include_bytes(
237241
// Don't care about getting the span for the raw bytes,
238242
// because the console can't really show them anyway.
239243
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(ByteSymbol::intern(&bytes)));
244+
// MacEager converts the expr into a pat if need be.
240245
MacEager::expr(expr)
241246
}
242247
Err(dummy) => dummy,

0 commit comments

Comments
 (0)