Skip to content

Commit

Permalink
Rollup merge of rust-lang#63930 - estebank:rustdoc-ice, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Account for doc comments coming from proc macros without spans

Fix rust-lang#63821.
  • Loading branch information
Centril authored Sep 5, 2019
2 parents e56fe69 + 7ed542d commit bdcc539
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
// We couldn't calculate the span of the markdown block that had the error, so our
// diagnostics are going to be a bit lacking.
let mut diag = self.cx.sess().struct_span_warn(
super::span_of_attrs(&item.attrs),
super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
"doc comment contains an invalid Rust code block",
);

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ fn resolution_failure(
}
};
let attrs = &item.attrs;
let sp = span_of_attrs(attrs);
let sp = span_of_attrs(attrs).unwrap_or(item.source.span());

let mut diag = cx.tcx.struct_span_lint_hir(
lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
Expand Down Expand Up @@ -517,7 +517,7 @@ fn ambiguity_error(
}
};
let attrs = &item.attrs;
let sp = span_of_attrs(attrs);
let sp = span_of_attrs(attrs).unwrap_or(item.source.span());

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

Expand Down
21 changes: 11 additions & 10 deletions src/librustdoc/passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub fn look_for_tests<'tcx>(
find_testable_code(&dox, &mut tests, ErrorCodes::No);

if check_missing_code == true && tests.found_tests == 0 {
let sp = span_of_attrs(&item.attrs).substitute_dummy(item.source.span());
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
let mut diag = cx.tcx.struct_span_lint_hir(
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
hir_id,
Expand All @@ -352,20 +352,23 @@ pub fn look_for_tests<'tcx>(
let mut diag = cx.tcx.struct_span_lint_hir(
lint::builtin::PRIVATE_DOC_TESTS,
hir_id,
span_of_attrs(&item.attrs),
span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
"Documentation test in private item");
diag.emit();
}
}

/// Returns a span encompassing all the given attributes.
crate fn span_of_attrs(attrs: &clean::Attributes) -> Span {
crate fn span_of_attrs(attrs: &clean::Attributes) -> Option<Span> {
if attrs.doc_strings.is_empty() {
return DUMMY_SP;
return None;
}
let start = attrs.doc_strings[0].span();
if start == DUMMY_SP {
return None;
}
let end = attrs.doc_strings.last().expect("No doc strings provided").span();
start.to(end)
Some(start.to(end))
}

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

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

let sp = span_of_attrs(attrs).from_inner(InnerSpan::new(
Some(span_of_attrs(attrs)?.from_inner(InnerSpan::new(
md_range.start + start_bytes,
md_range.end + start_bytes + end_bytes,
));

Some(sp)
)))
}
20 changes: 20 additions & 0 deletions src/test/rustdoc/auxiliary/through-proc-macro-aux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// force-host
// no-prefer-dynamic
#![crate_type = "proc-macro"]
#![crate_name="some_macros"]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn first(_attr: TokenStream, item: TokenStream) -> TokenStream {
item // This doesn't erase the spans.
}

#[proc_macro_attribute]
pub fn second(_attr: TokenStream, item: TokenStream) -> TokenStream {
// Make a new `TokenStream` to erase the spans:
let mut out: TokenStream = TokenStream::new();
out.extend(item);
out
}
12 changes: 12 additions & 0 deletions src/test/rustdoc/through-proc-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// aux-build:through-proc-macro-aux.rs
// build-aux-docs
#![warn(intra_doc_link_resolution_failure)]
extern crate some_macros;

#[some_macros::second]
pub enum Boom {
/// [Oooops]
Bam,
}

fn main() {}

0 comments on commit bdcc539

Please sign in to comment.