Skip to content

Commit ffea7e2

Browse files
authored
Rollup merge of #123204 - notriddle:notriddle/include-str-span, r=pnkfelix
rustdoc: point at span in `include_str!`-ed md file Fixes #118549
2 parents bd71213 + 1c41dd6 commit ffea7e2

File tree

16 files changed

+141
-55
lines changed

16 files changed

+141
-55
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ pub fn expand_include_str(
196196
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
197197
};
198198
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
199-
Ok(bytes) => match std::str::from_utf8(&bytes) {
199+
Ok((bytes, bsp)) => match std::str::from_utf8(&bytes) {
200200
Ok(src) => {
201201
let interned_src = Symbol::intern(src);
202-
MacEager::expr(cx.expr_str(sp, interned_src))
202+
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
203203
}
204204
Err(_) => {
205205
let guar = cx.dcx().span_err(sp, format!("`{path}` wasn't a utf-8 file"));
@@ -225,7 +225,9 @@ pub fn expand_include_bytes(
225225
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
226226
};
227227
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
228-
Ok(bytes) => {
228+
Ok((bytes, _bsp)) => {
229+
// Don't care about getting the span for the raw bytes,
230+
// because the console can't really show them anyway.
229231
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes));
230232
MacEager::expr(expr)
231233
}
@@ -238,7 +240,7 @@ fn load_binary_file(
238240
original_path: &Path,
239241
macro_span: Span,
240242
path_span: Span,
241-
) -> Result<Lrc<[u8]>, Box<dyn MacResult>> {
243+
) -> Result<(Lrc<[u8]>, Span), Box<dyn MacResult>> {
242244
let resolved_path = match resolve_path(&cx.sess, original_path, macro_span) {
243245
Ok(path) => path,
244246
Err(err) => {

compiler/rustc_errors/src/emitter.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,9 @@ impl HumanEmitter {
15131513
for line_idx in 0..annotated_file.lines.len() {
15141514
let file = annotated_file.file.clone();
15151515
let line = &annotated_file.lines[line_idx];
1516-
if let Some(source_string) = file.get_line(line.line_index - 1) {
1516+
if let Some(source_string) =
1517+
line.line_index.checked_sub(1).and_then(|l| file.get_line(l))
1518+
{
15171519
let leading_whitespace = source_string
15181520
.chars()
15191521
.take_while(|c| c.is_whitespace())
@@ -1553,7 +1555,10 @@ impl HumanEmitter {
15531555
for line in &annotated_file.lines {
15541556
max_line_len = max(
15551557
max_line_len,
1556-
annotated_file.file.get_line(line.line_index - 1).map_or(0, |s| s.len()),
1558+
line.line_index
1559+
.checked_sub(1)
1560+
.and_then(|l| annotated_file.file.get_line(l))
1561+
.map_or(0, |s| s.len()),
15571562
);
15581563
for ann in &line.annotations {
15591564
span_right_margin = max(span_right_margin, ann.start_col.display);

compiler/rustc_resolve/src/rustdoc.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ pub fn attrs_to_doc_fragments<'a>(
194194
for (attr, item_id) in attrs {
195195
if let Some((doc_str, comment_kind)) = attr.doc_str_and_comment_kind() {
196196
let doc = beautify_doc_string(doc_str, comment_kind);
197-
let kind = if attr.is_doc_comment() {
198-
DocFragmentKind::SugaredDoc
197+
let (span, kind) = if attr.is_doc_comment() {
198+
(attr.span, DocFragmentKind::SugaredDoc)
199199
} else {
200-
DocFragmentKind::RawDoc
200+
(span_for_value(attr), DocFragmentKind::RawDoc)
201201
};
202-
let fragment = DocFragment { span: attr.span, doc, kind, item_id, indent: 0 };
202+
let fragment = DocFragment { span, doc, kind, item_id, indent: 0 };
203203
doc_fragments.push(fragment);
204204
} else if !doc_only {
205205
other_attrs.push(attr.clone());
@@ -211,6 +211,16 @@ pub fn attrs_to_doc_fragments<'a>(
211211
(doc_fragments, other_attrs)
212212
}
213213

214+
fn span_for_value(attr: &ast::Attribute) -> Span {
215+
if let ast::AttrKind::Normal(normal) = &attr.kind
216+
&& let ast::AttrArgs::Eq(_, ast::AttrArgsEq::Hir(meta)) = &normal.item.args
217+
{
218+
meta.span.with_ctxt(attr.span.ctxt())
219+
} else {
220+
attr.span
221+
}
222+
}
223+
214224
/// Return the doc-comments on this item, grouped by the module they came from.
215225
/// The module can be different if this is a re-export with added documentation.
216226
///
@@ -482,15 +492,36 @@ pub fn span_of_fragments(fragments: &[DocFragment]) -> Option<Span> {
482492

483493
/// Attempts to match a range of bytes from parsed markdown to a `Span` in the source code.
484494
///
485-
/// This method will return `None` if we cannot construct a span from the source map or if the
486-
/// fragments are not all sugared doc comments. It's difficult to calculate the correct span in
487-
/// that case due to escaping and other source features.
495+
/// This method does not always work, because markdown bytes don't necessarily match source bytes,
496+
/// like if escapes are used in the string. In this case, it returns `None`.
497+
///
498+
/// This method will return `Some` only if:
499+
///
500+
/// - The doc is made entirely from sugared doc comments, which cannot contain escapes
501+
/// - The doc is entirely from a single doc fragment, with a string literal, exactly equal
502+
/// - The doc comes from `include_str!`
488503
pub fn source_span_for_markdown_range(
489504
tcx: TyCtxt<'_>,
490505
markdown: &str,
491506
md_range: &Range<usize>,
492507
fragments: &[DocFragment],
493508
) -> Option<Span> {
509+
if let &[fragment] = &fragments
510+
&& fragment.kind == DocFragmentKind::RawDoc
511+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(fragment.span)
512+
&& snippet.trim_end() == markdown.trim_end()
513+
&& let Ok(md_range_lo) = u32::try_from(md_range.start)
514+
&& let Ok(md_range_hi) = u32::try_from(md_range.end)
515+
{
516+
// Single fragment with string that contains same bytes as doc.
517+
return Some(Span::new(
518+
fragment.span.lo() + rustc_span::BytePos(md_range_lo),
519+
fragment.span.lo() + rustc_span::BytePos(md_range_hi),
520+
fragment.span.ctxt(),
521+
fragment.span.parent(),
522+
));
523+
}
524+
494525
let is_all_sugared_doc = fragments.iter().all(|frag| frag.kind == DocFragmentKind::SugaredDoc);
495526

496527
if !is_all_sugared_doc {

compiler/rustc_span/src/source_map.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl SourceMap {
218218
///
219219
/// Unlike `load_file`, guarantees that no normalization like BOM-removal
220220
/// takes place.
221-
pub fn load_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
221+
pub fn load_binary_file(&self, path: &Path) -> io::Result<(Lrc<[u8]>, Span)> {
222222
let bytes = self.file_loader.read_binary_file(path)?;
223223

224224
// We need to add file to the `SourceMap`, so that it is present
@@ -227,8 +227,16 @@ impl SourceMap {
227227
// via `mod`, so we try to use real file contents and not just an
228228
// empty string.
229229
let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
230-
self.new_source_file(path.to_owned().into(), text);
231-
Ok(bytes)
230+
let file = self.new_source_file(path.to_owned().into(), text);
231+
Ok((
232+
bytes,
233+
Span::new(
234+
file.start_pos,
235+
BytePos(file.start_pos.0 + file.source_len.0),
236+
SyntaxContext::root(),
237+
None,
238+
),
239+
))
232240
}
233241

234242
// By returning a `MonotonicVec`, we ensure that consumers cannot invalidate

src/tools/clippy/clippy_lints/src/large_include_file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl LateLintPass<'_> for LargeIncludeFile {
7171
span_lint_and_note(
7272
cx,
7373
LARGE_INCLUDE_FILE,
74-
expr.span,
74+
expr.span.source_callsite(),
7575
"attempted to include a large file",
7676
None,
7777
format!(

src/tools/clippy/clippy_lints/src/strings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
300300
e.span,
301301
"calling `as_bytes()` on `include_str!(..)`",
302302
"consider using `include_bytes!(..)` instead",
303-
snippet_with_applicability(cx, receiver.span, r#""foo""#, &mut applicability).replacen(
303+
snippet_with_applicability(cx, receiver.span.source_callsite(), r#""foo""#, &mut applicability).replacen(
304304
"include_str",
305305
"include_bytes",
306306
1,

src/tools/clippy/tests/ui-toml/large_include_file/large_include_file.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | const TOO_BIG_INCLUDE_BYTES: &[u8; 654] = include_bytes!("too_big.txt");
77
= note: the configuration allows a maximum size of 600 bytes
88
= note: `-D clippy::large-include-file` implied by `-D warnings`
99
= help: to override `-D warnings` add `#[allow(clippy::large_include_file)]`
10-
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)
1110

1211
error: attempted to include a large file
1312
--> tests/ui-toml/large_include_file/large_include_file.rs:14:35
@@ -16,7 +15,6 @@ LL | const TOO_BIG_INCLUDE_STR: &str = include_str!("too_big.txt");
1615
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1716
|
1817
= note: the configuration allows a maximum size of 600 bytes
19-
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info)
2018

2119
error: aborting due to 2 previous errors
2220

src/tools/clippy/tests/ui/empty_docs.stderr

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ LL | ///
2525
= help: consider removing or filling it
2626

2727
error: empty doc comment
28-
--> tests/ui/empty_docs.rs:30:5
28+
--> tests/ui/empty_docs.rs:30:13
2929
|
3030
LL | #[doc = ""]
31-
| ^^^^^^^^^^^
31+
| ^^
3232
|
3333
= help: consider removing or filling it
3434

3535
error: empty doc comment
36-
--> tests/ui/empty_docs.rs:33:5
36+
--> tests/ui/empty_docs.rs:33:13
3737
|
38-
LL | / #[doc = ""]
38+
LL | #[doc = ""]
39+
| _____________^
3940
LL | | #[doc = ""]
40-
| |_______________^
41+
| |______________^
4142
|
4243
= help: consider removing or filling it
4344

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
HEADS UP! https://example.com MUST SHOW UP IN THE STDERR FILE!
2+
3+
Normally, a line with errors on it will also have a comment
4+
marking it up as something that needs to generate an error.
5+
6+
The test harness doesn't gather hot comments from this file.
7+
Rustdoc will generate an error for the line, and the `.stderr`
8+
snapshot includes this error, but Compiletest doesn't see it.
9+
10+
If the stderr file changes, make sure the warning points at the URL!
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://github.com/rust-lang/rust/issues/118549
2+
//
3+
// HEADS UP!
4+
//
5+
// Normally, a line with errors on it will also have a comment
6+
// marking it up as something that needs to generate an error.
7+
//
8+
// The test harness doesn't gather hot comments from the `.md` file.
9+
// Rustdoc will generate an error for the line, and the `.stderr`
10+
// snapshot includes this error, but Compiletest doesn't see it.
11+
//
12+
// If the stderr file changes, make sure the warning points at the URL!
13+
14+
#![deny(rustdoc::bare_urls)]
15+
#![doc=include_str!("auxiliary/include-str-bare-urls.md")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: this URL is not a hyperlink
2+
--> $DIR/auxiliary/include-str-bare-urls.md:1:11
3+
|
4+
LL | HEADS UP! https://example.com MUST SHOW UP IN THE STDERR FILE!
5+
| ^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com>`
6+
|
7+
= note: bare URLs are not automatically turned into clickable links
8+
note: the lint level is defined here
9+
--> $DIR/include-str-bare-urls.rs:14:9
10+
|
11+
LL | #![deny(rustdoc::bare_urls)]
12+
| ^^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to 1 previous error
15+

tests/rustdoc-ui/intra-doc/warning.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ pub fn d() {}
4747

4848
macro_rules! f {
4949
($f:expr) => {
50-
#[doc = $f] //~ WARNING `BarF`
50+
#[doc = $f]
5151
pub fn f() {}
5252
}
5353
}
54-
f!("Foo\nbar [BarF] bar\nbaz");
54+
f!("Foo\nbar [BarF] bar\nbaz"); //~ WARNING `BarF`
5555

5656
/** # for example,
5757
*

tests/rustdoc-ui/intra-doc/warning.stderr

+8-11
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ LL | bar [BarC] bar
6969
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
7070

7171
warning: unresolved link to `BarD`
72-
--> $DIR/warning.rs:45:1
72+
--> $DIR/warning.rs:45:9
7373
|
7474
LL | #[doc = "Foo\nbar [BarD] bar\nbaz"]
75-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
7676
|
7777
= note: the link appears in this line:
7878

@@ -82,13 +82,10 @@ LL | #[doc = "Foo\nbar [BarD] bar\nbaz"]
8282
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
8383

8484
warning: unresolved link to `BarF`
85-
--> $DIR/warning.rs:50:9
85+
--> $DIR/warning.rs:54:4
8686
|
87-
LL | #[doc = $f]
88-
| ^^^^^^^^^^^
89-
...
9087
LL | f!("Foo\nbar [BarF] bar\nbaz");
91-
| ------------------------------ in this macro invocation
88+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
9289
|
9390
= note: the link appears in this line:
9491

@@ -115,10 +112,10 @@ LL | * time to introduce a link [error]
115112
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
116113

117114
warning: unresolved link to `error`
118-
--> $DIR/warning.rs:68:1
115+
--> $DIR/warning.rs:68:9
119116
|
120117
LL | #[doc = "single line [error]"]
121-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118+
| ^^^^^^^^^^^^^^^^^^^^^
122119
|
123120
= note: the link appears in this line:
124121

@@ -128,10 +125,10 @@ LL | #[doc = "single line [error]"]
128125
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
129126

130127
warning: unresolved link to `error`
131-
--> $DIR/warning.rs:71:1
128+
--> $DIR/warning.rs:71:9
132129
|
133130
LL | #[doc = "single line with \"escaping\" [error]"]
134-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135132
|
136133
= note: the link appears in this line:
137134

tests/rustdoc-ui/invalid-syntax.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ LL | | /// ```
9090
= note: error from rustc: unknown start of token: \
9191

9292
warning: could not parse code block as Rust code
93-
--> $DIR/invalid-syntax.rs:70:1
93+
--> $DIR/invalid-syntax.rs:70:9
9494
|
95-
LL | / #[doc = "```"]
95+
LL | #[doc = "```"]
96+
| _________^
9697
LL | | /// \_
9798
LL | | #[doc = "```"]
98-
| |______________^
99+
| |_____________^
99100
|
100101
= help: mark blocks that do not contain Rust code as text: ```text
101102
= note: error from rustc: unknown start of token: \

0 commit comments

Comments
 (0)