Skip to content

Commit b3bb786

Browse files
Rollup merge of rust-lang#88033 - GuillaumeGomez:jump-to-def-primitive, 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``
2 parents b0ee495 + d73c0a3 commit b3bb786

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/librustdoc/html/highlight.rs

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//!
66
//! Use the `render_with_highlighting` to highlight some rust code.
77
8+
use crate::clean::PrimitiveType;
89
use crate::html::escape::Escape;
910
use crate::html::render::Context;
1011

@@ -584,6 +585,13 @@ fn string<T: Display>(
584585
.ok()
585586
.map(|(url, _, _)| url)
586587
}
588+
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
589+
PrimitiveType::primitive_locations(context.tcx())[&prim],
590+
context,
591+
Some(context_info.root_path),
592+
)
593+
.ok()
594+
.map(|(url, _, _)| url),
587595
}
588596
})
589597
{

src/librustdoc/html/render/span_map.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::clean;
1+
use crate::clean::{self, PrimitiveType};
22
use crate::html::sources;
33

44
use rustc_data_structures::fx::FxHashMap;
@@ -22,6 +22,7 @@ use std::path::{Path, PathBuf};
2222
crate enum LinkFromSrc {
2323
Local(clean::Span),
2424
External(DefId),
25+
Primitive(PrimitiveType),
2526
}
2627

2728
/// This function will do at most two things:
@@ -73,17 +74,20 @@ impl<'tcx> SpanMapVisitor<'tcx> {
7374
Some(def_id)
7475
}
7576
Res::Local(_) => None,
77+
Res::PrimTy(p) => {
78+
// FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
79+
let span = path_span.unwrap_or(path.span);
80+
self.matches.insert(span, LinkFromSrc::Primitive(PrimitiveType::from(p)));
81+
return;
82+
}
7683
Res::Err => return,
7784
_ => return,
7885
};
7986
if let Some(span) = self.tcx.hir().res_span(path.res) {
80-
self.matches.insert(
81-
path_span.unwrap_or_else(|| path.span),
82-
LinkFromSrc::Local(clean::Span::new(span)),
83-
);
84-
} else if let Some(def_id) = info {
8587
self.matches
86-
.insert(path_span.unwrap_or_else(|| path.span), LinkFromSrc::External(def_id));
88+
.insert(path_span.unwrap_or(path.span), LinkFromSrc::Local(clean::Span::new(span)));
89+
} else if let Some(def_id) = info {
90+
self.matches.insert(path_span.unwrap_or(path.span), LinkFromSrc::External(def_id));
8791
}
8892
}
8993
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile-flags: -Zunstable-options --generate-link-to-definition
2+
3+
#![crate_name = "foo"]
4+
5+
// @has 'src/foo/check-source-code-urls-to-def-std.rs.html'
6+
7+
fn babar() {}
8+
9+
// @has - '//a[@href="{{channel}}/std/primitive.u32.html"]' 'u32'
10+
// @has - '//a[@href="{{channel}}/std/primitive.str.html"]' 'str'
11+
// @has - '//a[@href="{{channel}}/std/primitive.bool.html"]' 'bool'
12+
// @has - '//a[@href="../../src/foo/check-source-code-urls-to-def-std.rs.html#7"]' 'babar'
13+
pub fn foo(a: u32, b: &str, c: String) {
14+
let x = 12;
15+
let y: bool = true;
16+
babar();
17+
}

src/test/rustdoc/check-source-code-urls-to-def.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ impl Foo {
2727
fn babar() {}
2828

2929
// @has - '//a/@href' '/struct.String.html'
30+
// @has - '//a/@href' '/primitive.u32.html'
31+
// @has - '//a/@href' '/primitive.str.html'
3032
// @count - '//a[@href="../../src/foo/check-source-code-urls-to-def.rs.html#21"]' 5
3133
// @has - '//a[@href="../../source_code/struct.SourceCode.html"]' 'source_code::SourceCode'
3234
pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::SourceCode) {
@@ -40,5 +42,9 @@ pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::Sour
4042

4143
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'bar::sub::Trait'
4244
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
43-
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V) {
45+
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {
4446
}
47+
48+
// @has - '//a[@href="../../foo/primitive.bool.html"]' 'bool'
49+
#[doc(primitive = "bool")]
50+
mod whatever {}

0 commit comments

Comments
 (0)