From a26cb6154d3f357fccc15fea5e47ed6a82c9a8f3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 2 May 2022 15:06:45 +0200 Subject: [PATCH 1/4] Fix regression in link-to-definition introduced in #93803 --- src/librustdoc/html/render/span_map.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index b5502309560ee..02a52b2f98b07 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{ExprKind, GenericParam, HirId, Mod, Node}; +use rustc_hir::{ExprKind, Generics, HirId, Mod, Node, WherePredicate}; use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; use rustc_span::Span; @@ -100,7 +100,17 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> { self.tcx.hir() } - fn visit_generic_param(&mut self, _: &'tcx GenericParam<'tcx>) {} + fn visit_generics(&mut self, g: &'tcx Generics<'tcx>) { + for predicate in g.predicates { + if let WherePredicate::BoundPredicate(w) = predicate { + for bound in w.bounds { + if let Some(trait_ref) = bound.trait_ref() { + self.handle_path(trait_ref.path, None); + } + } + } + } + } fn visit_path(&mut self, path: &'tcx rustc_hir::Path<'tcx>, _id: HirId) { self.handle_path(path, None); From 5c7ce84d7409075803bf44bc38d3d212fa83feef Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 6 May 2022 21:54:31 +0200 Subject: [PATCH 2/4] Remove unneeded SpanMapVisitor::visit_generics function --- src/librustdoc/html/render/span_map.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index 02a52b2f98b07..1ae888d059dc6 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{ExprKind, Generics, HirId, Mod, Node, WherePredicate}; +use rustc_hir::{ExprKind, HirId, Mod, Node}; use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; use rustc_span::Span; @@ -100,18 +100,6 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> { self.tcx.hir() } - fn visit_generics(&mut self, g: &'tcx Generics<'tcx>) { - for predicate in g.predicates { - if let WherePredicate::BoundPredicate(w) = predicate { - for bound in w.bounds { - if let Some(trait_ref) = bound.trait_ref() { - self.handle_path(trait_ref.path, None); - } - } - } - } - } - fn visit_path(&mut self, path: &'tcx rustc_hir::Path<'tcx>, _id: HirId) { self.handle_path(path, None); intravisit::walk_path(self, path); From fd6b01f0e993eecd1d0eecc00b6cb53f6e0d0c0c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 2 May 2022 15:07:11 +0200 Subject: [PATCH 3/4] Add regression test for jump-to-def --- .../rustdoc/check-source-code-urls-to-def.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/test/rustdoc/check-source-code-urls-to-def.rs b/src/test/rustdoc/check-source-code-urls-to-def.rs index ca4179d403d69..12c5df2871cf5 100644 --- a/src/test/rustdoc/check-source-code-urls-to-def.rs +++ b/src/test/rustdoc/check-source-code-urls-to-def.rs @@ -46,6 +46,24 @@ pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::Sour // @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'Trait' pub fn foo2(t: &T, v: &V, b: bool) {} +pub trait AnotherTrait {} +pub trait WhyNot {} + +// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#49"]' 'AnotherTrait' +// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#50"]' 'WhyNot' +pub fn foo3(t: &T, v: &V) +where + T: AnotherTrait, + V: WhyNot +{} + +pub trait AnotherTrait2 {} + +// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#60"]' 'AnotherTrait2' +pub fn foo4() { + let x: Vec = Vec::new(); +} + // @has - '//a[@href="../../foo/primitive.bool.html"]' 'bool' #[doc(primitive = "bool")] mod whatever {} From 3bfa2eb9f09035dcd779910b77880252baaf2ef5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 2 May 2022 15:11:15 +0200 Subject: [PATCH 4/4] Add rustdoc documentation about unstable feature "jump to def" --- src/doc/rustdoc/src/unstable-features.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/doc/rustdoc/src/unstable-features.md b/src/doc/rustdoc/src/unstable-features.md index 537ab48bbfc12..30b3d6defb4b8 100644 --- a/src/doc/rustdoc/src/unstable-features.md +++ b/src/doc/rustdoc/src/unstable-features.md @@ -567,3 +567,10 @@ $ rustdoc src/lib.rs -Z unstable-options \ The example above check every well known names (`target_os`, `doc`, `test`, ... via `names()`) and check the values of `feature`: `foo` and `bar`. + +### `--generate-link-to-definition`: Generate links on types in source code + + * Tracking issue: [#89095](https://github.com/rust-lang/rust/issues/89095) + +This flag enables the generation of links in the source code pages which allow the reader +to jump to a type definition.