Skip to content

rustdoc: use descriptive tooltip if doctest is conditionally ignored #141517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Use the `render_with_highlighting` to highlight some rust code.

use std::collections::VecDeque;
use std::fmt::{Display, Write};
use std::fmt::{self, Display, Write};

use rustc_data_structures::fx::FxIndexMap;
use rustc_lexer::{Cursor, FrontmatterAllowed, LiteralKind, TokenKind};
Expand Down Expand Up @@ -36,9 +36,10 @@ pub(crate) struct HrefContext<'a, 'tcx> {
#[derive(Default)]
pub(crate) struct DecorationInfo(pub(crate) FxIndexMap<&'static str, Vec<(u32, u32)>>);

#[derive(Eq, PartialEq, Clone, Copy)]
#[derive(Eq, PartialEq, Clone)]
pub(crate) enum Tooltip {
Ignore,
IgnoreAll,
IgnoreSome(Vec<String>),
CompileFail,
ShouldPanic,
Edition(Edition),
Expand Down Expand Up @@ -70,7 +71,7 @@ fn write_header(
format_args!(
"<div class=\"example-wrap{}\">",
match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::IgnoreAll | Tooltip::IgnoreSome(_) => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Expand All @@ -80,18 +81,46 @@ fn write_header(
);

if tooltip != Tooltip::None {
let edition_code;
// variable for extending lifetimes of temporaries
let tmp;
write_str(
out,
format_args!(
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
match tooltip {
Tooltip::Ignore => "This example is not tested",
Tooltip::IgnoreAll => "This example is not tested",
Tooltip::IgnoreSome(platforms) => {
tmp = format!(
"This example is not tested on {}",
fmt::from_fn(|f| {
match platforms.len() {
0 => unreachable!(),
1 => f.write_str(&platforms[0]),
2 => write!(f, "{} or {}", &platforms[0], &platforms[1]),
_ => {
for (i, plat) in platforms.iter().enumerate() {
match (platforms.len() - 2).cmp(&i) {
std::cmp::Ordering::Greater => {
write!(f, "{}, ", plat)?
}
std::cmp::Ordering::Equal => {
write!(f, "{}, or ", plat)?
}
std::cmp::Ordering::Less => f.write_str(&plat)?,
}
}
Ok(())
}
}
})
);
&tmp
}
Tooltip::CompileFail => "This example deliberately fails to compile",
Tooltip::ShouldPanic => "This example panics",
Tooltip::Edition(edition) => {
edition_code = format!("This example runs with edition {edition}");
&edition_code
tmp = format!("This example runs with edition {edition}");
&tmp
}
Tooltip::None => unreachable!(),
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,10 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
))
});

let tooltip = if ignore != Ignore::None {
highlight::Tooltip::Ignore
let tooltip = if ignore == Ignore::All {
highlight::Tooltip::IgnoreAll
} else if let Ignore::Some(platforms) = ignore {
highlight::Tooltip::IgnoreSome(platforms)
} else if compile_fail {
highlight::Tooltip::CompileFail
} else if should_panic {
Expand Down
23 changes: 23 additions & 0 deletions tests/rustdoc/doctest/ignore-sometimes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![crate_name = "foo"]

// test for https://github.com/rust-lang/rust/issues/141092

//@ has 'foo/fn.f.html' '//a[@title="This example is not tested on wasm"]' 'ⓘ'
/// Example
///
/// ```ignore-wasm
/// let x = 1;
/// ```
pub fn f() {}

//@ has 'foo/fn.g.html' '//a[@title="This example is not tested on wasm or windows"]' 'ⓘ'
/// ```ignore-wasm,ignore-windows
/// let x = 1;
/// ```
pub fn g() {}

//@ has 'foo/fn.h.html' '//a[@title="This example is not tested on wasm, windows, or unix"]' 'ⓘ'
/// ```ignore-wasm,ignore-windows,ignore-unix
/// let x = 1;
/// ```
pub fn h() {}
Loading