Skip to content

Commit b2b9b81

Browse files
committed
Account for doc comments coming from proc macros without spans
1 parent 9b91b9c commit b2b9b81

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

src/librustdoc/passes/check_code_block_syntax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
6969
// We couldn't calculate the span of the markdown block that had the error, so our
7070
// diagnostics are going to be a bit lacking.
7171
let mut diag = self.cx.sess().struct_span_warn(
72-
super::span_of_attrs(&item.attrs),
72+
super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
7373
"doc comment contains an invalid Rust code block",
7474
);
7575

src/librustdoc/passes/collect_intra_doc_links.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ fn resolution_failure(
465465
}
466466
};
467467
let attrs = &item.attrs;
468-
let sp = span_of_attrs(attrs);
468+
let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
469469

470470
let mut diag = cx.tcx.struct_span_lint_hir(
471471
lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
@@ -517,7 +517,7 @@ fn ambiguity_error(
517517
}
518518
};
519519
let attrs = &item.attrs;
520-
let sp = span_of_attrs(attrs);
520+
let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
521521

522522
let mut msg = format!("`{}` is ", path_str);
523523

src/librustdoc/passes/mod.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pub fn look_for_tests<'tcx>(
339339
find_testable_code(&dox, &mut tests, ErrorCodes::No);
340340

341341
if check_missing_code == true && tests.found_tests == 0 {
342-
let sp = span_of_attrs(&item.attrs).substitute_dummy(item.source.span());
342+
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
343343
let mut diag = cx.tcx.struct_span_lint_hir(
344344
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
345345
hir_id,
@@ -352,20 +352,23 @@ pub fn look_for_tests<'tcx>(
352352
let mut diag = cx.tcx.struct_span_lint_hir(
353353
lint::builtin::PRIVATE_DOC_TESTS,
354354
hir_id,
355-
span_of_attrs(&item.attrs),
355+
span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
356356
"Documentation test in private item");
357357
diag.emit();
358358
}
359359
}
360360

361361
/// Returns a span encompassing all the given attributes.
362-
crate fn span_of_attrs(attrs: &clean::Attributes) -> Span {
362+
crate fn span_of_attrs(attrs: &clean::Attributes) -> Option<Span> {
363363
if attrs.doc_strings.is_empty() {
364-
return DUMMY_SP;
364+
return None;
365365
}
366366
let start = attrs.doc_strings[0].span();
367+
if start == DUMMY_SP {
368+
return None;
369+
}
367370
let end = attrs.doc_strings.last().expect("No doc strings provided").span();
368-
start.to(end)
371+
Some(start.to(end))
369372
}
370373

371374
/// Attempts to match a range of bytes from parsed markdown to a `Span` in the source code.
@@ -391,7 +394,7 @@ crate fn source_span_for_markdown_range(
391394
let snippet = cx
392395
.sess()
393396
.source_map()
394-
.span_to_snippet(span_of_attrs(attrs))
397+
.span_to_snippet(span_of_attrs(attrs)?)
395398
.ok()?;
396399

397400
let starting_line = markdown[..md_range.start].matches('\n').count();
@@ -441,10 +444,8 @@ crate fn source_span_for_markdown_range(
441444
}
442445
}
443446

444-
let sp = span_of_attrs(attrs).from_inner(InnerSpan::new(
447+
Some(span_of_attrs(attrs)?.from_inner(InnerSpan::new(
445448
md_range.start + start_bytes,
446449
md_range.end + start_bytes + end_bytes,
447-
));
448-
449-
Some(sp)
450+
)))
450451
}

0 commit comments

Comments
 (0)