Skip to content

Commit

Permalink
Don't let rustdoc source code urls look at bodies
Browse files Browse the repository at this point in the history
It is too fragile (keeps ICEing) and will only ever work for a single cfg, but rustdoc looks at all cfgs at the same time
  • Loading branch information
oli-obk committed Mar 7, 2024
1 parent aa029ce commit 55bc8e9
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 133 deletions.
49 changes: 2 additions & 47 deletions src/librustdoc/html/render/span_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{ExprKind, HirId, Item, ItemKind, Mod, Node};
use rustc_middle::hir::nested_filter;
use rustc_hir::{HirId, Item, ItemKind, Mod, Node};
use rustc_middle::ty::TyCtxt;
use rustc_span::hygiene::MacroKind;
use rustc_span::{BytePos, ExpnKind, Span};
Expand Down Expand Up @@ -49,7 +48,7 @@ pub(crate) fn collect_spans_and_sources(

if include_sources {
if generate_link_to_definition {
tcx.hir().walk_toplevel_module(&mut visitor);
tcx.hir().visit_all_item_likes_in_crate(&mut visitor);
}
let sources = sources::collect_local_sources(tcx, src_root, krate);
(sources, visitor.matches)
Expand Down Expand Up @@ -154,37 +153,9 @@ impl<'tcx> SpanMapVisitor<'tcx> {
self.matches.insert(new_span, link_from_src);
true
}

fn handle_call(&mut self, hir_id: HirId, expr_hir_id: Option<HirId>, span: Span) {
let hir = self.tcx.hir();
let body_id = hir.enclosing_body_owner(hir_id);
// FIXME: this is showing error messages for parts of the code that are not
// compiled (because of cfg)!
//
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
let typeck_results = self
.tcx
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
// Interestingly enough, for method calls, we need the whole expression whereas for static
// method/function calls, we need the call expression specifically.
if let Some(def_id) = typeck_results.type_dependent_def_id(expr_hir_id.unwrap_or(hir_id)) {
let link = if def_id.as_local().is_some() {
LinkFromSrc::Local(rustc_span(def_id, self.tcx))
} else {
LinkFromSrc::External(def_id)
};
self.matches.insert(span, link);
}
}
}

impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
type NestedFilter = nested_filter::All;

fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}

fn visit_path(&mut self, path: &rustc_hir::Path<'tcx>, _id: HirId) {
if self.handle_macro(path.span) {
return;
Expand Down Expand Up @@ -212,22 +183,6 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
intravisit::walk_mod(self, m, id);
}

fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
match expr.kind {
ExprKind::MethodCall(segment, ..) => {
self.handle_call(segment.hir_id, Some(expr.hir_id), segment.ident.span)
}
ExprKind::Call(call, ..) => self.handle_call(call.hir_id, None, call.span),
_ => {
if self.handle_macro(expr.span) {
// We don't want to go deeper into the macro.
return;
}
}
}
intravisit::walk_expr(self, expr);
}

fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
match item.kind {
ItemKind::Static(_, _, _)
Expand Down
5 changes: 0 additions & 5 deletions tests/rustdoc/check-source-code-urls-to-def-std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ fn babar() {}

// @has - '//a[@href="{{channel}}/std/primitive.u32.html"]' 'u32'
// @has - '//a[@href="{{channel}}/std/primitive.str.html"]' 'str'
// @has - '//a[@href="{{channel}}/std/primitive.bool.html"]' 'bool'
// @has - '//a[@href="#7"]' 'babar'
pub fn foo(a: u32, b: &str, c: String) {
let x = 12;
let y: bool = true;
Expand All @@ -33,10 +31,7 @@ pub fn another_foo() {
// can't find any item or anything that could tell us that it comes from expansion.
// @!has - '//a[@href="#19"]' 'yolo!'
yolo!();
// @has - '//a[@href="{{channel}}/std/macro.eprintln.html"]' 'eprintln!'
eprintln!();
// @has - '//a[@href="#27-29"]' 'data!'
let x = data!(4);
// @has - '//a[@href="#23-25"]' 'bar!'
bar!(x);
}
10 changes: 4 additions & 6 deletions tests/rustdoc/check-source-code-urls-to-def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate source_code;
#[path = "auxiliary/source-code-bar.rs"]
pub mod bar;

// @count - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#5-7"]' 4
// @count - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#5-7"]' 2
use bar::Bar;
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#13-17"]' 'self'
// @has - '//pre[@class="rust"]//a[@href="auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
Expand All @@ -31,15 +31,14 @@ fn babar() {}
// @has - '//pre[@class="rust"]//a/@href' '/struct.String.html'
// @has - '//pre[@class="rust"]//a/@href' '/primitive.u32.html'
// @has - '//pre[@class="rust"]//a/@href' '/primitive.str.html'
// @count - '//pre[@class="rust"]//a[@href="#23"]' 5
// @count - '//pre[@class="rust"]//a[@href="#23"]' 2
// @has - '//pre[@class="rust"]//a[@href="../../source_code/struct.SourceCode.html"]' \
// 'source_code::SourceCode'
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
let x = 12;
let y: Foo = Foo;
let z: Bar = bar::Bar { field: Foo };
babar();
// @has - '//pre[@class="rust"]//a[@href="#26"]' 'hello'
y.hello();
}

Expand All @@ -50,8 +49,8 @@ pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}
pub trait AnotherTrait {}
pub trait WhyNot {}

// @has - '//pre[@class="rust"]//a[@href="#50"]' 'AnotherTrait'
// @has - '//pre[@class="rust"]//a[@href="#51"]' 'WhyNot'
// @has - '//pre[@class="rust"]//a[@href="#49"]' 'AnotherTrait'
// @has - '//pre[@class="rust"]//a[@href="#50"]' 'WhyNot'
pub fn foo3<T, V>(t: &T, v: &V)
where
T: AnotherTrait,
Expand All @@ -60,7 +59,6 @@ where

pub trait AnotherTrait2 {}

// @has - '//pre[@class="rust"]//a[@href="#61"]' 'AnotherTrait2'
pub fn foo4() {
let x: Vec<&dyn AnotherTrait2> = Vec::new();
}
Expand Down
27 changes: 0 additions & 27 deletions tests/rustdoc/jump-to-def-doc-links-calls.rs

This file was deleted.

48 changes: 0 additions & 48 deletions tests/rustdoc/jump-to-non-local-method.rs

This file was deleted.

0 comments on commit 55bc8e9

Please sign in to comment.