Skip to content

Commit

Permalink
Don't require a DocContext for report_diagnostic
Browse files Browse the repository at this point in the history
This is needed for the next commit, which needs access to the `cx` from
within the `decorate` closure.

- Change `as_local_hir_id` to an associated function, since it only
  needs a `TyCtxt`
- Change `source_span_for_markdown_range` to only take a `TyCtxt`
  • Loading branch information
jyn514 committed Mar 4, 2021
1 parent ec7f258 commit b3c4e25
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ impl<'tcx> DocContext<'tcx> {

/// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
/// (This avoids a slice-index-out-of-bounds panic.)
crate fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<HirId> {
if MAX_DEF_IDX.with(|m| {
m.borrow().get(&def_id.krate).map(|&idx| idx <= def_id.index).unwrap_or(false)
}) {
None
} else {
def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id))
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
}
}
Expand Down Expand Up @@ -479,7 +479,7 @@ crate fn run_global_ctxt(
https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html";
tcx.struct_lint_node(
crate::lint::MISSING_CRATE_LEVEL_DOCS,
ctxt.as_local_hir_id(m.def_id).unwrap(),
DocContext::as_local_hir_id(tcx, m.def_id).unwrap(),
|lint| {
let mut diag =
lint.build("no documentation found for this crate's top-level module");
Expand Down
9 changes: 6 additions & 3 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
let buffer = buffer.borrow();

if buffer.has_errors || is_empty {
let mut diag = if let Some(sp) =
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
{
let mut diag = if let Some(sp) = super::source_span_for_markdown_range(
self.cx.tcx,
&dox,
&code_block.range,
&item.attrs,
) {
let (warning_message, suggest_using_text) = if buffer.has_errors {
("could not parse code block as Rust code", true)
} else {
Expand Down
20 changes: 10 additions & 10 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ impl LinkCollector<'_, '_> {
suggest_disambiguator(resolved, diag, path_str, dox, sp, &ori_link.range);
};
report_diagnostic(
self.cx,
self.cx.tcx,
BROKEN_INTRA_DOC_LINKS,
&msg,
&item,
Expand Down Expand Up @@ -1220,7 +1220,7 @@ impl LinkCollector<'_, '_> {
&& !self.cx.tcx.features().intra_doc_pointers
{
let span = super::source_span_for_markdown_range(
self.cx,
self.cx.tcx,
dox,
&ori_link.range,
&item.attrs,
Expand Down Expand Up @@ -1674,15 +1674,15 @@ impl Suggestion {
/// parameter of the callback will contain it, and the primary span of the diagnostic will be set
/// to it.
fn report_diagnostic(
cx: &DocContext<'_>,
tcx: TyCtxt<'_>,
lint: &'static Lint,
msg: &str,
item: &Item,
dox: &str,
link_range: &Range<usize>,
decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>),
) {
let hir_id = match cx.as_local_hir_id(item.def_id) {
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) {
Some(hir_id) => hir_id,
None => {
// If non-local, no need to check anything.
Expand All @@ -1694,10 +1694,10 @@ fn report_diagnostic(
let attrs = &item.attrs;
let sp = span_of_attrs(attrs).unwrap_or(item.source.span());

cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
let mut diag = lint.build(msg);

let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs);
let span = super::source_span_for_markdown_range(tcx, dox, link_range, attrs);

if let Some(sp) = span {
diag.set_span(sp);
Expand Down Expand Up @@ -1742,7 +1742,7 @@ fn resolution_failure(
) {
let tcx = collector.cx.tcx;
report_diagnostic(
collector.cx,
tcx,
BROKEN_INTRA_DOC_LINKS,
&format!("unresolved link to `{}`", path_str),
item,
Expand Down Expand Up @@ -1973,7 +1973,7 @@ fn anchor_failure(
),
};

report_diagnostic(cx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| {
report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| {
if let Some(sp) = sp {
diag.span_label(sp, "contains invalid anchor");
}
Expand Down Expand Up @@ -2013,7 +2013,7 @@ fn ambiguity_error(
}
}

report_diagnostic(cx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| {
report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| {
if let Some(sp) = sp {
diag.span_label(sp, "ambiguous link");
} else {
Expand Down Expand Up @@ -2066,7 +2066,7 @@ fn privacy_error(cx: &DocContext<'_>, item: &Item, path_str: &str, dox: &str, li
let msg =
format!("public documentation for `{}` links to private item `{}`", item_name, path_str);

report_diagnostic(cx, PRIVATE_INTRA_DOC_LINKS, &msg, item, dox, &link.range, |diag, sp| {
report_diagnostic(cx.tcx, PRIVATE_INTRA_DOC_LINKS, &msg, item, dox, &link.range, |diag, sp| {
if let Some(sp) = sp {
diag.span_label(sp, "this item is private");
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/doc_test_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
}

crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
let hir_id = match cx.as_local_hir_id(item.def_id) {
let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) {
Some(hir_id) => hir_id,
None => {
// If non-local, no need to check anything.
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/passes/html_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ fn extract_tags(

impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
fn fold_item(&mut self, item: Item) -> Option<Item> {
let hir_id = match self.cx.as_local_hir_id(item.def_id) {
let tcx = self.cx.tcx;
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) {
Some(hir_id) => hir_id,
None => {
// If non-local, no need to check anything.
Expand All @@ -176,13 +177,12 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
};
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
if !dox.is_empty() {
let cx = &self.cx;
let report_diag = |msg: &str, range: &Range<usize>| {
let sp = match super::source_span_for_markdown_range(cx, &dox, range, &item.attrs) {
let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs) {
Some(sp) => sp,
None => span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
};
cx.tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| {
tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| {
lint.build(msg).emit()
});
};
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/passes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Contains information about "passes", used to modify crate information during the documentation
//! process.

use rustc_middle::ty::TyCtxt;
use rustc_span::{InnerSpan, Span, DUMMY_SP};
use std::ops::Range;

Expand Down Expand Up @@ -167,7 +168,7 @@ crate fn span_of_attrs(attrs: &clean::Attributes) -> Option<Span> {
/// attributes are not all sugared doc comments. It's difficult to calculate the correct span in
/// that case due to escaping and other source features.
crate fn source_span_for_markdown_range(
cx: &DocContext<'_>,
tcx: TyCtxt<'_>,
markdown: &str,
md_range: &Range<usize>,
attrs: &clean::Attributes,
Expand All @@ -179,7 +180,7 @@ crate fn source_span_for_markdown_range(
return None;
}

let snippet = cx.sess().source_map().span_to_snippet(span_of_attrs(attrs)?).ok()?;
let snippet = tcx.sess.source_map().span_to_snippet(span_of_attrs(attrs)?).ok()?;

let starting_line = markdown[..md_range.start].matches('\n').count();
let ending_line = starting_line + markdown[md_range.start..md_range.end].matches('\n').count();
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/non_autolinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ crate fn check_non_autolinks(krate: Crate, cx: &mut DocContext<'_>) -> Crate {

impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
fn fold_item(&mut self, item: Item) -> Option<Item> {
let hir_id = match self.cx.as_local_hir_id(item.def_id) {
let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) {
Some(hir_id) => hir_id,
None => {
// If non-local, no need to check anything.
Expand All @@ -70,7 +70,7 @@ impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
if !dox.is_empty() {
let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range<usize>| {
let sp = super::source_span_for_markdown_range(cx, &dox, &range, &item.attrs)
let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs)
.or_else(|| span_of_attrs(&item.attrs))
.unwrap_or(item.source.span());
cx.tcx.struct_span_lint_hir(crate::lint::NON_AUTOLINKS, hir_id, sp, |lint| {
Expand Down

0 comments on commit b3c4e25

Please sign in to comment.