Skip to content

Commit

Permalink
Rollup merge of rust-lang#88033 - GuillaumeGomez:jump-to-def-primitiv…
Browse files Browse the repository at this point in the history
…e, r=jyn514

Add links for primitives in "jump to definition" feature

Follow-up of rust-lang#84176.

I created a function `primitive_from_str` which is code that was originally in `collect_intra_doc_links::resolve_primitive` to prevent code duplication.

I also created the `primitive_link_url` function which is somewhat similar to `primitive_link` but too much different to merge both of them.

r? `@jyn514`
  • Loading branch information
GuillaumeGomez committed Sep 13, 2021
2 parents b0ee495 + d73c0a3 commit f789108
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//!
//! Use the `render_with_highlighting` to highlight some rust code.

use crate::clean::PrimitiveType;
use crate::html::escape::Escape;
use crate::html::render::Context;

Expand Down Expand Up @@ -584,6 +585,13 @@ fn string<T: Display>(
.ok()
.map(|(url, _, _)| url)
}
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
PrimitiveType::primitive_locations(context.tcx())[&prim],
context,
Some(context_info.root_path),
)
.ok()
.map(|(url, _, _)| url),
}
})
{
Expand Down
18 changes: 11 additions & 7 deletions src/librustdoc/html/render/span_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::clean;
use crate::clean::{self, PrimitiveType};
use crate::html::sources;

use rustc_data_structures::fx::FxHashMap;
Expand All @@ -22,6 +22,7 @@ use std::path::{Path, PathBuf};
crate enum LinkFromSrc {
Local(clean::Span),
External(DefId),
Primitive(PrimitiveType),
}

/// This function will do at most two things:
Expand Down Expand Up @@ -73,17 +74,20 @@ impl<'tcx> SpanMapVisitor<'tcx> {
Some(def_id)
}
Res::Local(_) => None,
Res::PrimTy(p) => {
// FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
let span = path_span.unwrap_or(path.span);
self.matches.insert(span, LinkFromSrc::Primitive(PrimitiveType::from(p)));
return;
}
Res::Err => return,
_ => return,
};
if let Some(span) = self.tcx.hir().res_span(path.res) {
self.matches.insert(
path_span.unwrap_or_else(|| path.span),
LinkFromSrc::Local(clean::Span::new(span)),
);
} else if let Some(def_id) = info {
self.matches
.insert(path_span.unwrap_or_else(|| path.span), LinkFromSrc::External(def_id));
.insert(path_span.unwrap_or(path.span), LinkFromSrc::Local(clean::Span::new(span)));
} else if let Some(def_id) = info {
self.matches.insert(path_span.unwrap_or(path.span), LinkFromSrc::External(def_id));
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/rustdoc/check-source-code-urls-to-def-std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// compile-flags: -Zunstable-options --generate-link-to-definition

#![crate_name = "foo"]

// @has 'src/foo/check-source-code-urls-to-def-std.rs.html'

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="../../src/foo/check-source-code-urls-to-def-std.rs.html#7"]' 'babar'
pub fn foo(a: u32, b: &str, c: String) {
let x = 12;
let y: bool = true;
babar();
}
8 changes: 7 additions & 1 deletion src/test/rustdoc/check-source-code-urls-to-def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ impl Foo {
fn babar() {}

// @has - '//a/@href' '/struct.String.html'
// @has - '//a/@href' '/primitive.u32.html'
// @has - '//a/@href' '/primitive.str.html'
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#21"]' 5
// @has - '//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) {
Expand All @@ -40,5 +42,9 @@ 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"]' 'bar::sub::Trait'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V) {
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {
}

// @has - '//a[@href="../../foo/primitive.bool.html"]' 'bool'
#[doc(primitive = "bool")]
mod whatever {}

0 comments on commit f789108

Please sign in to comment.