Skip to content
/ rust Public
forked from rust-lang/rust

Commit 32443a8

Browse files
authored
Rollup merge of rust-lang#133458 - GuillaumeGomez:fix-prelude-tys-links, r=notriddle
Fix `Result` and `Option` not getting a jump to def link generated It was just because we didn't store the "span" in the `PreludeTy` variant. r? `@notriddle`
2 parents fa2c1ab + c839925 commit 32443a8

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/librustdoc/html/highlight.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ impl<'a, 'tcx, F: Write> TokenHandler<'a, 'tcx, F> {
181181
// current parent tag is not the same as our pending content.
182182
let close_tag = if self.pending_elems.len() > 1
183183
&& let Some(current_class) = current_class
184+
// `PreludeTy` can never include more than an ident so it should not generate
185+
// a wrapping `span`.
186+
&& !matches!(current_class, Class::PreludeTy(_))
184187
{
185188
Some(enter_span(self.out, current_class, &self.href_context))
186189
} else {
@@ -333,7 +336,7 @@ enum Class {
333336
/// `Ident` isn't rendered in the HTML but we still need it for the `Span` it contains.
334337
Ident(Span),
335338
Lifetime,
336-
PreludeTy,
339+
PreludeTy(Span),
337340
PreludeVal,
338341
QuestionMark,
339342
Decoration(&'static str),
@@ -381,7 +384,7 @@ impl Class {
381384
Class::Bool => "bool-val",
382385
Class::Ident(_) => "",
383386
Class::Lifetime => "lifetime",
384-
Class::PreludeTy => "prelude-ty",
387+
Class::PreludeTy(_) => "prelude-ty",
385388
Class::PreludeVal => "prelude-val",
386389
Class::QuestionMark => "question-mark",
387390
Class::Decoration(kind) => kind,
@@ -392,7 +395,7 @@ impl Class {
392395
/// a "span" (a tuple representing `(lo, hi)` equivalent of `Span`).
393396
fn get_span(self) -> Option<Span> {
394397
match self {
395-
Self::Ident(sp) | Self::Self_(sp) | Self::Macro(sp) => Some(sp),
398+
Self::Ident(sp) | Self::Self_(sp) | Self::Macro(sp) | Self::PreludeTy(sp) => Some(sp),
396399
Self::Comment
397400
| Self::DocComment
398401
| Self::Attribute
@@ -403,14 +406,14 @@ impl Class {
403406
| Self::Number
404407
| Self::Bool
405408
| Self::Lifetime
406-
| Self::PreludeTy
407409
| Self::PreludeVal
408410
| Self::QuestionMark
409411
| Self::Decoration(_) => None,
410412
}
411413
}
412414
}
413415

416+
#[derive(Debug)]
414417
enum Highlight<'a> {
415418
Token { text: &'a str, class: Option<Class> },
416419
EnterSpan { class: Class },
@@ -847,7 +850,7 @@ impl<'src> Classifier<'src> {
847850
}
848851
TokenKind::Ident => match get_real_ident_class(text, false) {
849852
None => match text {
850-
"Option" | "Result" => Class::PreludeTy,
853+
"Option" | "Result" => Class::PreludeTy(self.new_span(before, text)),
851854
"Some" | "None" | "Ok" | "Err" => Class::PreludeVal,
852855
// "union" is a weak keyword and is only considered as a keyword when declaring
853856
// a union type.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This test checks that prelude types like `Result` and `Option` still get a link generated.
2+
3+
//@ compile-flags: -Zunstable-options --generate-link-to-definition
4+
5+
#![crate_name = "foo"]
6+
7+
//@ has 'src/foo/jump-to-def-prelude-types.rs.html'
8+
// FIXME: would be nice to be able to check both the class and the href at the same time so
9+
// we could check the text as well...
10+
//@ has - '//a[@class="prelude-ty"]/@href' '{{channel}}/core/result/enum.Result.html'
11+
//@ has - '//a[@class="prelude-ty"]/@href' '{{channel}}/core/option/enum.Option.html'
12+
pub fn foo() -> Result<Option<()>, ()> { Err(()) }
13+
14+
// This part is to ensure that they are not linking to the actual prelude ty.
15+
pub mod bar {
16+
struct Result;
17+
struct Option;
18+
19+
//@ has - '//a[@href="#16"]' 'Result'
20+
pub fn bar() -> Result { Result }
21+
//@ has - '//a[@href="#17"]' 'Option'
22+
pub fn bar2() -> Option { Option }
23+
}

0 commit comments

Comments
 (0)